I want my game to be very flexible, so no item will have fixed values. For example, a gun can work as if it were a sword. I wanted to do this by making a list of functions with each behavior
function crowbar()
...
end
function gun()
...
end
And then, I'd instantiate the items like this
item[1].func = gun()
So every frame I could run the item[1].func() so it would act like the behaviour of the function. But I'm not finding any way of doing this, all I saw were examples where you declared the function inside the table, not outside. Is there no other way?
[SOLVED] Attributing global function to table
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
[SOLVED] Attributing global function to table
Last edited by pauls313 on Sat Dec 09, 2017 10:08 pm, edited 1 time in total.
Re: Attributing global function to table
Your example would work as intended if you assigned the actual function instead of the result of the function call to item[1].func
Your also might want to look into a design pattern called Entity-Component-System.
Code: Select all
item[1].func = gun -- no parentheses
Re: Attributing global function to table
Something like this?
Code: Select all
function crowbar()
local item = {
damage = 10,
swingSpeed = .4
}
function item:use()
-- do whatever
end
return item
end
-- then assign and use it
item[1] = crowbar()
item[1]:use()
Re: Attributing global function to table
That's perfect! Thanks.grump wrote: ↑Sat Dec 09, 2017 1:58 am Your example would work as intended if you assigned the actual function instead of the result of the function call to item[1].funcYour also might want to look into a design pattern called Entity-Component-System.Code: Select all
item[1].func = gun -- no parentheses
Who is online
Users browsing this forum: No registered users and 120 guests