Hey there,
I’m trying to understand how Network IDs of entities work. I found with the help of @tellurium the functions NetworkGetNetworkIdFromEntity
and NetworkGetEntityFromNetworkId
and decided to test them.
My debugging process is simple. I have 2 players (Let’s call them Bob and Steve). On a debug client script, I have this :
if IsControlJustPressed(1, 168) then
local model = GetHashKey("prototipo")
RequestModel(model)
while not HasModelLoaded(model) do
Citizen.Wait(0)
end
local vehicle = CreateVehicle(model, x, y, z, h, true, true)
SetModelAsNoLongerNeeded(model)
SetVehicleOnGroundProperly(vehicle)
SetVehicleHasBeenOwnedByPlayer(GetPlayerPed(-1), true)
end
Now Bob can spawn a fancy X80 by using F7.
I also have this on F6 :
if IsControlJustPressed(1, 167) then
local ped = GetPlayerPed(-1)
if IsPedInAnyVehicle(ped, false) then
local vehicle = GetVehiclePedIsIn(GetPlayerPed(-1), true)
local netID = NetworkGetNetworkIdFromEntity(vehicle)
local entityFromNetID = NetworkGetEntityFromNetworkId(netID)
Citizen.Trace(vehicle .. " | " .. netID .. " | " .. entityFromNetID)
end
end
With those 2, Bob and Steve can now try figuring out NetworkIDs. So, first Bob spawns an X80, gets in it, and presses F6. Here’s the result for him :
3330 | 16711681 | 3330
Which is the expected result. Now if Bob leaves the car, Steve gets in, and presses F6, here’s the result :
1026 | 16711681 | 0
Now Steve’s local ID is different, which is also normal. Also, the NetID is also the same, and that’s good. The problem is, why is the third result not the same as the first ?
And if it’s an expected result, is there another way to get an entity ID from a netID ?
Let’s try another situation. Let’s say Bob spawns a vehicle, that vehicle’s netID is 16711681
, then spawns another one, that second vehicle’s netID is 16711682
. It increased by one, nothing unexpected. Now let’s make Steve spawn a vehicle; That third vehicle’s (spawned by Steve this time) netID is 16711681
and the first spawned vehicle by Bob disappeared. My question, is why ?
On the technical level, both players have the same netID which is good, but both players also have different counters for their NetIDs. This means that if Bob and Steve repeatedly press F7 one after the other 10 times, there would only be 10 vehicles instead of an expected 20.
EDIT
Let’s change the second boolean of CreateVehicle to false, and let’s try again. Now the disappearing issue is not a problem anymore, both vehicles stay. This also means that we now have 2 vehicles with the same NetID.The NetIDs are still acting weird tho. Now if Bob spawns 2 vehicles, here are the results :
Vehicle 1 : 11010 | 1 | 11010
Vehicle 2 : 11266 | 2 | 11266
Which is expected. Now if Steve spawns a vehicle and Bob gets in it, here’s the result :
12034 | 1 | 11010
This means that Bob gets the result of his first spawned vehicle, even tho he’s in Steve’s vehicle.
Here’s some more testing following @ejb1123 's answer concerning NetworkRequestControlOfNetworkId
.
So, to test this out, I’ve made another function :
if IsControlJustPressed(1, 166) then
local ped = GetPlayerPed(-1)
if IsPedInAnyVehicle(ped, atGetIn) then
local vehicle = GetVehiclePedIsIn(GetPlayerPed(-1), false)
local netId = NetworkGetNetworkIdFromEntity(vehicle)
if NetworkHasControlOfEntity(vehicle) then
Citizen.Trace("You have control of this ENTITY")
else
Citizen.Trace("You DON'T have control of this ENTITY")
end
if NetworkHasControlOfNetworkId(netId) then
Citizen.Trace("You have control of this NETID")
else
Citizen.Trace("You DON'T have control of this NETID")
Citizen.Trace("Requesting Control...")
NetworkRequestControlOfNetworkId(netId)
while not NetworkHasControlOfNetworkId(netId) do
Citizen.Wait(0)
end
if NetworkHasControlOfNetworkId(netId) then
Citizen.Trace("You now have control !")
else
Citizen.Trace("This should not come up, ever.")
end
end
end
end
Bob and Steve can now Request for control over a NetID. Now let’s say Bob spawns a vehicle, and Steve gets in it; If Steve presses F6, the result are as usual :
5634 | 0 | 0
Which I still don’t understand, (that’s the point lel) but it makes sense with the results from previous tests since Steve never spawned a vehicle, his counter is still at 0.
Now let’s make Steve press F5 and here are the results :
You have control of this ENTITY
You DON'T have control of this NETID
Requesting Control...
And it hangs. My guess is that it’s stuck in the while loop, as NetworkHasControlOfNetworkId
never returns true. Other inputs didn’t work, since the script crashed. From here, I’ve tried 2 things :
- First, I tried commenting that loop, and just seeing if Steve gets the control right away, and no surprise, he never gets it. I also waited to see if Steve gets the control at some point, didn’t work either. During the wait, I also checked frequently if the result from F6 changed, it never did.
- Second, I tried making Bob disconnect in that infinite loop, to see if Steve gets Control because Bob just doesn’t exist anymore; nope, still no control, still stuck in the loop.
Note : For Bob it says that Bob has control on both NetID and Entity
If anyone has an explanation for this, I’m open to any suggestion.
Thanks,
Exorion.