20.06.2018, 17:26
Well, your code is pretty simple and readable, but this:
Can cause problems because in this method, you are reading first packet value, I belive that you connect this method to onPacket event.
So, what if you have more similar functions/methods, and trying to read packet id in this way:
Even if this condition won't pass, you still read first value from packet, which was id.
Take a look at this code:
https://www97.zippyshare.com/v/LxAq6iiv/file.html
To simply avoid this problem, you must have ONLY ONE function connected to onPacket event on each side, then ONLY ONCE read packet Id, and check which function should be called based on it's value.
Even if you try to call two diffrent functions and pass them a packet object, it won't work, because this function will be working with an object reference, not it's copy.
In my opinion, this simple script makes it a little bit easier to add some new function(s) to call when packet with specific id arrive to it's location, but it's only my opinion, maybe i'm wrong
.
Squirrel Script
- function receivedPacket(pid, packet)
- {
- if(packet.readUInt8() == packetList.id)
- {
- switch(packet.readUInt8())
- {
- case packetList.option_lineChat: changeLine(pid, packet.readInt8()); break;
- case packetList.option_hud: changeHud(pid, packet.readInt8()); break;
- case packetList.option_password: changePassword(pid, packet.readString(), packet.readString()); break;
- case packetList.option_notifications: changeNotifications(pid, packet.readBool()); break;
- }
- }
- }
Can cause problems because in this method, you are reading first packet value, I belive that you connect this method to onPacket event.
So, what if you have more similar functions/methods, and trying to read packet id in this way:
Squirrel Script
- if(packet.readUInt8() == packetList.id)
Even if this condition won't pass, you still read first value from packet, which was id.
Take a look at this code:
https://www97.zippyshare.com/v/LxAq6iiv/file.html
To simply avoid this problem, you must have ONLY ONE function connected to onPacket event on each side, then ONLY ONCE read packet Id, and check which function should be called based on it's value.
Even if you try to call two diffrent functions and pass them a packet object, it won't work, because this function will be working with an object reference, not it's copy.
In my opinion, this simple script makes it a little bit easier to add some new function(s) to call when packet with specific id arrive to it's location, but it's only my opinion, maybe i'm wrong
