Is there a way to evaluate a function call stored as a string, say "cls(2)"? I tried
eval("cls(2)")
but nope; no such beast.
no - and unlikely to happen as it would "break" the token limit.
I've developed a little system that does something kind of like this, in that it can execute a list of function calls via instructions encoded into a string. I was going for an efficient way of drawing an arbitrary number of shapes on screen, but it seems to work with all types of functions. Here's a little demo where it clears the screen and draws a few shapes.
s="````ナ██らきそきそっらめhxxらア"
function _update()
local t,f={},{pset,line,oval,ovalfill,rect,rectfill,spr,cls}
for i=0,#s-1 do
t[i%5+1]=ord(s,i+1)-96
if(i%5>3) f[t[5]\16](unpack(t))
end
end |
@JadeLombax I'd never heard of pack/unpack until today.
I was in fact looking for a way to script game actions (or function invokations) in a format similar to that of an animation exposure sheet. It makes things easier (for me) to tweak once foundation logic is in place.
Barring ability to evaluate strings as code, I ended up doing this. I have a feeling that pack/unpack will come in handy in no time at all. :)
Thanks!
function _init()
t=0
after(30,set_draw,drw_1)
after(60,set_draw,drw_2)
after(90,set_draw,drw_3)
end
function _update()
t+=1
upd_cq(t)
end
function _draw()
cls()
print(t,8,8,7)
end
function drw_1()
cls(1)
print(t,8,8,7)
end
function drw_2()
cls(2)
print(t,8,8,7)
end
function drw_3()
cls(3)
print(t,8,8,7)
end
-- command queue
cq={}
function after(tix, cmd, arg)
add(cq, {tix=tix, cmd=cmd, arg=arg})
end
function upd_cq(t)
for c in all(cq) do
if c.tix==t then
c.cmd(c.arg)
del(cq,c)
end
end
end
-- commands
function set_draw(f)
_draw=f
end
|
Glad I could offer something helpful.
I guess I'm not totally understanding your system. Maybe there's some more complex stuff it could do, but if you're interested I came up with a very small version that does the same kind of thing as the current code, just using table values to define the different states instead of separate functions.
t=0
tbl={1,2,3,8,9,5}
function _update()
t+=1
if(t<=150) col=tbl[t\30+1]
cls(col)
?t,8,8,7
end |
That was purely for demonstration purposes. The idea (for my needs) is to list the steps of a command sequence in as succinct a manner as possible. And yes, if I only wanted to change the color for cls at regular intervals, my code sample would represent one very convoluted way of doing that. :)
This is pretty easy if you already have a way of splitting the function name & arguments.
local functionTable = funcT{} --This table contains all of the functions you're planning on using.
local functionTable = math.sin --You can add any default function to it
funcT.f = function(x) --example function
print("hello " .. x)
end
local functionString = "f" --this string contains whatever function you want to call
local argumentString = "world" --this string contains the argument to that function. Doesn't need to be a string
(funcT[functionString])(argumentString) --This is the syntax for calling a function from a table.
--You can also do this with global functions:
function a()
print("hello world")
end
(_G["a"])()
|
I think this is the closest to what you want. Of course, you'd still need a way of splitting the function and its arguments into a separate string.
[Please log in to post a comment]




