How do I reference variables in other variables?

I am trying to make a game where a blob eats apples. In order to make the dissapear when you eat it, a ‘gone’ variable was once used. Now, as I see that that is hard to use if you want the apple to reappear in another random spot, so I replaced it with appleNumber. The problem is. I can’t efficiently make a new apple in a new spot. I was hoping to use the existing variables appleX, and appleY, to determine area but for the second apple I need to make a whole new variable manually how do you make it so the code automatically does so. I know how to do it with strings “string1” + “string2”, but how do you do it with variables without arithmetically adding them up?

TLDR: How do you put variables next to one other to make a new one without adding them?

Without seeing your code it’s hard to tell. I suggest looking at my simple snake example:https://gist.github.com/Westenburg/6487451

In it the when the snake eats a mushroom a new one is generated at a random location.

As an alternative look up tables - I think they might help your TLDR question

Well currently the apple generation code is this:

appleX = math.random (1,560)
appleY = math.random (1,560)
appleX1 = math.random (1,560)
appleY1 = math.random (1,560)
appleX2 = math.random (1,560)
appleY2 = math.random (1,560)

But, instead is there a way for for me to make as many apples as I want randomly without havering to use an infinite amount of variables and functions to eat the apple?

@Adam9812 - you need to learn about Lua tables. There are various tutorials around. I also cover them in my ebook below

https://www.dropbox.com/s/qh1e7ft4rvhlpt2/Lua%20for%20beginners.pdf

So sre tables the solution?

@Adam9812 Here’s an example using tables to draw 50 hearts. Replace the heart sprite with your apple sprite.


displayMode(FULLSCREEN)

function setup()
    apples={}
    for a=1,50 do
        table.insert(apples,vec2(math.random(WIDTH),math.random(HEIGHT)))
    end
end

function draw()
    background(40, 40, 50)
    for a,b in pairs(apples) do
        sprite("Small World:Heart",b.x,b.y)
    end
end

I am sorry if I seem unknowledgeble about lua. Thanks though. I will be sure to finish you book so I can more sufficiently solve these problems myself.

@Adam9812 - yes, learning about Lua will help a lot. I have other ebooks and a lot of posts here

http://coolcodea.wordpress.com/2013/06/19/index-of-posts/

And there is lots more stuff on the wiki link above.

Never apologize for seeming unknowledgable, everyone starts there at some point \^-^/
Plus there’s a whole forum segment dedicated to peoples’ questions, you aren’t alone