Thread Tools Display Modes
06-23-15, 04:54 AM   #1
FawKa
A Defias Bandit
Join Date: Jun 2015
Posts: 2
Lua Arrays Help

I am new to Lua and trying to grasp arrays but sooooo confused.

Why do i get error with the follow code?

local aliens={
{posX, posY, speedX, speedY}
}
for i=1, 5 do
aliens[i].posX = i*100
aliens[i].posY = i*200
aliens[i].speedX = math.random(2,4)
aliens[i].speedY = math.random(-4,4)
end

The error i get is:
main.lua:5: attempt to index field '?' (a nil value)
stack traceback:
main.lua:5: in main chunk
  Reply With Quote
06-23-15, 05:47 AM   #2
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
Your definition and use of arrays should match.

In this line:
Code:
local aliens={
{posX, posY, speedX, speedY}
}
You say aliens has 4 members:
aliens.posX (which is the same as aliens["posX"])
aliens.posY (which is the same as aliens["posY"])
aliens.SpeedX (which is the same as aliens["speedX"])
aliens.SpeedY (which is the same as aliens["speedY"])

By doing so it will imply that aliens[i] will be nill (because it is not one of the four members. So if aliens[i] is nil you can't use aliens[i].posX. posX is a member of aliens (which is a table) but posX is not a member of aliens[i] because aliens[i] is just nil.
The correct would be:
Lua Code:
  1. local aliens = {}
  2. for i=1,5 do
  3.      aliens[i] = {}
  4.      aliens[i].posX = i*100
  5.      aliens[i].posY = i*200
  6.      aliens[i].speedX = math.random(2,4)
  7.      aliens[i].speedY = math.random(-4,4)
  8. end
__________________
"In this world nothing can be said to be certain, except that fractional reserve banking is a Ponzi scheme and that you won't believe it." - Mandrill

Last edited by Banknorris : 06-23-15 at 05:52 AM.
  Reply With Quote
06-23-15, 01:10 PM   #3
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Banknorris: No, he's saying that aliens has 1 member, a table containing those other values.


FawKa: When doing this
Lua Code:
  1. local aliens={
  2. {posX, posY, speedX, speedY}
  3. }

You are doing this:
Lua Code:
  1. local aliens = {
  2.      [1] = {
  3.           [1] = posX,
  4.           [2] = posY,
  5.           [3] = speedX,
  6.           [4] = speedY
  7.      }
  8. }

If you are actually using posX, posY, etc. and nothing has been assigned to those values yet (ie, they are nil) then you end up with this instead (an empty table):
Lua Code:
  1. local aliens = {
  2.      [1] = {}
  3. }


If I assume what you want to do correctly, however, then what Banknorris provided will do it for you.

However, I greatly encourage you to go over this if you're still having trouble: http://lua-users.org/wiki/TablesTutorial
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
06-23-15, 01:10 PM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
Originally Posted by FawKa View Post
I am new to Lua and trying to grasp arrays but sooooo confused.

Why do i get error with the follow code?

Code:
local aliens={
	{posX, posY, speedX, speedY}
}
for i=1, 5 do
	aliens[i].posX = i*100
	aliens[i].posY = i*200
	aliens[i].speedX = math.random(2,4)
	aliens[i].speedY = math.random(-4,4)
end
The error i get is:
main.lua:5: attempt to index field '?' (a nil value)
stack traceback:
main.lua:5: in main chunk
The problem is you make a table at index 1 of aliens, then loop through entries 1 thru 5. You don't have any tables created for indices 2 and up. In addition to this, the way you're creating the aliens table isn't doing what you think it is. The first pair of braces creates the main table that has only one entry, which is the nested table defined by the second pair of braces. The nested table takes the values stored in variables with the names you specified and stores them in consecutive numerical indices (1 thru 4). To facilitate using a loop to populate the aliens table, you need to split this process as noted previously.

Originally Posted by Banknorris View Post
Lua Code:
  1. local aliens = {}
  2. for i=1,5 do
  3.      aliens[i] = {}
  4.      aliens[i].posX = i*100
  5.      aliens[i].posY = i*200
  6.      aliens[i].speedX = math.random(2,4)
  7.      aliens[i].speedY = math.random(-4,4)
  8. end
However, this can be done much more efficiently if you used the table constructor correctly.

Lua Code:
  1. local aliens={};--  Main table
  2. for i=1,5 do
  3.     aliens[i]={--   Nested table
  4.         posX=i*100,
  5.         posY=i*200,
  6.         speedX=math.random(2,4),
  7.         speedY=math.random(-4,4)
  8.     };
  9. end
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
06-23-15, 01:37 PM   #5
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
Originally Posted by SDPhantom View Post
However, this can be done much more efficiently if you used the table constructor correctly.
Funny that you mention that because coincidently I just read this http://www.lua.org/gems/sample.pdf and yes you are right it avoids an unnecessary re-hash.

Just noticed I misinterpreted his code. So my explanation is totally wrong.
__________________
"In this world nothing can be said to be certain, except that fractional reserve banking is a Ponzi scheme and that you won't believe it." - Mandrill

Last edited by Banknorris : 06-23-15 at 01:43 PM.
  Reply With Quote
08-04-15, 09:12 AM   #6
FawKa
A Defias Bandit
Join Date: Jun 2015
Posts: 2
Thank you..

To all who have reply to my question..

Thank you very much...

Learned from it. But i need to learn more as i am really new to Lua...
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Lua Arrays Help

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off