This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Calimore's profile picture

call function from string

Started by Calimore, 26 May 2013 - 06:11 AM
Calimore #1
Posted 26 May 2013 - 08:11 AM
How can I call a function from a string…?

I have found this example but this doesn't work for me… What's wrong?


function foofunc ()
  print ("foo")
end

x='foofunc'

_G[x]()

I'm getting "attempt to call nil" for _G[x]()
Bomb Bloke #2
Posted 26 May 2013 - 08:46 AM
function foofunc ()
  print ("foo")
end

x = loadstring("foofunc()")
x()

-- Or just:

loadstring("foofunc()")()
Engineer #3
Posted 26 May 2013 - 08:57 AM
function foofunc ()
  print ("foo")
end

x = loadstring("foofunc()")
x()

-- Or just:

loadstring("foofunc()")()
Im going to disagree on this one. Loadstring should be only used when it cant be done else.
I would suggest this, since you want to load if globally:

function _G['foobar']()
  print("foo")
end

_G["foobar"]()

You actually dont need to load it in the global table:

local functions = {}
function functions['foobar']()
   print("foobar")
end

local x = 'foobar'
functions[x]()
Calimore #4
Posted 26 May 2013 - 09:44 AM
Thank you both for the insight into how this could work… :)/>

I'll play around with this a little bit.
Calimore #5
Posted 26 May 2013 - 12:52 PM

local functions = {}
function functions['foobar']()
  print("foobar")
end

local x = 'foobar'
functions[x]()

Unfortunately this does not work… Error is: '(' expected in line 2
theoriginalbit #6
Posted 26 May 2013 - 12:55 PM
Unfortunately this does not work… Error is: '(' expected in line 2
Yeh you need to do one of these two

functions['foobar'] = function()
  print("foobar")
end
or this

function functions.foobar()
  print("foobar")
end
Calimore #7
Posted 26 May 2013 - 01:01 PM
Unfortunately this does not work… Error is: '(' expected in line 2
Yeh you need to do one of these two

functions['foobar'] = function()
  print("foobar")
end
or this

function functions.foobar()
  print("foobar")
end

Ahh… much better! This is it…

Thanks a lot :lol:/>
theoriginalbit #8
Posted 26 May 2013 - 01:05 PM
Ahh… much better! This is it…

Thanks a lot :lol:/>
No problems. Oh I should mention if you want a key with a space in it, you will need to use the first method.

This is ok

functions["some name"] = function()
  print("foo")
end

This is not ok

function functions.other name()
  print("bar")
end
Calimore #9
Posted 26 May 2013 - 03:28 PM
Oh I should mention if you want a key with a space in it, you will need to use the first method.

I suspect the same goes for numbers. I was having problems with that… ;)/>
Engineer #10
Posted 26 May 2013 - 03:45 PM
I suspect the same goes for numbers. I was having problems with that… ;)/>

Yes, but for numbers you dont use quotes:

local t = {}
local t[1] = function()
   print("I'm index 1!")
end
Calimore #11
Posted 26 May 2013 - 04:12 PM
Thanks again… Much appreciated!

Most of what i'm doing is trying to figure out how LUA works. What I try to do is provide myself with some sort of unified computer-to-computer messaging protocol.
All messages I transmit should share a common header and a variable body. The message header contains basic stuff as senderID, receipientID as well as a messageID which I then use to call a function that deals with it's actual message body itself.

By using this way of function calling I can introduce new functions into the code without having to touch any other line of code. Do you think this is overly complicated…? I simply didn't want to do a giant "ifelse" statement… Are there any drawbacks doing it this way…?
Hawk777 #12
Posted 28 May 2013 - 04:08 AM
If you really, really want to access a function defined in the current file by name with no special syntax used at point of definition, what about this?


function myfunc()
  print("Hello World")
end

fname = "myfunc"

getfenv()[fname]()

The above snippet outputs the string “Hello World”.