Skip to content

Commit 433a6ba

Browse files
committed
Docs upgrades & optimization
1 parent 8b7bcf0 commit 433a6ba

23 files changed

Lines changed: 343 additions & 364 deletions

dev/client/clientTests.client.luau

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
--!native
2+
--!optimize 2
13
local ReplicatedStorage = game:GetService("ReplicatedStorage")
24

35
local testPackets = require(ReplicatedStorage.shared.testPackets)
46

5-
ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
6-
--print(data)
7-
end)
7+
--ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
8+
--print(data)
9+
--end)
810

911
testPackets.myPacket.listen(function()
1012
--print("Confirming server -> client")

dev/server/serverTests.server.luau

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ local testPackets = require(ReplicatedStorage.shared.testPackets)
1919
}]]
2020

2121
local out = {}
22-
for _ = 1, 1000 do
22+
for _ = 1, 100 do
2323
table.insert(out, {
2424
a = 5,
2525
b = 4,

docs/api/dataTypes/Primitives.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
ByteNet provides a large amount of "primitive" types for you to build more complex types that suit your game. Since primitive types don't need any parameters, you can just access them like the following: `ByteNet.<typename>`. For building more complex data structures, go look at the <a href="../Specials">Specials page.</a>
88

99
---
10-
## Supported generic types
10+
## Supported general types
1111
- `string`: String
1212
- `buff`: Buffer
1313
- `bool`: Boolean
14+
- `nothing`: Nothing
15+
- `unknown`: Any type
1416
---
1517
## Supported number types
1618
- `uint8`: Unsigned 8-bit integer
@@ -26,4 +28,5 @@ ByteNet provides a large amount of "primitive" types for you to build more compl
2628
- `cframe`: CoordinateFrame
2729
- `vec2`: Vector2
2830
- `vec3`: Vector3
31+
- `inst`: Instance
2932
---

docs/api/dataTypes/Specials.md

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,47 @@ Special types are how complex packet types are made. They can take in nearly any
99
Special types always take *parameters*. You have to call them: `ByteNet.<type name>(<any primitive type>)`.
1010

1111
!!!danger
12-
### There are drawbacks to using these!
13-
1412
- Using these types incurs 1-2 bytes of overhead due to the dynamic nature.
1513
- They take drastically more time to parse
1614
- They are heavier on memory usage, as a new closure is created each time. You will never have to worry about this unless you have dozens of packets, though.
1715

1816
---
1917

18+
## Structs
19+
Structs let you send structured data in the form of dictionaries. The fields are optimized away, so they don't take any bandwidth. Structs are how you'll be sending most of your data likely: they are the best for organization, and they let you do really interesting things. For example:
20+
```lua title="chunkPackets.luau"
21+
return ByteNet.defineNamespace("chunks", function()
22+
local chunkData = ByteNet.struct({
23+
biome = ByteNet.uint8,
24+
seed = ByteNet.int32,
25+
})
26+
27+
return {
28+
sendChunks = ByteNet.definePacket({
29+
value = ByteNet.array(chunkData)
30+
})
31+
}
32+
end)
33+
```
34+
2035
## Optionals
2136
Optional types are a cool name for the concept of "This doesn't have to exist". It's good for optional parameters: for example if some invoked function were to fail, you might want to send back a blank copy to indicate that something is wrong.
2237

2338
```lua title="packets.luau"
24-
return {
25-
myCoolPacket = ByteNet.definePacket({
26-
structure = {
27-
-- An "optional" type takes in a parameter.
28-
-- This can be anything! You can even have optional arrays.
29-
helloWorldString = ByteNet.optional(ByteNet.string)
30-
31-
-- This works!
32-
eachCharacter = ByteNet.optional(ByteNet.array(ByteNet.string))
33-
},
34-
})
35-
}
39+
return ByteNet.defineNamespace("chunks", function()
40+
return {
41+
myCoolPacket = ByteNet.definePacket({
42+
value = ByteNet.struct({
43+
-- An "optional" type takes in a parameter.
44+
-- This can be anything! You can even have optional arrays.
45+
helloWorldString = ByteNet.optional(ByteNet.string)
46+
47+
-- This works!
48+
eachCharacter = ByteNet.optional(ByteNet.array(ByteNet.string))
49+
}),
50+
})
51+
}
52+
end)
3653
```
3754
You really don't have to think about using optional types. You just send it!
3855
```lua title="server.luau"
@@ -41,7 +58,7 @@ local packets = require(path.to.packets)
4158
local randomlyStringOrNil =
4259
if math.random(1, 2) == 1 then "Hello, world!" else nil
4360

44-
packets.myCoolPacket:sendToAll({
61+
packets.myCoolPacket.sendToAll({
4562
helloWorldString = randomlyAppearingString,
4663

4764
-- Note that even if we don't put the "eachCharacter" field here,
@@ -57,22 +74,22 @@ Arrays are fairly self explanatory. They're just plain old arrays. However, it's
5774
There is a 2 byte overhead to sending an array in ByteNet. This is because these 2 bytes are an unsigned 16-bit integer, which stores the array length. As a side effect, arrays sent through ByteNet have a max length of **2^16**, which is equal to **65,536**. It's likely that in the future, you will be able to reduce the overhead to 1 byte through configuration, in turn reducing the max length of the array.
5875

5976
```lua title="packets.luau"
60-
return {
61-
myCoolPacket = ByteNet.definePacket({
62-
structure = {
63-
myArray = ByteNet.array(ByteNet.bool)
64-
},
65-
})
77+
return ByteNet.defineNamespace("example", function()
78+
return {
79+
myCoolPacket = ByteNet.definePacket({
80+
value = ByteNet.array(ByteNet.bool),
81+
})
82+
}
6683
}
6784
```
6885

6986
```lua title="server.luau"
7087
local packets = require(path.to.packets)
7188

72-
packets.myCoolPacket:sendToAll({
89+
packets.myCoolPacket.sendToAll({
7390
-- Important to note that mixed arrays/arrays with holes
7491
-- shouldn't be sent through.
75-
myArray = { true, false, true }
92+
true, false, true
7693
})
7794
```
7895

@@ -84,25 +101,23 @@ Maps are by far the most powerful "special" type in ByteNet. They let you send,
84101
Like arrays, maps have a 2-byte overhead to store the length of the map. This is done by iterating over the map using <a href="https://devforum.roblox.com/t/luau-recap-may-2022/1818869">generic iteration</a> and increasing a variable by 1 for every key-value pair. This, once again, means that there is a **2^16** (which equals to **65,536**) cap to the number of elements in the map.
85102

86103
```lua title="packets.luau"
87-
return {
88-
myCoolPacket = ByteNet.definePacket({
89-
structure = {
104+
return ByteNet.defineNamespace("example", function()
105+
return {
106+
people = ByteNet.definePacket({
90107
-- [name] = age
91-
people = ByteNet.map(ByteNet.string, ByteNet.uint16)
92-
},
93-
})
94-
}
108+
value = ByteNet.map(ByteNet.string, ByteNet.uint8)
109+
})
110+
}
111+
end)
95112
```
96113

97114
```lua title="server.luau"
98115
local packets = require(path.to.packets)
99116

100-
packets.myCoolPacket:sendToAll({
101-
people = {
102-
john = 21,
103-
jane = 24,
104-
dave = 26,
105-
you = 162,
106-
}
117+
packets.myCoolPacket.sendToAll({
118+
john = 21,
119+
jane = 24,
120+
dave = 26,
121+
you = 162,
107122
})
108123
```
File renamed without changes.

docs/api/functions/definePacket.md

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,20 @@ ByteNet's purpose as a library is to provide a way to structure your data, and s
1212
---
1313

1414
## Okay, where do I start?
15-
I highly recommended storing all of your packets in a single, shared module, and then using a dictionary to access the individual packets. Not only does this make using ByteNet a breeze, it also gives you a better form of typechecking.
16-
17-
The keys of your packet determine the ID it gets. The server and the client sharing this key is essential: that's the core of how ByteNet works. Unfortunately, this means you cannot have duplicate contents of packets. **This may change in the future.**
18-
19-
Enough of how it works, let's start off with making a basic packet:
15+
Enough of how it works, let's start off with making a basic packet. We will also need to create a namespace:
2016
```lua title="packets.luau"
2117
local ByteNet = require(path.to.bytenet)
2218

23-
return {
24-
printSomething = ByteNet.definePacket({
25-
-- This structure field is very important!
26-
structure = {
27-
message = ByteNet.string,
28-
}
29-
})
30-
}
19+
return ByteNet.defineNamespace("messaging", function()
20+
return {
21+
printSomething = ByteNet.definePacket({
22+
-- This value field is very important!
23+
value = ByteNet.struct({
24+
message = ByteNet.string,
25+
})
26+
})
27+
}
28+
end)
3129
```
3230

3331
---
@@ -44,32 +42,32 @@ On the client, there is only one, because you can only send data to the server:
4442

4543
- `send`
4644

47-
These functions *must* obey your structure created in `definePacket`, and if you have strict typing on, an error will be raised if the types do not match.
45+
Any data passed through these functions *must* obey your structure created in `definePacket`, and if you have strict typing on, an error will be raised if the types do not match.
4846

4947
*code examples*
5048
```lua title="client.luau"
5149
-- Sending to server
52-
packets.myPacket:send({
50+
packets.myPacket.send({
5351
message = "Hello, world!"
5452
})
5553
```
5654
```lua title="server.luau"
5755
-- Sending to all players
58-
packets.myPacket:sendToAll({
56+
packets.myPacket.sendToAll({
5957
message = "Hello, players!"
6058
})
6159

6260
-- Sending to an individual player
6361
local someArbitraryPlayer = Players.You
6462

65-
packets.myPacket:sendTo({
63+
packets.myPacket.sendTo({
6664
message = "Hello, random player!"
6765
}, someArbitraryPlayer)
6866

6967
-- Sending to all except a certain player
7068
local someArbitraryPlayer = Players.You
7169

72-
packets.myPacket:sendToAllExcept({
70+
packets.myPacket.sendToAllExcept({
7371
message = "Hello, everyone except one person!"
7472
})
7573
```
@@ -80,12 +78,12 @@ You can use the `listen` method to listen for when a packet is received.
8078

8179
*code examples*
8280
```lua title="server.luau"
83-
packets.myPacket:listen(function(data, player)
81+
packets.myPacket.listen(function(data, player)
8482
print(`{player.UserId} says { data.message }`)
8583
end)
8684
```
8785
```lua title="client.luau"
88-
packets.myPacket:listen(function(data)
86+
packets.myPacket.listen(function(data)
8987
print(`server says { data.message }`)
9088
end)
9189
```

docs/assets/colors.css

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)