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

Checking if a string is a function variable

Started by mhiekuru, 12 February 2016 - 01:41 PM
mhiekuru #1
Posted 12 February 2016 - 02:41 PM
Is there a way I can check if a string is the same name as an existing function?

I'm using this line of code to execute a string stored in the object[id].type

assert( loadstring( 'return ' .. object[ id ].type .. '.print' .. '( ... )' ) )()

But sometimes there are no function with the same name as the string in the object[id].type and the code wont works because its calling a function that doesn't exist

So I'm trying to check like this if the string is a function but it appears to only return it as string type and not the one I wanted to happen

if type(object[id].config.type.. '.print') == 'function' then
  assert( loadstring( 'return ' .. object[ id ].type .. '.print' .. '( ... )' ) )()
end

So is there any way I can check if a string is also a function with the same name? or any other fix I can apply in here?

P.S.
I'm using this code as a module and whenever I use _G[]() it says I'm calling a nil so I'm forced on using loadstring() and assert()

module( ..., package.seeall )
Edited on 12 February 2016 - 01:42 PM
eniallator #2
Posted 12 February 2016 - 04:28 PM
You can just have the functions in a table and then call table[string] and it will work
KingofGamesYami #3
Posted 12 February 2016 - 04:41 PM
I really have no idea what you're doing here, perhaps you could elaborate on what you're trying to accomplish. I've never heard of module or package, are they in Lua 5.2 or something? CC uses Lua 5.1, although it's updating to 5.2 in the later versions. I'm not certain if 5.2 functionality is 100% there yet or not.
Creator #4
Posted 12 February 2016 - 05:27 PM
package and module are part of standard Lua.
Lignum #5
Posted 12 February 2016 - 05:41 PM
This should do what you want:

if type(object[id].type.print) == "function" then
   object[id].type.print(...)
end
No loadstring needed, since Lua is dynamically typed.
HPWebcamAble #6
Posted 12 February 2016 - 10:34 PM
P.S.
I'm using this code as a module and whenever I use _G[]() it says I'm calling a nil so I'm forced on using loadstring() and assert()

module( ..., package.seeall )

According to this page, module was used in Lua 5.0 and 5.1
In any case, it certainly isn't used now EDIT: In ComputerCraft

As for your main issue, if Lignum's suggestion wasn't what you are looking for, you'll have to give us more context. Like the rest of your code.
There may be a cleaner way to do it.
Edited on 13 February 2016 - 03:44 PM
MKlegoman357 #7
Posted 13 February 2016 - 03:28 PM
P.S.
I'm using this code as a module and whenever I use _G[]() it says I'm calling a nil so I'm forced on using loadstring() and assert()

module( ..., package.seeall )
According to this page, module was used in Lua 5.0 and 5.1
In any case, it certainly isn't used now.

Lua still has modules. It's CC that doesn't support Lua's packages and modules.