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

I am trying to set a color, but it does not work.

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

This is what I get printed (but 10 more times):

 1 1 table: 0x7ff73bf67bc0 2 2 table: 0x7ff73bf67d10 3 3 table: 0x7ff73bf67f80 4 4 table: 0x7ff73bf68230 5 5 table: 0x7ff73bf68350 6 6 table: 0x7ff7397a8040 7 7 table: 0x7ff7397a8340 8 8 table: 0x7ff7397a85d0 9 9 table: 0x7ff7397a8860 10 10 table: 0x7ff7397a8a90

How do I fix this? 

Set the colour in new() like this

aNewBot:setFillColor( color[1], color[2], color[3], color[4] or 1 )

How could I set an image for the bots? I tried this:

function bot:new(x, y, height, width, image) local newBot = {} newBot.x, newBot.y, x,y newBot.anchorX, newBot.anchorY = 1,0 newBot.height, newBot.width = height, width) newBot = display.newImage(image) --declare functions for bot newBot.start = start newBot.stop = stop return setmetatable( newBot, bot\_mt ) end

But it only appeared once at the the coordinate 0,0.

Look, I am busy and can’t write your stuff for you.  You are going to have to figure some stuff out for yourself.

But briefly you cannot say

newBot = display.newImage(image)

your would say

newBot.image = display.newImage(image) newBot.image.x, newBot.image.y = x, y

Thank you, and I apologize for using up your time.  :frowning:

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?