Table (list?) checks

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
mangadrive
Prole
Posts: 32
Joined: Sat Nov 17, 2012 10:26 pm

Table (list?) checks

Post by mangadrive »

Slightly new to LUA, not too new to OOP.

I'm assuming from all the reading that I would put all of my players/enemies/game objects in tables in LUA to represent the list.

I've dug around for some examples, but my mind is a terrible sponge unless we are talking context. Could someone give a brief example of how I would use a table to know if an Enemy was X,Y before I shot something at it. That's enough to get me on my merry way for a bit.

Thank you for your time in advance.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Table (list?) checks

Post by Robin »

I'm not exactly sure what you want. Could you maybe explain what "know if an Enemy was X,Y before I shot something at it" means in a bit more detail. (Maybe make a drawing if you can't find the right words.)
Help us help you: attach a .love.
User avatar
mangadrive
Prole
Posts: 32
Joined: Sat Nov 17, 2012 10:26 pm

Re: Table (list?) checks

Post by mangadrive »

Yes, my apologies for terrible wording.

1. I've created all my Enemy objects and put them in a list.

2. I have a selection device object that points on a grid x, y style.

3. I then need to call the list/table to know which enemy I'm about to attack.

Psudeo code (This is how it's done in Blitzmax, which is a bit C-ish I suppose):

If KeyHit(Key_Tab)
For SpaceShip:TSpaceShip = EachIn GameObjectList

If SpaceShip.X=200 Then SpaceShip.TakeDamage()
Next
EndIf

That attacks only the enemy at X 200. Not the whole list :)


If I'm doing this wrong, feel free to recommend another way. I'm just doing what I know works in other coding :x
User avatar
Nsmurf
Party member
Posts: 191
Joined: Fri Jul 27, 2012 1:58 am
Location: West coast.

Re: Table (list?) checks

Post by Nsmurf »

For me, the lib called Simple Educative Class System helped. For a demo, look here.
OBEY!!!
My Blog
UE0gbWUgd2l0aCB0aGUgd29yZCAnSE1TRycgYXMgdGhlIHN1YmplY3Q=
User avatar
mangadrive
Prole
Posts: 32
Joined: Sat Nov 17, 2012 10:26 pm

Re: Table (list?) checks

Post by mangadrive »

Thank you, I did bookmark this for later.

I looked at MiddleClass and a couple other mods, but I was going to stick with the core for the time being so I can translate well with others. I'm taking up LUA for a specific reason ;) I'm just stuck at tables and list Iteration for the time being. I mean I understand WHAT I should be doing, but not HOW :)
User avatar
Nsmurf
Party member
Posts: 191
Joined: Fri Jul 27, 2012 1:58 am
Location: West coast.

Re: Table (list?) checks

Post by Nsmurf »

mangadrive wrote: If KeyHit(Key_Tab)
For SpaceShip:TSpaceShip = EachIn GameObjectList

If SpaceShip.X=200 Then SpaceShip.TakeDamage()
Next
EndIf
From your post I'm not sure if the problem is solved, so here's your psudocode in lua:

Code: Select all


ships = {
    {x = 100, y = 150},
    {x = 150, y = 100},
    {x = 200, y = 200},
}

if love.keyboard.isDown("x") then
    for i = ships, #ships, 1 do
        if ships[i].x = 200 then
            --Attack ship
        end
    end
end
OBEY!!!
My Blog
UE0gbWUgd2l0aCB0aGUgd29yZCAnSE1TRycgYXMgdGhlIHN1YmplY3Q=
User avatar
mangadrive
Prole
Posts: 32
Joined: Sat Nov 17, 2012 10:26 pm

Re: Table (list?) checks

Post by mangadrive »

Thank you so very much. This is exactly what I needed to see :) I have a hard time explaining to people that source code and tutorials are moonspeak to me unless put in context, so I appreciate the patience you all gave me.

I'm assuming that I could put the entire "ship" object in the list and just iterate for X,Y,Health,is_Level90098908 as well like?


s = Ship:new()
t = Ship:new()

ships = {
{t},
{s},
}

if love.keyboard.isDown("x") then
for i = ships, #ships, 1 do
if ships.x = 200 then
--Attack ship
end
end

Is that kinda right? :)
User avatar
Nsmurf
Party member
Posts: 191
Joined: Fri Jul 27, 2012 1:58 am
Location: West coast.

Re: Table (list?) checks

Post by Nsmurf »

mangadrive wrote:I'm assuming that I could put the entire "ship" object in the list and just iterate for X,Y,Health,is_Level90098908 as well like?


s = Ship:new()
t = Ship:new()

ships = {
{t},
{s},
}

if love.keyboard.isDown("x") then
for i = ships, #ships, 1 do
if ships.x = 200 then
--Attack ship
end
end


What are you using that allows you to call Ship:new?

SECS? Or is it built into lua, and I just didn't know about it?

P.S.
Next time you post code, please use the "Code" tags.
OBEY!!!
My Blog
UE0gbWUgd2l0aCB0aGUgd29yZCAnSE1TRycgYXMgdGhlIHN1YmplY3Q=
User avatar
mangadrive
Prole
Posts: 32
Joined: Sat Nov 17, 2012 10:26 pm

Re: Table (list?) checks

Post by mangadrive »

Code: Select all

function Ship:new()

    local object = {
    x = 0,
    y = 0,
	health = 100
    }

    return object
end
Just a basic function to create.

Normally though I'd have something like this right before the return in a similar function in other languages:

Code: Select all

-- This is psudeo code
addlistlast Ship, Shiplist
I'm still trying to figure out how to go about my lists in LUA
User avatar
Nsmurf
Party member
Posts: 191
Joined: Fri Jul 27, 2012 1:58 am
Location: West coast.

Re: Table (list?) checks

Post by Nsmurf »

Oh, ok.

Then you could use code like this:

Code: Select all

ships = {
    ship:new(),
    ship:new(),
    ship:new(),
}
Because "return" will return the list that contains all that stuff, eliminating the extra variables.

EDIT:

You can also use "table.insert(tablename, item)" to add a variable to the list.
OBEY!!!
My Blog
UE0gbWUgd2l0aCB0aGUgd29yZCAnSE1TRycgYXMgdGhlIHN1YmplY3Q=
Post Reply

Who is online

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