Jump to content




Call Functions with string


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

#1 Wilma456

  • Members
  • 187 posts
  • LocationGermany

Posted 15 June 2016 - 01:22 PM

Why I can call the function test with the string func and the args arg?
function test(text)
 print(text)
end

func = "test"
arg = "hello World"


#2 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 15 June 2016 - 02:24 PM

I'm not entirely sure what your question is, but here's my best guess at an answer.

function example(input)
  print(input)
end
example("this is a test") --#calling the function
word = "second test"
example(word) --#passing a variable


#3 Wilma456

  • Members
  • 187 posts
  • LocationGermany

Posted 15 June 2016 - 02:31 PM

I want to write
func(arg)
to call function test

#4 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 15 June 2016 - 04:31 PM

Umm... You could achieve that with a lot of metatable magic I think. Or, actually... You can't because the string metatable is "hidden".

You could try to do
getfenv()[func](arg)

Edited by H4X0RZ, 15 June 2016 - 04:31 PM.


#5 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 15 June 2016 - 05:02 PM

function test(text)
  print(text)
end

func = test
arg = "hello World"

You were setting func to the string "test". This sets func to equal test so you can call func(arg).

#6 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 15 June 2016 - 05:25 PM

View PostDog, on 15 June 2016 - 05:02 PM, said:

function test(text)
  print(text)
end

func = test
arg = "hello World"

You were setting func to the string "test". This sets func to equal test so you can call func(arg).

I believe the OP want's to call a function using a string, and not a pointer. Although your approach would "fix" the problem too.

#7 Bomb Bloke

    Hobbyist Coder

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

Posted 16 June 2016 - 03:19 AM

View PostH4X0RZ, on 15 June 2016 - 04:31 PM, said:

You could try to do
getfenv()[func](arg)

This seems correct to me. But I'd instead suggest:

local funcs = {}

funcs.test = function(text)
  print(text)
end

local func = "test"
local arg = "hello World"

funcs[func](arg)

... as dumping everything into the environment table (as globals) is messy.

http://lua-users.org...i/ScopeTutorial

#8 Wilma456

  • Members
  • 187 posts
  • LocationGermany

Posted 16 June 2016 - 11:52 AM

Thanks. But both methods work only with functions in the same script. if I load an API, it doesent work.
Method 1
Method 2
That does not work. How can I call function from another skript?

#9 valithor

  • Members
  • 1,053 posts

Posted 16 June 2016 - 01:03 PM

Both of the methods have a similar problem when trying to use an api

It would need to be getfenv()["testapi"]["test"](arg)

If you wanted to use an api with the first method you would probably want to do something like:
func = "testapi.test"
arg = "hello world"

local function callFunc(func,arg)
  local fenv = getfenv()
  for word in func:gmatch("[^ %.]+") do
	fenv = fenv[word]
  end
  return fenv(arg)
end

callFunc(func,arg)



For the second method you would need to use testapi.funcs[func](arg)

The problem you are experiencing is due to how variables/functions are loaded into apis.

Edited by valithor, 16 June 2016 - 01:15 PM.






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users