Calling method from string

Hi.

I Saw it is possible to call a function contained into a string with loadstring

local instruction = 'add(5, 6)'
loadstring(instruction)

But I need to do th samething with a method of a class

local instruction = 'add(5, 6)'
instruction = 'self:' .. instruction
loadstring(instruction)

Is it possible to do that with LUA ?

Just curious as to what your use case is and why you want to do this?

In an editor I put some intruction like

Translate(10, 12)

when I animated the object in Solar2D I would like to call

object:Translate(10, 12)
Actualy, this is working

self["Translate"](self, 10, 12)

but I do not have any information about called function name and the arguments.
For the moment the best thing I can do is parse

Translate(10, 12)

in order to find

fct = Translate
arg1 = 10
arg2 = 12

to be able to call

self[fct ](self, arg1, arg2)

But I don not understand how works the regex parser of Lua for the moment !

local instruction = 'Translate(10, 12)'

-- First I remove space
instruction = instruction:gsub("%s+", "")
-- Function parsing (Up to 4 arguments)
local fct, arg1, arg2, arg3, arg4 = string.match(instruction, "^(%a+)%((-?%d*),?(-?%d*),?(-?%d*),?(-?%d*).*%)")
-- Method call
self["__T"](self,  arg1, arg2, arg3, arg4)

4 hours for that code :sob:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.