Created
October 22, 2013 17:35
-
-
Save bennlich/7104763 to your computer and use it in GitHub Desktop.
die() behavior with while loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var count = 0; | |
while (agent.patchRightAndAhead(0,1).isWall()) { | |
agent.rotate(Math.PI/2); | |
count++; | |
if (count >= 3) { | |
agent.die(); | |
} | |
} |
Makes a lot of sense, and indeed I seem to run into it too. Yet another reference vs value thing.
Unfortunately, agent is a local variable and there's no way for AS or another program to change it. AS could convert it to a "phantom" agent, i.e. the reference "agent" could somehow hollow itself out after the die call, then the GC would eventually get rid of it.
Wait tho, I see what you're saying .. is there a way to delete an instance so to speak. Have you tried it in a snippet? It is a cool idea.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, Owen. This is a snippet of code that kills an agent if, after four 90 degree rotations, the agent finds itself surrounded by walls.
I would expect to get an error at line 2, regarding the attempt to call
patchRightAndAhead()
of a now undefined object (what used to beagent
). Instead, the reference toagent
is still around, and I get an error when it tries to kill it a second time around (since it no longer exists in the agentset).Could this be changed by adding a
delete this
toAgent.prototype.die
? Is it unfixable? Is my expected behavior silly?