How to Compare Table Key with number?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Larsii30
Party member
Posts: 267
Joined: Sun Sep 11, 2011 9:36 am
Location: Germany

How to Compare Table Key with number?

Post by Larsii30 »

Hi,
just a short question. How could I compare the Table KEY with a number ?

Example of what I want to do :

Code: Select all


img = {
             enemyBlue,
             enemyGreen
         }

if img == 1 then 
                     explodeBlue ()
end

                  if img == 2 then
                                        explodeGreen()
                  end

now the code try to compare the data of img (enemy1 / enemy2 / enemy3) with the numbers. But I want a compare with the key (img [key])
Hopefully you understand what I mean.

Greets,
Lars
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to Compare Table Key with number?

Post by Robin »

What exactly do you want to do? (As in, what purpose?)

It's a bit unclear what you want.
Help us help you: attach a .love.
User avatar
Larsii30
Party member
Posts: 267
Joined: Sun Sep 11, 2011 9:36 am
Location: Germany

Re: How to Compare Table Key with number?

Post by Larsii30 »

okay. What I do :

Code: Select all


-- I load my images this way
img.alien = {
		lg.newQuad(32, 16, 16,16,img.Size,img.Size),
		lg.newQuad(0, 16, 16,16,img.Size,img.Size),
		lg.newQuad(16, 16, 16,16,img.Size,img.Size),
		lg.newQuad(32, 32, 16,16,img.Size,img.Size),
		lg.newQuad(0, 32, 16,16,img.Size,img.Size),
		lg.newQuad(16, 32, 16,16,img.Size,img.Size)
	    }

-- then I could draw them, only one now for example

lg.drawq (img.tiles, img.alien[1], aliens[1].x, aliens[1].y,0,img.scale,img.scale)

Thats an alien on screen.
Now, if I hit one, I draw a explosion at its position for a short time
The alien images got different colours so the explosion image should be in the same colour as the alien colour.

I try :

Code: Select all

if img.alien == 1 then
                       lg.drawq( img.tiles , img.boom , aliens[i].x , aliens[i].y , 0  ,img.scale,img.scale )
end
Thats not possible for sure. So my question how could I compare the table KEY ( the number in the [] i.e img.alien[ 1 ] ) with a number?
Or maybe a tip how I could do it better :)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to Compare Table Key with number?

Post by Robin »

You want to keep a separate variable for that (I still don't exactly know what you want).

Code: Select all

current_alien = 1
lg.drawq (img.tiles, img.alien[current_alien], aliens[current_alien].x, aliens[current_alien].y,0,img.scale,img.scale)

if current_alien == 1 then
    -- blah
end
Help us help you: attach a .love.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: How to Compare Table Key with number?

Post by miko »

Larsii30 wrote:Hi,
just a short question. How could I compare the Table KEY with a number ?
You either search for it, or "remember" it.
So:

Code: Select all

function getByKey(Table, key)
  for k,v in pairs(Table) do
    if k==key then return v end
  end
end

enemy=getByKey(Enemies, 1)
or:

Code: Select all

function myinsert(Table, value)
  table.insert(Table, value)
  Table[value]=#Table
end

Enemies={}

enemy1=Enemy()
enemy2=Enemy()
myinsert(Enemies, enemy1)
myinsert(Enemies, enemy2)
print(string.format("The index of %s is: %s", enemy1, Enemies[enemy1]))
The trick with the second example is that you keep entries indexed both by number and by value (in one table). It works as long as you do not remove/reorder the items in the table. Seems it should work for you.
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: How to Compare Table Key with number?

Post by kikito »

I would create one separate table for each enemy, and use common names for the "image", "explosion", etc.

Code: Select all

local enemy1 = { image = enemy1, explosion = explosion1, x = 0, y = 0 }
local enemy2 = { image = enemy2, explosion = explosion2, x = 100, y = 200 }
When later on you are drawing an enemy and you don't know if it's enemy1 or enemy2, you just don't care what explosion/image they have - you use the generic "enemy.image" or "enemy.explosion" fields:

Code: Select all

function drawEnemy(enemy)
  love.graphics.draw(enemy.image, enemy.x, enemy.y)
end

function drawEnemyExplosion(enemy)
  love.graphics.draw(enemy.explosion, enemy.x, enemy.y)
end
Edit: Lua is not Javascript
Last edited by kikito on Mon Sep 26, 2011 12:50 pm, edited 1 time in total.
When I write def I mean function.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: How to Compare Table Key with number?

Post by BlackBulletIV »

kikito wrote:

Code: Select all

local enemy1 = { image: enemy1, explosion: explosion1, x: 0, y: 0 }
local enemy2 = { image: enemy2, explosion: explosion2, x: 100, y: 200 }
A little syntax mixup there.

Code: Select all

local enemy1 = { image = enemy1, explosion = explosion1, x = 0, y = 0 }
local enemy2 = { image = enemy2, explosion = explosion2, x = 100, y = 200 }
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: How to Compare Table Key with number?

Post by kikito »

:oops: too much ruby and javascript lately... fixing my post just to not confuse anybody.
When I write def I mean function.
User avatar
Larsii30
Party member
Posts: 267
Joined: Sun Sep 11, 2011 9:36 am
Location: Germany

Re: How to Compare Table Key with number?

Post by Larsii30 »

yes thanks miko thats what I want. :)

I don't use it now for the images but its great to know it.
I've got one more question. Look at this :

Code: Select all

variable1 = variable 1 + 1

In C++ i could do this :

Code: Select all


variable1 += 1;

instead of write the name of variable1 again. Is something like this possible in lua ?
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: How to Compare Table Key with number?

Post by miko »

Larsii30 wrote:I've got one more question. Look at this :

Code: Select all

variable1 = variable 1 + 1

In C++ i could do this :

Code: Select all


variable1 += 1;

instead of write the name of variable1 again. Is something like this possible in lua ?
No. You can create an object with a method like enemy:increaseAmmo(), but you cannot use increment/decrement operators.
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests