Event listener question

Hi, I’m working on my first project atm and am getting stuck (have been on this issue for a while)

I’ve tried originally using buttons and onEvent contained within them to do this, but i couldn’t get that to work. Now I’ve tried using event listeners but im stuck here as well.

my code is as follows

 hand[i]:addEventListener( "touch", {function selecting( event ) selected = i end} )

hand is a table (a hand of cards for a player) and this is in a for i,v in pairs statement hence the use of ‘i’. What I’m trying to do is record which card is being ‘selected’ or touched in order to use in other parts of the code. however i cant seem to work out the syntax to do this.

previously I had 

 hand[i]:addEventListener( "touch", selected = i )

however it was telling me a ) was missing near  = which im guessing it expected something like ( “touch”, selected)

any help would be appreciated, thanks!

The second parameter to addEventListener needs to be the address to a function.

local function doSomething()   -- end hand[i]:addEventListener( "touch", doSomething )

Note, you use the function name. You don’t call the function.  doSomething() with parans. That executes the function. The function name without the () passes the address of the function.

You should not use anonymous functions here either. You can’t remove them later.

Rob

Could you please tell me the difference between passing the adress of the function and executing it please? I’ve been wondering for a while how functions are called in lua (i.e after they are done does the code return to where the function was called from or does it continue off the end of the function)

Thanks!

Let’s first define a function:

local function doSomething()       return "Hello World" end

Passing the address of a function:

local someVariable = doSomething   --- no ()

The variable someVariable will now hold the address to the function doSomething.

local someVariable = doSomething()  -- with ()

Since doSomething() returns a value (the string “Hello World”), the value of someVariable will be the string “Hello World”.

Rob

thanks, so the first ( no () ) directs it to the function where it is in the code and the second ( with () ) executes it from where it is called?

I’m wanting to know this because if i call a function from another lua file im wondering if it will execute that function and then run through that whole lua file or just execute that function and return to where it was called from.

no () gives you the address of the function in memory

() causes the function to run and you get the results of the function run.

Rob

sorry im still not quite sure what that means, is there any simpler or ‘for dummies’ explanation?

If you call a function from another lua file and you put () on the end or (param1, param2, etc.) to pass parameters to the function, it will run that one function only.

If you use just the name of the function with out any parameters or parens, you get the memory address to where the code for that function lives.

 Now back to your module/other .lua file question. If you have any code in the .lua file that is not inside a function, that code will execute when you “require” the module.

Ok thank you so much!

The second parameter to addEventListener needs to be the address to a function.

local function doSomething()   -- end hand[i]:addEventListener( "touch", doSomething )

Note, you use the function name. You don’t call the function.  doSomething() with parans. That executes the function. The function name without the () passes the address of the function.

You should not use anonymous functions here either. You can’t remove them later.

Rob

Could you please tell me the difference between passing the adress of the function and executing it please? I’ve been wondering for a while how functions are called in lua (i.e after they are done does the code return to where the function was called from or does it continue off the end of the function)

Thanks!

Let’s first define a function:

local function doSomething()       return "Hello World" end

Passing the address of a function:

local someVariable = doSomething   --- no ()

The variable someVariable will now hold the address to the function doSomething.

local someVariable = doSomething()  -- with ()

Since doSomething() returns a value (the string “Hello World”), the value of someVariable will be the string “Hello World”.

Rob

thanks, so the first ( no () ) directs it to the function where it is in the code and the second ( with () ) executes it from where it is called?

I’m wanting to know this because if i call a function from another lua file im wondering if it will execute that function and then run through that whole lua file or just execute that function and return to where it was called from.

no () gives you the address of the function in memory

() causes the function to run and you get the results of the function run.

Rob

sorry im still not quite sure what that means, is there any simpler or ‘for dummies’ explanation?

If you call a function from another lua file and you put () on the end or (param1, param2, etc.) to pass parameters to the function, it will run that one function only.

If you use just the name of the function with out any parameters or parens, you get the memory address to where the code for that function lives.

 Now back to your module/other .lua file question. If you have any code in the .lua file that is not inside a function, that code will execute when you “require” the module.

Ok thank you so much!