Jump to content




call function from string

lua

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

#1 Calimore

  • Members
  • 28 posts

Posted 26 May 2013 - 07: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]()

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 26 May 2013 - 07:46 AM

function foofunc ()
  print ("foo")
end

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

-- Or just:

loadstring("foofunc()")()


#3 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 26 May 2013 - 07:57 AM

 Bomb Bloke, on 26 May 2013 - 07:46 AM, said:

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]()


#4 Calimore

  • Members
  • 28 posts

Posted 26 May 2013 - 08:44 AM

Thank you both for the insight into how this could work... :)

I'll play around with this a little bit.

#5 Calimore

  • Members
  • 28 posts

Posted 26 May 2013 - 11:52 AM

 Engineer, on 26 May 2013 - 07:57 AM, said:

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

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


Unfortunately this does not work... Error is: '(' expected in line 2

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 26 May 2013 - 11:55 AM

 Calimore, on 26 May 2013 - 11:52 AM, said:

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


#7 Calimore

  • Members
  • 28 posts

Posted 26 May 2013 - 12:01 PM

 theoriginalbit, on 26 May 2013 - 11:55 AM, said:

 Calimore, on 26 May 2013 - 11:52 AM, said:

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:

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 26 May 2013 - 12:05 PM

 Calimore, on 26 May 2013 - 12:01 PM, said:

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


#9 Calimore

  • Members
  • 28 posts

Posted 26 May 2013 - 02:28 PM

 theoriginalbit, on 26 May 2013 - 12:05 PM, said:

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... ;)

#10 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 26 May 2013 - 02:45 PM

 Calimore, on 26 May 2013 - 02:28 PM, said:

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


#11 Calimore

  • Members
  • 28 posts

Posted 26 May 2013 - 03: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...?

#12 Hawk777

  • Members
  • 162 posts

Posted 28 May 2013 - 03: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”.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users