Page 1 of 1
How would I create spawnable objects in love2d without using OOP?
Posted: Sat Sep 30, 2023 3:37 am
by MaxGamz
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?
Re: How would I create spawnable objects in love2d without using OOP?
Posted: Sat Sep 30, 2023 6:03 am
by BrotSagtMist
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?
Posted: Sat Sep 30, 2023 8:28 am
by dusoft
MaxGamz wrote: ↑Sat Sep 30, 2023 3:37 am
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?
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
You will have to loop over enemies object in both update() and draw().
Re: How would I create spawnable objects in love2d without using OOP?
Posted: Sat Sep 30, 2023 1:38 pm
by MaxGamz
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
Will this also work for updating animations and other information?
Re: How would I create spawnable objects in love2d without using OOP?
Posted: Sat Sep 30, 2023 1:39 pm
by MaxGamz
dusoft wrote: ↑Sat Sep 30, 2023 8:28 am
MaxGamz wrote: ↑Sat Sep 30, 2023 3:37 am
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?
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
You will have to loop over enemies object in both update() and draw().
Can I also add other parameters like location?
Re: How would I create spawnable objects in love2d without using OOP?
Posted: Sat Sep 30, 2023 5:27 pm
by dusoft
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/