Requesting assistance coercing a string to a function call

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
captn_retardo
Prole
Posts: 2
Joined: Tue May 26, 2009 4:31 am

Requesting assistance coercing a string to a function call

Post by captn_retardo »

Hello all,
I am attempting to load a string from a file, and then coercing that string into the function name. For example, I would like to do something like:

Code: Select all

function test_func()
    print("yes!")
end

str = "test_func"

str()
So that end part, where I str() is obviously wrong, as a string is not a function. I am at a loss at how to do this. Does anyone have any tricks they can share?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Requesting assistance coercing a string to a function call

Post by Robin »

Try using loadstring:

Code: Select all

function test_func()
    print("yes!")
end

str = "test_func"

assert(loadstring(str))()
Disclaimer: I didn't test it.
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Requesting assistance coercing a string to a function call

Post by bartbes »

Try this:

Code: Select all

function test_func()
    print("yes!")
end

str = loadstring("test_func()") --notice the (), better syntax..

str()

--or..
str = "test_func()"
loadstring(str)() --however, this converts to a function every time
Robin beat me, but I hope this is going to help anyway

EDIT: though robin's suggestion of using assert is good..
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Requesting assistance coercing a string to a function call

Post by Robin »

We both were almost right. I think this is the way to go:

Code: Select all

function test_func()
    print("yes!")
end

str = assert(loadstring("test_func()"))

str()
Explanation:
"test_func()" is the string that gets executed.
loadstring creates a function (or was it called "chunk" or something like that?), containing the code from a string
assert gives an error if something went wrong, otherwise passes the function to str
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Requesting assistance coercing a string to a function call

Post by bartbes »

So.. what I said (if you include my edit).. :P

I kind of feel like saying "I was better", but I won't.. or will I?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Requesting assistance coercing a string to a function call

Post by Robin »

bartbes wrote:I kind of feel like saying "I was better", but I won't.. or will I?
I say we call it a draw :P
Help us help you: attach a .love.
captn_retardo
Prole
Posts: 2
Joined: Tue May 26, 2009 4:31 am

Re: Requesting assistance coercing a string to a function call

Post by captn_retardo »

Thank you so much for the quick response. Loadstring() is exactly what I was looking for.
User avatar
Jake
Prole
Posts: 27
Joined: Sun Jul 20, 2008 2:01 pm

Re: Requesting assistance coercing a string to a function call

Post by Jake »

Rather than loading the string as a piece of Lua you can just call the function from the global table

Code: Select all

local str = "test_func"
_G[str]()
I would prefer to do that because it seems safer to me.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Requesting assistance coercing a string to a function call

Post by bartbes »

And what if you want to do more than just calling a single functions without arguments?
User avatar
Jake
Prole
Posts: 27
Joined: Sun Jul 20, 2008 2:01 pm

Re: Requesting assistance coercing a string to a function call

Post by Jake »

Depends on exactly what but surely it's quicker just to do a table lookup, right?
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 3 guests