Author |
Message |
xerxys
Joined: Sun Feb 17, 2008 11:21 am Posts: 54
|
Help with Arrays?
I do not understand how to use arrays, i.e. how to check if an object is in an array, how to add to an array etc. etc. There is no Wiki article and the lua website page is more than slightly confusing. Could someone enlighten me and perhaps provide a wiki page?
Thanks a load.
|
Mon Oct 26, 2009 4:10 pm |
|
|
Abdul Alhazred
DRL Developer
Joined: Tue Aug 11, 2009 5:09 am Posts: 395
|
Re: Help with Arrays?
Technically Lua only use tables. You can read about it here: http://www.lua.org/pil/2.5.html
|
Mon Oct 26, 2009 4:27 pm |
|
|
xerxys
Joined: Sun Feb 17, 2008 11:21 am Posts: 54
|
Re: Help with Arrays?
I read through that and now understand more, but how do I check if an array contains a certain string for example?
|
Mon Oct 26, 2009 5:01 pm |
|
|
Duh102
happy carebear mom
Joined: Tue Mar 04, 2008 1:40 am Posts: 7096 Location: b8bbd5
|
Re: Help with Arrays?
Code: for item in (table) do if... ... end end To be more specific, if it's a table of strings, Code: for string in (table) do if string == "Something" then do something; end end
|
Mon Oct 26, 2009 5:24 pm |
|
|
xerxys
Joined: Sun Feb 17, 2008 11:21 am Posts: 54
|
Re: Help with Arrays?
Right now I get an error that reads roughly "table index is nil, see line 86" I have no idea what is wrong. here's the code; Code: function Create(self) self.reloadTimer = Timer(); self.reloadTime = 3000 self.repartTimer = Timer(); self.repartTime = 10000 self.gravTester = CreateMOPixel("Teleporty Grav Tester"); self.gravTester.Pos = Vector(self.Pos.X, self.Pos.Y); self.gravTester.Vel = Vector(0,0); MovableMan:AddParticle(self.gravTester); self.gravAcc = 0
self.tele = 0 self.spam = 0 telelist = {} end
function Update(self) if not(self:IsDead()) then if self.gravAcc == 0 then if self.gravTester.Vel.Y > 0 then self.gravAcc = self.gravTester.Vel.Y; end end self.RotAngle = 0; local ownController = self:GetController(); if ownController:IsState(Controller.HOLD_LEFT) then self.Vel.X = self.Vel.X - 0.5; if self.Vel.X < -15 then self.Vel.X = -15; end elseif ownController:IsState(Controller.HOLD_RIGHT) then self.Vel.X = self.Vel.X + 0.5; if self.Vel.X > 15 then self.Vel.X = 15; end elseif self.Vel.X < 0 then self.Vel.X = self.Vel.X + 0.5; if self.Vel.X > 0 then self.Vel.X = 0; end elseif self.Vel.X > 0 then self.Vel.X = self.Vel.X - 0.5; if self.Vel.X < 0 then self.Vel.X = 0; end end if ownController:IsState(Controller.HOLD_UP) then self.Vel.Y = self.Vel.Y - 0.5; if self.Vel.Y < -15 then self.Vel.Y = -15; end elseif ownController:IsState(Controller.HOLD_DOWN) then self.Vel.Y = self.Vel.Y + 0.5; if self.Vel.Y > 15 then self.Vel.Y = 15; end elseif self.Vel.Y < 0 then self.Vel.Y = self.Vel.Y + 0.5; if self.Vel.Y > 0 then self.Vel.Y = 0; end elseif self.Vel.Y > 0 then self.Vel.Y = self.Vel.Y - 0.5; if self.Vel.Y < 0 then self.Vel.Y = 0; end end if ownController:IsState(Controller.WEAPON_FIRE) and self.tele == 0 then self.boxTL = Vector(self.Pos.X - 75 , self.Pos.Y - 35); self.boxBR = Vector(self.Pos.X + 75 , self.Pos.Y + 35); self.box = Box(self.boxTL , self.boxBR); for P in MovableMan.Actors do if self.box:WithinBox(P.Pos) then telelist[K] = P; self.tele = 1 print("got") end end elseif self.tele == 1 then for K in telelist do K.Pos = self.Pos print("ported") end self.spam = 1 end self.Vel.Y = self.Vel.Y - self.gravAcc end end
|
Mon Oct 26, 2009 5:57 pm |
|
|
Duh102
happy carebear mom
Joined: Tue Mar 04, 2008 1:40 am Posts: 7096 Location: b8bbd5
|
Re: Help with Arrays?
You don't have anything in the "telelist" table, do you? That's what it's saying. It's empty, so it can't do anything. Add in a quick check before the loop to eliminate that problem. elseif (self.tele == 1) and ~(telelist == nil) then Least, I think that's how you check if a table is empty...
|
Mon Oct 26, 2009 6:08 pm |
|
|
Geti
Joined: Sun Jul 13, 2008 9:57 am Posts: 4886 Location: some compy
|
Re: Help with Arrays?
if not #telelist == 0 then
|
Mon Oct 26, 2009 8:47 pm |
|
|
xerxys
Joined: Sun Feb 17, 2008 11:21 am Posts: 54
|
Re: Help with Arrays?
I tried Code: elseif self.tele == 1 and #telelist ~= 0 then And it gave the same error. Any ideas?
|
Mon Oct 26, 2009 9:08 pm |
|
|
Grif
REAL AMERICAN HERO
Joined: Sat Jan 27, 2007 10:25 pm Posts: 5655
|
Re: Help with Arrays?
for i in ipairs(table) do
alternately, for i in pairs(table) do
ipairs is iterated pairs, means it'll go through each table item in numeric order pairs just uses the next primitive
|
Mon Oct 26, 2009 11:39 pm |
|
|
Geti
Joined: Sun Jul 13, 2008 9:57 am Posts: 4886 Location: some compy
|
Re: Help with Arrays?
shouldnt it be for k,v in ipairs(table) do stuff end? where k is the index of the thing and v is the value. or does using one var return only the stored data? i use the two var method anyway so i can remove data more easily.
|
Tue Oct 27, 2009 2:11 am |
|
|
Grif
REAL AMERICAN HERO
Joined: Sat Jan 27, 2007 10:25 pm Posts: 5655
|
Re: Help with Arrays?
yeah my syntax was off
point is, ipairs/pairs is your friend
really not a *whole* lot of other uses for tables.
|
Tue Oct 27, 2009 5:58 am |
|
|
xerxys
Joined: Sun Feb 17, 2008 11:21 am Posts: 54
|
Re: Help with Arrays?
I did the ipairs thing, but I get an error before there at line 86 which now reads Code: for actors in MovableMan.Actors do if self.box:WithinBox(actors.Pos) then telelist[actor] = actors --Here is the problem self.tele = 1 print("got") end end else--if self.tele == 1 and #telelist ~= 0 then for actor, v in ipairs(telelist) do actor.Pos = self.Pos print("ported") end Any Ideas? did I create the table wrong? I did it like this; Thanks.
|
Tue Oct 27, 2009 9:58 am |
|
|
Grif
REAL AMERICAN HERO
Joined: Sat Jan 27, 2007 10:25 pm Posts: 5655
|
Re: Help with Arrays?
actor is a pointer to an MO, not a number
you're trying to put a pointer in as a number value.
try telelist[actor.ID] = actor;
alternately, just use a global increasing scale and reset your list when you're done
|
Tue Oct 27, 2009 3:19 pm |
|
|
Geti
Joined: Sun Jul 13, 2008 9:57 am Posts: 4886 Location: some compy
|
Re: Help with Arrays?
for the ipairs bit: for k,actor in ipairs(telelist) do also table.insert(telelist, actor) will put the actor into the table at the next available key. you can remove it later by finding it with ipairs() and string.find() on its presetname or some shindig and doing table.remove(telelist, k)
|
Wed Oct 28, 2009 10:23 am |
|
|
Daman
Joined: Fri Jan 26, 2007 3:22 am Posts: 1451
|
Re: Help with Arrays?
uh ok here is actually what is wrong Code: for actors in MovableMan.Actors do -- THIS IS WRONG WHY IS THE WORD ACTOR PLURAL THAT IS WRONG MAKE IT actor if self.box:WithinBox(actors.Pos) then --WRONG. telelist[actor] = actors --WHERE THE ♥♥♥♥ IS THE VARIABLE 'actor' SET? JUST USE table.insert(telelist, actor) AFTER YOU CHANGE THE FOR LOOP'S VARIABLE TO 'actor' INSTEAD OF 'actors' self.tele = 1 print("got") end end else--if self.tele == 1 and #telelist ~= 0 then for actor, v in ipairs(telelist) do --THIS LINE IS ALL RETARDED, PLEASE LOOK UP THE SYNTAX FOR A FOR LOOP. WHEN DOING IT IN THIS FORM IT WOULD RETURN A KEY, AND THEN THE VALUE OF THAT KEY IN THE GIVEN TABLE. YOU SEEM TO THINK IT'S RETURNING A VALUE AND THEN ANOTHER VALUE. actor.Pos = self.Pos -- WITH YOUR CURRENT CODE WITHOUT MY SUGGESTED IMPROVEMENTS THIS WOULD NEED TO BE v.Pos = self.Pos AS v IS SET TO THE VALUE OF EACH KEY IN THE TABLE, AND actor IS SET TO THE KEY OF EACH ENTRY IN THE TABLE. BUT YOU'RE TRYING TO STORE ACTORS THAT ARE WITHIN A BOX IN AN ITERATED TABLE WITH EACH ENTRY'S VALUE AS ONE ACTOR INSIDE THE BOX. print("ported") end hope that explained some things
|
Sat Oct 31, 2009 6:40 pm |
|
|
|