20.06.2018, 18:43
(20.06.2018, 17:26)Patrix Wrote: Well, your code is pretty simple and readable, but this:If you write a structured script, it can be useful, but when it comes to oop, g2o packages are enough in this state and you really have to try to make a mistake(I think so). In any case, it can be useful for someone
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.

Squirrel Script
- enum packetList
- {
- option_lineChat,
- option_hud,
- option_password,
- option_notifications,
-
- id = 255
- };
This is a piece of code from my project. Enum is global (client / server) I have cut out a fragment for an example.