Getting a function into a table...

ugh…never mind guys I figured it out, the first function was passed after the table was instantiated, so it didn’t exist yet, thanks anyways  :rolleyes:

Guys I’m trying to pass a function as a param to another function. I’m using a table to hold all the information. Here is a very simplified example of what I’m doing the problem is that when I pass the function as a param it prints out as nil, but if I pass the function directly it works just fine. There’s no typos I’ve checked them 100 times, so what else could it be? I appreciate you help thank you!

–function to pass as reference

function doSomething( ) 

end

–function with params

function doThat( params )

   --create object up here etc

   object.collision = params.methodParam --doesnt work

   object.life = life

   --Here is where it gets weird both of these should print the same thing, but the latter prints nil, hence

   --the reason it doesnt work…why does it do that?

  print( doSomething ) --prints function reference

  print( params.methodParam ) --prints nil

end

–the params

playerParams = {

  methodParam = doSomething,

  life = 5

}

–call the function

doThat(playerParams)

You are passing nil to the function because you are setting playerParams only *after* your call to doThat(playerParams).
Try swapping their relative positions in the source code:

playerParams ={
MethodParam = doSomething
}

doThat(playerParams)

You are passing nil to the function because you are setting playerParams only *after* your call to doThat(playerParams).
Try swapping their relative positions in the source code:

playerParams ={
MethodParam = doSomething
}

doThat(playerParams)