Log In  

Hi. I'm trying to learn about collisions using the DOM8VERSE section of zine 3 but I'm getting weird error messages whenever I try and use a foreach.

For example, when I try this:

function _update()
if btnp(5, 0) then
add(objects, bulletconstruct(player1.position.x,player1.position.y))
end

foreach(objects, function(obj)
obj.update(obj)
end

end

.. I get ')' expected ( to close '(' at line 0.0003891 near 'end'

I've tried adding and taking away brackets, as well as 'end' statements but I can't get it to work. Thought it was strange as I'm copy pasting zine 3 code.

Anyone know the solution?

P#19977 2016-04-23 17:12 ( Edited 2016-04-24 16:17)

You need a closing parenthesis after the "end" of the function used inside the foreach. And yes this is an error in the Picozine #3.

    foreach(objects, function(obj)
      obj.update(obj)
    end)

Explanation: foreach() is a built-in that takes an array as its first argument and a function as its second argument. In Lua, you can define a named function elsewhere then use the name of that function as the second argument, or you can do what this code does and define an anonymous function where the argument goes. The anonymous function is defined with "function(obj) ... end". Because this function is the second argument to foreach(), so there must be a closing parenthesis after the "end".

P#19982 2016-04-23 19:53 ( Edited 2016-04-23 23:53)

Thankyou!

P#19985 2016-04-24 12:17 ( Edited 2016-04-24 16:17)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 06:05:30 | 0.005s | Q:9