Skip to content

Instantly share code, notes, and snippets.

@bennlich
Created October 22, 2013 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennlich/7104763 to your computer and use it in GitHub Desktop.
Save bennlich/7104763 to your computer and use it in GitHub Desktop.
die() behavior with while loop
var count = 0;
while (agent.patchRightAndAhead(0,1).isWall()) {
agent.rotate(Math.PI/2);
count++;
if (count >= 3) {
agent.die();
}
}
@bennlich
Copy link
Author

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 be agent). Instead, the reference to agent 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 to Agent.prototype.die? Is it unfixable? Is my expected behavior silly?

@backspaces
Copy link

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