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.
Table (list?) checks
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- mangadrive
- Prole
- Posts: 32
- Joined: Sat Nov 17, 2012 10:26 pm
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Table (list?) checks
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.
- mangadrive
- Prole
- Posts: 32
- Joined: Sat Nov 17, 2012 10:26 pm
Re: Table (list?) checks
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
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
Re: Table (list?) checks
For me, the lib called Simple Educative Class System helped. For a demo, look here.
- mangadrive
- Prole
- Posts: 32
- Joined: Sat Nov 17, 2012 10:26 pm
Re: Table (list?) checks
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
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
Re: Table (list?) checks
From your post I'm not sure if the problem is solved, so here's your psudocode in lua:mangadrive wrote: If KeyHit(Key_Tab)
For SpaceShip:TSpaceShip = EachIn GameObjectList
If SpaceShip.X=200 Then SpaceShip.TakeDamage()
Next
EndIf
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
- mangadrive
- Prole
- Posts: 32
- Joined: Sat Nov 17, 2012 10:26 pm
Re: Table (list?) checks
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?
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?
Re: Table (list?) checks
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.
- mangadrive
- Prole
- Posts: 32
- Joined: Sat Nov 17, 2012 10:26 pm
Re: Table (list?) checks
Code: Select all
function Ship:new()
local object = {
x = 0,
y = 0,
health = 100
}
return object
end
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
Re: Table (list?) checks
Oh, ok.
Then you could use code like this:
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.
Then you could use code like this:
Code: Select all
ships = {
ship:new(),
ship:new(),
ship:new(),
}
EDIT:
You can also use "table.insert(tablename, item)" to add a variable to the list.
Who is online
Users browsing this forum: Google [Bot] and 3 guests