How would I create spawnable objects in love2d without using OOP?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
How would I create spawnable objects in love2d without using OOP?
I'm having trouble understanding metatables and created multiple instances of the same object with them. For example spawning an enemy or collectables like coins. Is there an easier method of doing so in lua?
- BrotSagtMist
- Party member
- Posts: 664
- Joined: Fri Aug 06, 2021 10:30 pm
Re: How would I create spawnable objects in love2d without using OOP?
Metatable spawing works like this:
Code: Select all
base={a=1,b=2,c=3,d=4}
meta={__index=function(tab,name) return base[name] end}
object1=setmetatable({a=99,c=5656},meta)
object2=setmetatable({l=0,d=1},meta)
object3=setmetatable({c="nope"},meta)
for k,v in pairs(base) do
print("key:",k,"value:",v)
print(object1[k])
print(object2[k])
print(object3[k])
end
obey
Re: How would I create spawnable objects in love2d without using OOP?
Just forget metatables and use arrays / objects?
Code: Select all
enemies={}
function create_enemies(no)
for i = 1, #no do
enemies[i] = {
type='jumping_carp',
health=10
}
end
end
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
Re: How would I create spawnable objects in love2d without using OOP?
Will this also work for updating animations and other information?BrotSagtMist wrote: ↑Sat Sep 30, 2023 6:03 am Metatable spawing works like this:Code: Select all
base={a=1,b=2,c=3,d=4} meta={__index=function(tab,name) return base[name] end} object1=setmetatable({a=99,c=5656},meta) object2=setmetatable({l=0,d=1},meta) object3=setmetatable({c="nope"},meta) for k,v in pairs(base) do print("key:",k,"value:",v) print(object1[k]) print(object2[k]) print(object3[k]) end
Re: How would I create spawnable objects in love2d without using OOP?
Can I also add other parameters like location?dusoft wrote: ↑Sat Sep 30, 2023 8:28 amJust forget metatables and use arrays / objects?
You will have to loop over enemies object in both update() and draw().Code: Select all
enemies={} function create_enemies(no) for i = 1, #no do enemies[i] = { type='jumping_carp', health=10 } end end
Re: How would I create spawnable objects in love2d without using OOP?
Sure, you can add any parameters, completely up to you. You can also change or remove any or add callbacks to functions.
See also this tutorial: https://videlais.com/2018/11/03/learnin ... d-looping/
See also this tutorial: https://videlais.com/2018/11/03/learnin ... d-looping/
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
Who is online
Users browsing this forum: Semrush [Bot] and 6 guests