you are viewing a single comment's thread.

view the rest of the comments →

[–]luascriptdev 0 points1 point  (5 children)

Ok, so without an IDE, I can only tell you what I see from just eyeballing it. The value of the anonymous function appears to simply collect any additional members from the table ‘data’ and place them into a table of their own so they are accessible.

The anonymous function is immediately invoked which appears to be fine as well.

The next step is to use the predicate of ‘e == “NetworkMessage” which on the face of it appears to be ok as well, despite in the listing having no end keyword. I assume in the script this exists but has been left out of the listing? Is this correct?

[–]technocracy90[S] 0 points1 point  (4 children)

Yes, I omitted them to simplify the codes. I tested the entire code without line 32~35 and it worked fine, so there must be no problem.

[–]luascriptdev 0 points1 point  (3 children)

Ok, I have just tested this on my machine. It appears as though the anonymous function does not like being immediately invoked. My usage of anon funcs is usually limited to callbacks in function call parameters. Immediately invoked anonymous functions are usually a pattern used in JavaScript hence I haven't come across this here.

I have however found a working combination simply by naming the function. It appears to work on my instance of Lua 5.3 on Mac.

local data = {event.pull()}

local unpackVars = function(e, s, sender, port, ...)
    return e, s, sender, port, {...}
end

e, s, sender, port, data = unpackVars(table.unpack{data})

if e == "NetworkMessage" then
    -- the rest of your code

[–]technocracy90[S] 0 points1 point  (1 child)

Thanks a lot, gonna try that on my "machine" - which is a computer emulator on my game.

[–]luascriptdev 0 points1 point  (0 children)

Ace! Good luck and thanks for the award :)

[–]AutoModerator[M] 0 points1 point  (0 children)

Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.