Forum

> > CS2D > Scripts > Calling a function
Forums overviewCS2D overview Scripts overviewLog in to reply

English Calling a function

3 replies
To the start Previous 1 Next To the start

old Calling a function

Powermonger
User Off Offline

Quote
Hi everyone,

I have a question about how to call function from table, similar with this one Function in table.

I want to have a table array that calls the function. Function can't exist in table, because I want it to be called from other sources.

Not like this
1
2
table = {
[1] = {name = "Drop",func = TMMdrop(id) *do stuff here* end}}


But more like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function TMMdrop(id)
-- stuff
end
function TMMtake(id)
-- stuff
end
function TMMequip(id)
-- stuff
end

function TMmenu(id,t,b)
local data = {
[1] = {name = "Drop",func = TMMdrop(id)},
[2] = {name = "Take",func = TMMtake(id)},
[3] = {name = "Equip",func = TMMequip(id)}
}
for i = 1,#data do
if type(data[i]) == "table" and t == data[i].name then
	data[i].func(id)
	break
end
end
end

I hope that you understand what I am looking for and please help me to get there. Thank you very much

old Re: Calling a function

VADemon
User Off Offline

Quote
It works, if written correctly. There's a difference between defining a function normally and inside a table.
1
2
3
4
5
6
7
8
9
10
local function hello(n) -- a local function will become unavailable in global space after the whole .lua file is loaded and thus it's good for One Time Initialization
	print("Hello: ".. n * 2)
end

powermonger = {
	[1] = {name = "Foo", func = hello}, -- function defined here
	[2] = {name = "Bar", ["func"] = hello}, -- just the same
	[3] = {name = "FizzBuzz"}
}
powermonger[3].func = hello -- same outside the table

Your only mistake: hello(n) will execute the function IMMEDIATELY. Since the function does not return any value, NIL will be saved to func.
hello by itself only refers to the function as an object/variable.

For example:
1
2
3
4
5
6
7
function returner(n)
	print("Returning: ".. n * 2 + 1)
	return (n*2+1)
end

print(type( returner ))
print(type( returner(123) ))
edited 1×, last 03.03.16 05:48:49 pm

old Re: Calling a function

GeoB99
Moderator Off Offline

Quote
To call a function and its reference inside an array you'd normally just assign them within a local similarly to this:
1
local a = {myfunc = function(a,b,c) return a,b,c end}
In your case it should be like this.
1
2
3
local a = {TMMdrop = function(id) end}
local b = {TMMtake = function(id) end}
local c = {TMMequip = function(id) end}
Note that the local value must be assigned with your function name (a.TMMdrop for example) depending on your wish and needs. Hope that helps.

old Re: Calling a function

Powermonger
User Off Offline

Quote
Thanks for your fast answers!

I ended up using a mix of my and VADemon's script.
The function must be global so I edited that.

And I just realised something... the example that I wrote also works in other way, with one edit:
1
2
3
[1] = {name = "Drop",func = TMMdrop},  -- took (id) away
[2] = {name = "Take",func = TMMtake},
[3] = {name = "Equip",func = TMMequip}
It allows function calls in global tables.

Quote
You learn something new everyday.
- Ray LeBlond


Thank you guys very much for your help. I really appreciate it.

Have a nice week everyone!
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview