How to store data about enemy attack, health, attack speed, and movement speed?

The question says it all. How do I store it, in a table, or a module? I want to be able to spawn the enemies in one line of code:

bot(atkBuff=1.5, hpBuff=1.5, spawnTime=1000, spawnFrequency=5)

Something like this, that controls buffing the enemy and placing an optional pattern of spawning it multiple times. 

I am thinking that making a table of stats in a module, than writing functions for each enemy would work. 

enemies = { bot = {atk=1000, hp=1000, atkSpeed=40, movementSpeed=100} } function bot() end

However, is there a more efficient manner?

You need to use metatables and create instances of a module (or class in OOP).  This might help explain things https://forums.coronalabs.com/topic/58376-question-regarding-metatables-and-oop/

Thanks! I read here that 30 log can help with creating this. Do you know of any other tutorials or samples?

I just need a small tutorial with only 2 or 3 units and a way to spawn them in a similar fashion in this game:

https://www.youtube.com/watch?v=9zI43_0I2ns

I am just having trouble getting started. 

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.

Read more here

May I ask what game did you release?

https://coronalabs.com/blog/2017/04/12/featured-game-designer-city/

Still very confused.  :frowning:

What are the advantages of using metatables over regular modules and tables?

confused == not enough research done

You might want to look here:

https://coronalabs.com/learn-lua/

And scroll down to the metatable discussion. 

Rob

I think this is a bit too much for me at the moment. Thanks for the help.

Stick with it sdktester15! You can do it!  I’ve been at it for years and still bump into new methods and ways of doing things!

Metatables are not an easy to grasp concept, but at the same time it’s not that hard.

One way to use metatables is to use them to initialize your object. Instead of coding a function:

bot(atkBuff=1.5, hpBuff=1.5, spawnTime=1000, spawnFrequency=5)

you can use the metatable to set the defaults for each of those parameters. Then you could just do:

bot( {spawnTime=2000} )

Rob

and you get all the defaults set for you.

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:)

@LavaLevel, Thanks for the support!

@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?

Here is a skeleton class you can extend

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,...)   local newBot = {} //set properties of bot based on parameters based   newBot.x, newBot.y = x, y //declare functions for bot   newBot.start = start   newBot.stop = stop   return setmetatable( newBot, bot\_mt ) end return bot

Now you can create multiple instances of bot - in this case we will spawn 100 of them

local bots = {} for i = 1, 100 do   local aNewBot = bot:new(i,i)   bots[#bots+1] = aNewBot end

You can also reference them individually

bots[25]:start() bots[71]:stop()

  

I was referring to the car example. But the post above does a great job of showing how to use it.

Rob

@Sphere Game Studios, Thanks for the template! I am trying it out.

Thank you so much for this template, I was able to print out all their x and y values. Now I will move on to more complex things, like printing atk or def.