Data Realms Fan Forums
http://forums.datarealms.com/

Checking against all of a table simultaneously (solved)
http://forums.datarealms.com/viewtopic.php?f=73&t=31402
Page 1 of 1

Author:  Bad Boy [ Wed Jul 04, 2012 8:52 pm ]
Post subject:  Checking against all of a table simultaneously (solved)

I'm trying to fix up 4zk's gas script and I've got everything set up nicely. The only problem is that I have to check an actor's (in MovableMan.Actor) ID against the ID of all the actors in the table of masked actors. for i = 1 , #self.Masked do doesn't work because it doesn't act as and.
Essentially I'm hoping to get a method of doing if actor.ID ~= self.Masked[1].ID and actor.ID ~= self.Masked[2].ID ... then for #self.Masked. I've been trying for probably way too long and by brain is now frustrated and doing stupid things so I decided to give up and ask for help.
I'm not sure how much sense all of this makes but here's the script to clarify things. All my failed or partially succesful attempts have been taken out so it's just the base of it. Just replace the gas grenade script with it if you want to test it.

Author:  Bad Boy [ Fri Jul 06, 2012 4:25 pm ]
Post subject:  Re: Checking against all members in a table simultaneously

Bump, I know there are some very knowledgeable people here and I'd love help or at least someone letting me know if it can't be done. That said, I'm pretty sure it's doable but my brain just couldn't quite sort it out without hitting a road block of some sort and getting sidetracked.

Author:  Zuriki [ Fri Jul 06, 2012 6:22 pm ]
Post subject:  Re: Checking against all members in a table simultaneously

I don't understand the first part "it doesn't act as and."?

Author:  Bad Boy [ Fri Jul 06, 2012 11:09 pm ]
Post subject:  Re: Checking against all members in a table simultaneously

If it did it'd work. What I mean by that is
Code:
for i = 1 , #self.Masked do
   if actor.ID ~= self.Masked[i].ID

is not the same as
Code:
if actor.ID ~= self.Masked[1].ID and actor.ID ~= self.Masked[2].ID and ... actor.ID ~= self.Masked[#self.Masked].ID

and the latter is what I want.

If there's some way to do this directly by iterating over all of them simultaneously then that'd be work. Otherwise some sort of logic which'd continue the series til it reaches #self.Masked would be fine too. I don't know of the former and I couldn't (and probably still can't) quite figure out how to do the latter.

Author:  CaveCricket48 [ Sat Jul 07, 2012 5:18 am ]
Post subject:  Re: Checking against all members in a table simultaneously

Like this?
Code:
local disIsCounter = 0;
for i = 1, #self.datTable do
   if actor.ID ~= self.datTable[i].ID then
      disIsCounter = disIsCounter + 1;
   else
      break;
   end
end
if disIsCounter == #self.datTable then
   print("ACTION");
end

Author:  Zuriki [ Sat Jul 07, 2012 10:34 pm ]
Post subject:  Re: Checking against all members in a table simultaneously

No, he's saying for loops don't complete all their iterations instantaneously. Which I'm pretty sure they're supposed to, but I can't verify that...

All I could suggest is trying one of the other loops like while or repeat until, and see if you have better luck with one of those...

Author:  Bad Boy [ Sun Jul 08, 2012 12:34 am ]
Post subject:  Re: Checking against all members in a table simultaneously

I'll try your script out Cave Cricket but I don't think it's what I want, unless I'm reading it wrong
Zuriki, you're right on the money, I'm glad I didn't do too bad a job of explaining it :D And yeah, I thought it was supposed to act simultaneously too but that doesn't seem to be the case. I've tried a using while loop and manually increasing i with little success and though I've never used repeat loops I'm not sure if it'd help much either.
I guess I'll have to keep trying.

Author:  CaveCricket48 [ Sun Jul 08, 2012 2:02 am ]
Post subject:  Re: Checking against all members in a table simultaneously

If I read correctly, you want make sure that the actor's ID is not equal to any of the object IDs in the table, right?

Author:  Bad Boy [ Sun Jul 08, 2012 4:42 am ]
Post subject:  Re: Checking against all members in a table simultaneously

Yep, and if it's not a member of that table make it lose health or whatever.
Your script seems to be the same as what I put up except it serves a different purpose, i.e. increasing a count when it finds actors not in the table.
However, like mine it doesn't work properly, because
Code:
for i = 1 , #self.table do
   if actor.ID ~= self.table[i].ID then
seems to act like
Code:
if actor.ID ~= self.table[1].ID or actor.ID ~= self.table[2].ID... or actor.ID ~= self.table[#self.table].ID then


Sort of edit, since I meant to post this earlier then edit it in:
I just finished putting in your script and yeah, it doesn't seem to work, or maybe I'm trying to apply it incorrectly.

______________________________

Real edit, I figured it out, please allow me a moment to revel in my cleverness :P
Because for i = 1 etc checks or instead of and, it allows you to see if something is a member of a table. Use that to return true in a function and you can check if it doesn't return true to find what's not in the table properly. Now I feel dumb for not thinking of it last time :)
For anyone interested, here's the code:

Author:  TheLastBanana [ Sun Jul 08, 2012 5:51 am ]
Post subject:  Re: Checking against all of a table simultaneously (solved)

The only time code ever executes simultaneously is in separate threads of execution, which aren't supported in CC and certainly don't occur in a standard for loop.

Author:  Bad Boy [ Sun Jul 08, 2012 6:07 am ]
Post subject:  Re: Checking against all of a table simultaneously (solved)

After quickly doublechecking with google I have to say, fair enough, I'd be surprised if that was supported in CC's lua (since CC doesn't have multithread support at all if I'm not too confused) or occurred in a normal for loop.
I made the mistake because I was used to using a for loop to figure out if something is in a table, which made me assume it happened simultaneously. Of course if I'd thought it through I would have realised it needs to not happen simultaneously for that to work.

Author:  CaveCricket48 [ Sun Jul 08, 2012 11:52 pm ]
Post subject:  Re: Checking against all of a table simultaneously (solved)

You missed the part where it checks the counter against the number of items in the table. If the actor's ID does not match any of the IDs in the table, the counter would be equal to the size of the table. If the actor ID DOES match one of the IDs in the table, the loop breaks and the counter is not equal to the size of the table.

That means that if the actor ID matches on in the table, counter < #table
If the actor ID does not match any ID in the table, counter == #table

Anyways, glad you found your own solution.

Edit: Alternatively, and a more simple piece:
Code:
local matchNone = true;
for i = 1, #self.datTable do
   if actor.ID == self.datTable[i].ID then
      matchNone = false;
      break;
   end
end
if matchNone then
   print("ACTION");
end

Author:  Bad Boy [ Mon Jul 09, 2012 3:43 am ]
Post subject:  Re: Checking against all of a table simultaneously (solved)

Ah, I see now, that's clever and yep, I completely missed it.

The second way you posted is much the same as what I ended up doing, though more compact. I was planning on doing pretty much that but was worried about having mixups with the local variable because I was planning to define it outside of the for actor in MovableMan.Actors loop, i.e. I wasn't thinking clearly :D

Thanks for the help though, I'm sure it'll be useful to someone else somewhere down the line.

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/