goodgest
goodgest

Reputation: 418

NetLogo: Can I write a syntax that allows a turtle to be die and a new turtle jump into the same specific patch at the same time within 1 tick?

Can I control to be die one turtle in a specific patch (max-pxcor 0) and then jump another turtle into a specific patch (max-pxcor 0) at the same time within 1 tick time smoothly? I want to keep the rule the specific patch (max-pxcor 0) is a condition where only one turtle can exist (This rule has been resolved in another case. So the sample codes has been omitted this syntax). The following is a part of sample syntax, but it doesn't work. In the case of this syntax, turtles can not be dead and moving at the same time within 1 tick time. It feels like the turtle is dead and moving within 2 ticks or else. Does the move-to syntax cause this problem?

      if (count turtles-on patch max-pxcor 0 = 1)[
      ask turtles-on patch max-pxcor 0 [ die ]]
      ask turtles-on patch (max-pxcor - 1) 0 [
      move-to patch max-pxcor 0 ]
      tick

Upvotes: 1

Views: 92

Answers (1)

JenB
JenB

Reputation: 17678

Here is your code with the brackets repositioned so you can see the logic blocks.

if (count turtles-on patch max-pxcor 0 = 1)
[ ask turtles-on patch max-pxcor 0
  [ die
  ]
]
ask turtles-on patch (max-pxcor - 1) 0
[ move-to patch max-pxcor 0
]
tick

The description of your problem is hard to understand. You suggest that the turtles are moving in different ticks. That can't happen with your code. But I can see a couple of things that could be wrong.

From looking at the code, I think you are probably getting a lot of turtles on that last patch. If that is the problem, the reason it is happening is because you move the turtles on the second last patch regardless of the if condition. The ask that moves the turtles is not within the brackets.

The other potential problem is that your if condition only operates if the number of turtles is exactly equal to 1. As soon as you have more than 1 turtle on that last patch, no turtle will ever die, they will just keep on moving onto the last patch.

In combination, does this fix the problem?

if (count turtles-on patch max-pxcor 0 >= 1)  ; changed the = to >=
[ ask turtles-on patch max-pxcor 0
  [ die
  ]
  ask turtles-on patch (max-pxcor - 1) 0     ; moved this inside the [ ]
  [ move-to patch max-pxcor 0
  ]
]
tick

Upvotes: 2

Related Questions