Waseem Adil
Waseem Adil

Reputation: 35

Initialization and termination of Turtles timer

In a procedure I want to start a timer for every turtles (agent) which when change its shape from "shape2" to "shape1", and that timer expire after 10 ticks and the shape changes back to "shape1" . my procedure works only when I hit "go" it works for only the first 10 ticks counted. after that it is not called. I have called this procedure name "Change" in the GO block.

to change
    let test one-of breed-here with [ shape = "shape2" ]
    if test != nobody and [ ticks ] of test = 10
    [ask breed with [ shape = "shape2" ]
        [ set shape "shape1" ]
    ]
end

the GO block statement is :

to Go
ask breed with [ shape = "shape2" ] [ change ]
end

Upvotes: 1

Views: 51

Answers (1)

Alan
Alan

Reputation: 9610

Here is an illustration using patches. (Colors stand in for shapes.)

patches-own [shape-timer]
globals [s1 s2]

to setup
  ca
  set s1 blue     ;"shape" 1
  set s2 red      ;"shape" 2
  ask patches [set pcolor one-of (list s1 s2)]
end

to temp-change-shape
  set pcolor s2
  set plabel "temp"
  set shape-timer 10
end

to update
  set shape-timer (shape-timer - 1)
  if (shape-timer = 0) [
    set plabel ""
    show "changing back!" 
    set pcolor s1
  ]
end

to go
  ask patches with [pcolor = s2 and shape-timer > 0] [
    update
  ]
  ask one-of patches with [pcolor = s1] [
    temp-change-shape
  ]
end

A nicer solution uses the table extension, mapping dates (ticks) to the agents that need to be updated at each date. (That way you don't have to check each agent each tick to find out if it's time to update it.)

Upvotes: 1

Related Questions