How would I create spawnable objects in love2d without using OOP?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
MaxGamz
Party member
Posts: 101
Joined: Fri Oct 28, 2022 3:09 am

How would I create spawnable objects in love2d without using OOP?

Post 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?
User avatar
BrotSagtMist
Party member
Posts: 628
Joined: Fri Aug 06, 2021 10:30 pm

Re: How would I create spawnable objects in love2d without using OOP?

Post 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
obey
User avatar
dusoft
Party member
Posts: 518
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: How would I create spawnable objects in love2d without using OOP?

Post 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().
MaxGamz
Party member
Posts: 101
Joined: Fri Oct 28, 2022 3:09 am

Re: How would I create spawnable objects in love2d without using OOP?

Post 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?
MaxGamz
Party member
Posts: 101
Joined: Fri Oct 28, 2022 3:09 am

Re: How would I create spawnable objects in love2d without using OOP?

Post 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?
User avatar
dusoft
Party member
Posts: 518
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: How would I create spawnable objects in love2d without using OOP?

Post 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/
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests