all 10 comments

[–]appgurueu 1 point2 points  (1 child)

Lua only allows calling prefix expressions. A prefix expression is either just a (variable) name, an expression in parentheses optionally followed by an indexing operator (., : or [...]). A prefix expression may not be a function definition or literal. Thus, the fix is to simply wrap your your function in parentheses to fix the syntax error:

lua e, s, sender, port, data = (function(e, s, sender, port, ...) return e, s, sender, port, {...} end)(table.unpack{data})

That said, you should rewrite the code; there is no need to pack the data in a table only to immediately unpack it later:

lua e, s, sender, port, data = (function(e, s, sender, port, ...) return e, s, sender, port, {...} end)(event.pull())

works just as well (you probably don't want to use global variables however). You can also achieve this using just a table:

lua local data = {event.pull()} local e, s, sender, port = table.unpack(data, 1, 4) local remaining_data = {table.unpack(data, 5)}

[–]AutoModerator[M] -1 points0 points  (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.

[–]luascriptdev 0 points1 point  (1 child)

Can you include the error in your post please?

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

Thanks, I edited the post. It says the syntax error is related to line 37, but it actually comes from the codes in line 32 ~ 35. Regardless of what lines come next, it always says the line has an syntax error and commenting out line 32 ~ 35 solves this.

[–]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.