Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Same object with different variable values
Started by gammabeam Nov 18 2010 04:18 AM

8 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

gammabeam

[GLOBAL: userInfoPane.html]
gammabeam
  • Enthusiast

  • 33 posts
  • Guests

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

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!!
uid: 11130 topic_id: 3766 reply_id: 303766


[TOPIC: post.html]
#2

gammabeam

[GLOBAL: userInfoPane.html]
gammabeam
  • Enthusiast

  • 33 posts
  • Guests

Can't edit... :)

For example: How would I be able to set up a FOR to test each object's "bonus" variable?
uid: 11130 topic_id: 3766 reply_id: 11424


[TOPIC: post.html]
#3

jmp909

[GLOBAL: userInfoPane.html]
jmp909
  • Corona Geek

  • 1,328 posts
  • Corona SDK

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

it's a good place to start anyway

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


[TOPIC: post.html]
#4

gammabeam

[GLOBAL: userInfoPane.html]
gammabeam
  • Enthusiast

  • 33 posts
  • Guests

jmp909 saves my day once again! ;-)
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 :)

Cheers mate!
uid: 11130 topic_id: 3766 reply_id: 11426


[TOPIC: post.html]
#5

gammabeam

[GLOBAL: userInfoPane.html]
gammabeam
  • Enthusiast

  • 33 posts
  • Guests

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?!
uid: 11130 topic_id: 3766 reply_id: 11428


[TOPIC: post.html]
#6

gammabeam

[GLOBAL: userInfoPane.html]
gammabeam
  • Enthusiast

  • 33 posts
  • Guests

** EDIT **
uid: 11130 topic_id: 3766 reply_id: 11429


[TOPIC: post.html]
#7

GrahamRanson

[GLOBAL: userInfoPane.html]
GrahamRanson
  • Corona Geek

  • 1,426 posts
  • Guests

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:


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"

uid: 5833 topic_id: 3766 reply_id: 11433


[TOPIC: post.html]
#8

gammabeam

[GLOBAL: userInfoPane.html]
gammabeam
  • Enthusiast

  • 33 posts
  • Guests

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?
uid: 11130 topic_id: 3766 reply_id: 11444


[TOPIC: post.html]
#9

GrahamRanson

[GLOBAL: userInfoPane.html]
GrahamRanson
  • Corona Geek

  • 1,426 posts
  • Guests

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)

uid: 5833 topic_id: 3766 reply_id: 11446



[topic_controls]
[/topic_controls]