Data Realms Fan Forums http://45.55.195.193/ |
|
Help with Arrays? http://45.55.195.193/viewtopic.php?f=73&t=16965 |
Page 1 of 1 |
Author: | xerxys [ Mon Oct 26, 2009 4:10 pm ] |
Post subject: | 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. |
Author: | Abdul Alhazred [ Mon Oct 26, 2009 4:27 pm ] |
Post subject: | Re: Help with Arrays? |
Technically Lua only use tables. You can read about it here: http://www.lua.org/pil/2.5.html |
Author: | xerxys [ Mon Oct 26, 2009 5:01 pm ] |
Post subject: | 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? |
Author: | Duh102 [ Mon Oct 26, 2009 5:24 pm ] |
Post subject: | 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 |
Author: | xerxys [ Mon Oct 26, 2009 5:57 pm ] |
Post subject: | 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 |
Author: | Duh102 [ Mon Oct 26, 2009 6:08 pm ] |
Post subject: | 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... |
Author: | Geti [ Mon Oct 26, 2009 8:47 pm ] |
Post subject: | Re: Help with Arrays? |
if not #telelist == 0 then |
Author: | xerxys [ Mon Oct 26, 2009 9:08 pm ] |
Post subject: | Re: Help with Arrays? |
I tried Code: elseif self.tele == 1 and #telelist ~= 0 then And it gave the same error. Any ideas? |
Author: | Grif [ Mon Oct 26, 2009 11:39 pm ] |
Post subject: | 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 |
Author: | Geti [ Tue Oct 27, 2009 2:11 am ] |
Post subject: | 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. |
Author: | Grif [ Tue Oct 27, 2009 5:58 am ] |
Post subject: | 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. |
Author: | xerxys [ Tue Oct 27, 2009 9:58 am ] |
Post subject: | 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; Code: telelist = {} Thanks. |
Author: | Grif [ Tue Oct 27, 2009 3:19 pm ] |
Post subject: | 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 |
Author: | Geti [ Wed Oct 28, 2009 10:23 am ] |
Post subject: | 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) |
Author: | Daman [ Sat Oct 31, 2009 6:40 pm ] |
Post subject: | 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 |
Page 1 of 1 | All times are UTC [ DST ] |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |