--stats.lua local bot = {} local bot\_mt = {\_\_index = bot} local function start(self) --do something with this bot end local function stop(self) --do something with this bot end function bot:new(x, y, color, height, width) local newBot = {} newBot.x, newBot.y = x,y newBot.height, newBot.width, newBot.fill = height, width, color --declare functions for bot newBot.start = start newBot.stop = stop return setmetatable( newBot, bot\_mt ) end return bot --main.lua local bot = require "stats" local bots = {} for i = 1, 20 do local aNewBot = bot:new(i, i, {1,0,0,0.5}, 10, 10) bots[#bots+1] = aNewBot print(aNewBot.x, aNewBot.y) print(aNewBot.fill) end
The post I gave you was a great start… I do a similar thing in my game and I spawn hundreds of the same object.
Try researching “corona metatables” so you understand the concepts. I don’t know if there are any Corona tutorials on the subject as it is quite advanced.
Maybe try analyze code from open source games from Ponywolf (they are available in marketplace). Look like they organize objects in game. I’m making clon of Pong based his code. My game is available free on github (work in progress). Good luck:)
@Rob Miracle, If I set defaults for those parameters, does that mean I will have to write other individual functions if another unit has a different buff or frequency? Could you provide an example of what you are talking about? Is the metatable car example in this article: https://coronalabs.com/learn-lua/ similar to what you are talking about?
@Idurniat, I did not know about the open-source games, I will try looking at them.
I am understanding a little more about metatables, I just don’t know where they would be used in my game. Looking at the Classes and Inheritance section of the article, I could use them to give all the enemies the same properties, like make them move towards the player base or attack the player’s units. Would those be possibilities?