-
-
Notifications
You must be signed in to change notification settings - Fork 60
Entity IDs to be u_int32 instead of 12-character strings. #242
Description
@rpc("any_peer", "reliable", "call_remote")
func _request_projectile(id: String, tick: int, request_data: Dictionary):
var sender = multiplayer.get_remote_sender_id()
...
_save_projectile(projectile, id, local_data)
_accept_projectile.rpc(id, tick, local_data)
_after_fire(projectile)func _save_projectile(projectile: Node, id: String, data: Dictionary = {}):
_projectiles[id] = projectile
projectile.name += " " + id
projectile.set_multiplayer_authority(get_multiplayer_authority())
if data.is_empty():
data = _get_data(projectile)
_projectile_data[id] = dataSending a string of 12 characters on an RPC as ID, instead of an integer, costs a lot of bandwidth (and some slight CPU)
func _generate_id(length: int = 12, charset: String = "abcdefghijklmnopqrstuvwxyz0123456789") -> String:
var result = ""
for i in range(length):
var idx = randi_range(0, charset.length() - 1)
result += charset[idx]
return resultI suggest solving this with the following way:
Preset Network IDs
An unsigned integer (4 bytes) has range[0, 4294967296]
But let's simplify it, let's say there is [0, 5000]
Say there are 5 players max in the game. If you split the above range in 5 parts, each player occupies the ranges
Player1: [0-1000]
Player2: [1001-2000]
Player3: [2001-3000]
Player4: [3001-4000]
Player5: [4001-5000]
If Player2 spawns a projectile, that projectile's id is 1001. So, locally it is 1001. But the server knows at that exact same moment without even having received player2's new projectile, that whatever next projectile is of player2 at any future tick, it will have id 1001. Ofc, the next projectile of player2 will be 1002, and the next after it 1003.
So just by the ID, it is clear who spawned it, and ofc its very cheap on memory and bandwidth.
Now, since we are using an integer, we are not capped at range[0, 5000] (1000 projectiles/IDs per player), but more like 100000 so that's more than enough.