- Home /
Take only one action ~ if(A = var[i]) reveals a true
Sorry noob question! I'm comparing a gameobject transform to all transforms in an array. if not equals to any values in array, i just want to take an action 1 time, i only know how to do it as many times as i do the comparison:
for (var marker in markers)
{
if ( pos = marker) //return true if 1+ value is same and do just one
{Instantiate(obj,pos,rot);}
untested theory
for (var i:int = 0; i < markers.length; i ++)
{
if ( markers[i] == pos )
{
Instantiate(obj,pos,rot);
i = markers.length + 1; // fall out of the loop
}
}
I think the keyword you're looking for is "break". Try putting a "break;" right after your Instantiate call to quit out of the loop.
sure you don't mean
==
rather than
=
here's how you write such a function
function doesNOTcontain() // return TRUE if NO matches
{
for each of them ...
{
is it a match? if so return FALSE
}
// here, it must be that there are NO $$anonymous$$ATCHES, so...
return true;
}
it's sometimes hard to hop in to program$$anonymous$$g unless you have a deep background in, well, program$$anonymous$$g! $$anonymous$$eep it up!
it takes years and years and years and years to absolutely master all these maybe 1000 basic algorithms you must "see" instantly to work
@alu dude, you totally have to get with BREA$$anonymous$$ and CONTINUE
very confusing if you are new, since you always mix 'em up.
you should never, ever write this inline though.
make a little function that returns (it "breaks" with a return, essentially)
(of course, your comp sci professor told you "only ever have one point of return!". he's totally correct, but he can also go to hell.)
"I am self-taught" michael jackson, paul mccartney and mozart are self-taught so don't worry about that.
"surprised I have never seen it in one of the eleventeen million loops I have read..." that's because everything on the internet is crap :)
Answer by AlucardJay · Oct 03, 2012 at 08:27 PM
With great help from @mikebelotti :
for (var i:int = 0; i < markers.length; i ++)
{
if ( markers[i] != pos )
{
Instantiate(obj,pos,rot);
break; // fall out of the loop
}
}
alu dude (I THIN$$anonymous$$) your answer is THE OPPOSITE of what he's asking
"if NOT equals to ANY values in array" ....
i THIN$$anonymous$$ that's what the bloke meant
what this site needs is a £$@ing proofreader -- any language would do! -- for new questioners! heh
Thanks everyone! i programmed DSP with native instruments Reaktor, then dragon naturally speaking automation macro's, then winamp milkdrop presets, and i was always confused by program$$anonymous$$g, and now i can read it O$$anonymous$$ although and can write some kinds of games thanks to all you! i am just makign a dungeon generator it's to compare if the tunneller is in a new space in the array :)
You are absolutely right, reading the question rather than going off the code reveals this. I think I was thrown by the same thing as you mentioned, (is equal to only represented as = , but it now looks like it was is not equal to != )
I shall amend the answer accordingly, thankyou for your help. Regarding proofreading, am a bit blurry-eyed at this hour so that would be quite handy =]