127 posts
Posted 06 October 2012 - 10:34 AM
well i kinda know what to do here ive got it set out like this
func = {print("hello");print("bob")}
and i know i can call a function in it like
func = {function() print("hello");print("bob")} end}
what i want to know is how could i make it so that it automatically turns whats in the func
(so the whole range) into a useable code
521 posts
Location
Stockholm, Sweden
Posted 06 October 2012 - 10:38 AM
I don't think it's possible, without like making a temporary file and put in the table there, then load the file with os.loadAPI()
127 posts
Posted 06 October 2012 - 10:40 AM
well i dont see how if i can use
func = {function() print("hello");print("bob")} end}
i dont see why i wouldnt be able to do something like concatenate them together
1111 posts
Location
Portland OR
Posted 06 October 2012 - 11:32 AM
Try using this type of format
local func = {}
func[1] = function()
print("hello")
print("bob")
end
func[2] = function()
--put something else here
end
for x=1, #func do
func[x]()
end
127 posts
Posted 06 October 2012 - 11:35 AM
I thought of that but the problem is then because im using it as an API it would be very user unfriendly, i know i can do it in a single line thing but having to add the function() and the end annoys me
1111 posts
Location
Portland OR
Posted 06 October 2012 - 11:37 AM
Instead of adding the function() portion to the table try using loadstring() instead. I've never used it so I'm not sure how it works but from what I know it might work for what you're wanting to achieve.
127 posts
Posted 06 October 2012 - 11:39 AM
it wouldnt work as the name suggests its only works for strings and mine arent actually stored as strings, i just thought there might be a way to call the function somehow
127 posts
Posted 06 October 2012 - 11:41 AM
something like {function() func[1]() end} maybe ?
1111 posts
Location
Portland OR
Posted 06 October 2012 - 11:42 AM
you can always turn them into a string here's how i think it would work. I'll go try it.
func ={"print("hello")}
loadstring(tostring(func[1]))
EDIT: Here try this
func ={"print('Hello ') print('Bob')"}
local run = loadstring(tostring(func[1]))
run()
Once you add more just use a for loop to execute
for x=1, #func do
local run = loadstring(tostring(func[x]))
run()
end
Edited on 06 October 2012 - 09:50 AM
127 posts
Posted 06 October 2012 - 11:58 AM
Nope tryed and didnt work
ill try the edit now
127 posts
Posted 06 October 2012 - 12:11 PM
ahh see i dont wanna have to edit the functions themselves, i just want to run them without change
196 posts
Location
Norway
Posted 06 October 2012 - 04:44 PM
Use:
func ={function() print("Hello")end, function() print("Bob") end}
for i=1,#func do
func[i]();
end
1604 posts
Posted 06 October 2012 - 06:12 PM
Ok, first of all, what are you trying to do? You're probably going the wrong way, but to help you we need to know what you want to do.
1688 posts
Location
'MURICA
Posted 06 October 2012 - 07:15 PM
I believe he's trying to turn
function foo()
print 'foo'
end
function bar()
print 'bar'
end
into
function foobar()
print 'foo'
print 'bar'
end
I can see this being useful in a lot of situations, and I think I might know of a way to do so.
function group(functs)
local tfunct = {parts = {}}
for i,v in pairs(functs) do
table.insert(tfunct.parts, v)
end
local mt = {__call = function()
for i,v in pairs(tfunct.parts) do
v()
end
end}
return setmetatable(tfunct, mt)
end
local function foo() print 'foo' end
local function bar() print 'bar' end
local foobar = group{foo, bar}
foobar()
--> foo
--> bar
This bit of code defines a grouping function that throws all of the functions in a table given into another table, and with some metatables magic, is callable, where it will call all of it's parts in the order given.