Jump to content




Call A Function Depending On A Variable


  • You cannot reply to this topic
10 replies to this topic

#1 jay5476

  • Members
  • 289 posts

Posted 17 August 2013 - 05:40 AM

how do I call a function depending on a variable this is my code that does not work, how would I implement a way to run the function determined by func
function checkBox(x1,y1,x2,y2, func)
f = {fun=func}
e,b,x,y = os.pullEvent("mouse_click")
  for a = 1, y2-y1+1 do
 
    for b = 1, x2-x1+1 do
	  if a == x and b == y then
	  f[fun]()
	  end
    end
  end
end


#2 Sangar

  • Members
  • 30 posts

Posted 17 August 2013 - 05:53 AM

f[fun] means fun is interpreted as a variable in that context, i.e. Lua tries to look up a variable with that name, which doesn't exist, so it's the same as f[nil], which is, in turn, nil. What you actually want to do is f.fun, or the equivalent f["fun"].

Unless fun actually is a global variable you didn't show here, in which case you want to change the initialization of your table to f = {[fun] = func}.

#3 reububble

  • Members
  • 72 posts
  • LocationBehind you

Posted 17 August 2013 - 06:02 AM

Sounds like fun. ctions

#4 jay5476

  • Members
  • 289 posts

Posted 17 August 2013 - 07:41 AM

thanks guy i ended up sorting this out in the meantime but your replays are still appreciated

#5 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 17 August 2013 - 08:55 AM

Remove "f = {fun=func}" and change "f[fun]()" to "func()". No special code needed - you can pass functions around just like you can pass numbers and strings and tables around.

#6 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 17 August 2013 - 05:28 PM

View PostZagKalidor, on 17 August 2013 - 12:37 PM, said:

Sounds bolean to me
What in the [..]

anyway, passing Around variables, which is a boolean, string, number, table and functions, is never hard.

However if you talk about tables, you are possibly conflicting with pointers. It basically does not copy the table, as opposed to the other types of a variable. A pointer points to a place in the memory, so if you change the pointer you change the actual table.

And on a side not for the OP, you can declare table functions like so:
local tbl = {
  ['key'] = function( args )
      print( tostring(args))
  end;
}

local t = {}
function t.key( args )
  print( tostring(args))
end


#7 Jarle212

  • Members
  • 198 posts
  • LocationNorway

Posted 17 August 2013 - 05:41 PM

View PostSangar, on 17 August 2013 - 05:53 AM, said:

f[fun] means fun is interpreted as a variable in that context, i.e. Lua tries to look up a variable with that name, which doesn't exist, so it's the same as f[nil], which is, in turn, nil. What you actually want to do is f.fun, or the equivalent f["fun"].

Unless fun actually is a global variable you didn't show here, in which case you want to change the initialization of your table to f = {[fun] = func}.
You are wrong

this expression makes a table with fun in it. and fun is set as a pointer to func
f = {fun = func}

#8 Sangar

  • Members
  • 30 posts

Posted 17 August 2013 - 05:57 PM

View PostJarle212, on 17 August 2013 - 05:41 PM, said:

You are wrong

this expression makes a table with fun in it. and fun is set as a pointer to func
f = {fun = func}

I am most certainly not wrong ;) I am aware that OP's table initialization code does that, which is why I'm stating how to properly access it in the first paragraph of my post.

The second paragraph is just there for completion's sake: given a hypothetical global variable fun not mentioned in OP's code actually existed then f = {[fun] = func} ... f[fun] would work perfectly well, too - since the table entry's name would be the value of the variable fun.

Also: what the hell happened to this thread? o.O

#9 Jarle212

  • Members
  • 198 posts
  • LocationNorway

Posted 17 August 2013 - 06:29 PM

View PostSangar, on 17 August 2013 - 05:57 PM, said:

View PostJarle212, on 17 August 2013 - 05:41 PM, said:

You are wrong

this expression makes a table with fun in it. and fun is set as a pointer to func
f = {fun = func}

I am most certainly not wrong ;) I am aware that OP's table initialization code does that, which is why I'm stating how to properly access it in the first paragraph of my post.

The second paragraph is just there for completion's sake: given a hypothetical global variable fun not mentioned in OP's code actually existed then f = {[fun] = func} ... f[fun] would work perfectly well, too - since the table entry's name would be the value of the variable fun.

Also: what the hell happened to this thread? o.O

I am not sure what your point is, but if a global variable name fun existed it would not be accessable for f[fun]

I.e:
local fun = function() print("I am 'global' fun") end
local f = {fun = function() print("I am a child of f") end}

f.fun() --prints: I am a child of f
f["fun"]() --prints: I am a child of f
fun() --prints: I am 'global' fun

I do not what happened to the thread xD(random junk)

#10 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 17 August 2013 - 06:43 PM

I've cleaned up the thread some. Guys, please keep things related to the question on hand when you're in AaP. I'm not trying to be a dictator here, I just want don't want people to have to sift through random crap when they're looking for answers.

Thanks!

#11 Sangar

  • Members
  • 30 posts

Posted 17 August 2013 - 06:47 PM

Jarle212: Ah, I see where our misunderstanding lies, I think. I'm not saying fun is the actual function. It can be anything at all, just not nil. In which case, using your example:
local fun = {some = "variable", ["let's make it a table for"] = "fun"}
local f = {[fun] = function() print("blah") end}

f[fun]() -- prints: blah






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users