ID:923657
 
Code:
      if (!action&&!nest&&!waterfound)
for (var/turf/T as turf in oview(5))
if (istype(T,/turf/water))
if(!waterfound)
waterfound=1
step_away(src,T)
world<<"[src]: I see water"
waterloc+=T
action=1
if(get_dist(src,T)>=6)
var/obj/o=new /obj/rattsnest
o.loc=loc
o.owner=src
nestloc=o
nest=1
break
else
spawn
while(get_dist(src,T)<6)
step_rand(src)
sleep(metab)
var/obj/o=new /obj/rattsnest
o.loc=loc
o.owner=src
nestloc=o
nest=1
break


Problem description:

I'm not sure what's goin on here, but the mob seems to gain some aberrant behavior seemingly gaining an extra move per turn after the distance has been met and the while loop should end... And I'm sure there are better/easier ways to do this, but this is what my pea-brain came up with... Any help would be appreciated.
Some Slacker
It's probaly because you're using while() inside the for() loop.
Oh and spawn here is useless.
I thought that while would hold the for loop (in this instance) in its current loop position until the while is resolved and the it 'breaks' out of the for loop, is this not whats happening?

and spawn is useless here? why? where should it be? is it not needed or is it just being implemented by me wrongly?

To say this wasn't exactly the help I was lookin for, would be HUGE understatement.. is it possible to give negative votes? hahaha! j/k ;P
You're currently using spawn as a label, and I don't see where you use the label.

And, due to your comments on my assistance, that's the last you'll get from me.
In response to Some Slacker
Some Slacker wrote:

To say this wasn't exactly the help I was lookin for, would be HUGE understatement.. is it possible to give negative votes? hahaha! j/k ;P

Adding "haha j/k" doesn't make what you said any less rude. I had actually been spending about fifteen minutes looking at your code when you said that... I don't think that attitude inspires me to continue.

I'll give you a hint, though. Your entire methodology for going about what you are trying to do is absolutely wrong. Look up A* pathfinding, because you are essentially trying to move something randomly until you find a desired location. That's the worst thing you can ever do with pathfinding.

And yes, spawn is worthless there. It merely delays the while loop without discontinuing the for loop. You are looking for a sleep function.

Also, you really ought to explain exactly what you are trying to do. That mess of code above doesn't describe what you are trying to do at all. The problem with non-functional or buggy code, is that if it's buggy, it's difficult for even someone like me with 15+ years of programming experience to digest. The fact that it's not working as desired means that I can't tell what it's supposed to do without making some assumptions.
Well, what to say? Am I rude? Probably, psychopaths usually are. I'm sorry if you felt insulted NNAAAAHH but I too felt insulted by your response, so we are even no?

Again, I'm still new to the whole byond thing, oop, and any advances in programming since I dabbled with it way back in the early 80's.

So, I can do away with the spawn, as the sleep at the end does what I had wanted anyway? I was under the impression that spawning a while was a good idea, to create a new thread(ish) while the while finishes. Though I have no idea what I'm doing! (which is painfully obvious)

This started out as just a platform for me to try and learn some new (byond) concepts, so this program was written the exact opposite way of good programming, rather than a good top down design, I have just been doing things because I want to know how to do them. Perhaps its a general programming concept, perhaps its a way byond does things, perhaps its a more advanced programming concept, but whatever my reason for including it you have to understand it was simply me saying 'I wonder if..' kinda thing.

This code is to simply have a mob (in this case a ratt) to search around for water until he finds it, but he doesn't want to build his nest too close to the water, so once he finds it he will need to step_away a bit, so he's not in the middle of grand central (ie the path to the water) when he builds a nest, and in the while i use step_rand rather than step_away because I don't want him to just get stuck in a corner while trying to get away from the water.

Originally I simply had the ratt take one step away from the water and place his nest, and this worked fine, but often left him within view(5) of the water, and since the froggs he eats as food spawn in the water, I thought this was making it too easy for him. So I thought I would have him step away further, but then he often runs into a corner where he can't get further away, but can't meet the minimum distance either, so even though step_rand isn't optimal it will get the job done. I've watched step_rand in action for a while now and its NOT the same as what I would consider a TRUE step_rand, I think the guide or ref called it the difference between roaming aimlessly(step_rand) and roaming mindlessly(true rand) and I can handle the ratt roaming a bit aimlessly till he finds a suitable spot for his nest.

And to say that this code isn't cobbled together with little knowledge of coding, would also be a HUGE understatement, so if I can write a few if statements with some variables that can baffle someone with 15+ years programming experience, I know that I am really on the wrong path.

Still not gonna deter me from fudging something together that works, but also doesn't mean I wouldn't rather know the correct way of doing it. I simply don't know any other way than to try 10 or 400 times to get something to work and hopefully accidentally find the right way (which almost never happens).