Same object with different variable values

Hi guys!

I’ve been building up a game for almost a week now, and so far things have been great.
However, I sometimes stumble into some things that look easy, but my inexperience holds me back :stuck_out_tongue:

In my game, I have to spawn objects regularly, but they must hold a variable because, with each hit, they will award more points when they are destroyed. However, with the LUA not being very OOPish, I can’t seem to make this happen.

Should I create instances of the object with different names? Would that work? If so, how can I do this?
Can you guys please enlighten me?

Thanks in advance!! [import]uid: 11130 topic_id: 3766 reply_id: 303766[/import]

Can’t edit… :slight_smile:

For example: How would I be able to set up a FOR to test each object’s “bonus” variable? [import]uid: 11130 topic_id: 3766 reply_id: 11424[/import]

try this thread for starters

http://developer.anscamobile.com/forum/2010/11/09/display-objects-custom-component-question#comment-10615

the downside of convenience methods is that they add more code, hence run slower. but that’s if you need to worry about 10,000 objects :wink:

it’s a good place to start anyway

and here:
http://developer.anscamobile.com/forum/2010/11/05/structuring-games-without-oop [import]uid: 6645 topic_id: 3766 reply_id: 11425[/import]

jmp909 saves my day once again! :wink:
I’ll read this, but it looks easy enough.

Quick question: Can I do this without “requiring” a class? I mean, doing everything in the main.lua?
Not that its a problem, just trying to understand it :slight_smile:

Cheers mate! [import]uid: 11130 topic_id: 3766 reply_id: 11426[/import]

Hmmm I got everything but that’s not exactly what I wanted…

What’s missing is a way for me to create instances of the objects, but with different, dynamic names.

For example:

ball1
ball2
ball3

and so on.

So each instance has its own set of variables, but I can test them individually (when the hit the target, for example).

Can I do this?! [import]uid: 11130 topic_id: 3766 reply_id: 11428[/import]

** EDIT ** [import]uid: 11130 topic_id: 3766 reply_id: 11429[/import]

The way I have been doing a basic OO structure is like this:

For examples sake I have called this “ball.lua”

  
module(..., package.seeall)  
  
function new(name)  
  
 local self = {} -- this is just a blank table but it could be a Corona display object like display.newImage("ball.png")  
  
 self.name = name  
  
 function self:print()  
 print(self.name)  
 end  
  
 return self  
   
end  
  

Then after including this in for instance your main.lua file you will be able to do things like this:

[code]

local ball = require(“ball.lua”)

local ball1 = ball.new(“Ball1”)
local ball2 = ball.new(“Ball2”)
local ball3 = ball.new(“AnyNameYouWant”)

ball1:print() – Prints “Ball1”
ball2:print() – Prints “Ball2”
ball3:print() – Prints “AnyNameYouWant”

[/code] [import]uid: 5833 topic_id: 3766 reply_id: 11433[/import]

Hi Graham!

Thanks for the reply, but I got this already. What I’m trying to figure out is a way of creating those names dynamically.

For example, let’s say I want to create 40 balls.
I wanted to set up a FOR and add the counter to the name of each ball.

Then, if I wanted to pick up the Y position or a variable “.blahblah” of a certain ball (i.e. 25), I would just say:

print(ball25.blahblah)

Get it? [import]uid: 11130 topic_id: 3766 reply_id: 11444[/import]

You could do something like this:

  
local balls = {}  
  
for i = 1, 50, 1 do  
 balls["ball" .. i] = ball.new()  
end  
  

And then access them like this:

  
print(balls["ball25"].blahblah)  
  

I don’t know about the efficiency of this sort of thing though compared to just doing:

  
print(balls[25].blahblah)  
  

[import]uid: 5833 topic_id: 3766 reply_id: 11446[/import]