Mercurial
Mercurial

Reputation: 79

Netlogo code adding people on the same patch

I'm actively trying to reprogram the sample model of Traffic 2 lanes but with my own addition, I have added what looks like a foothpath with people at the bottom, but when I run the code it adds the 4 people required sometimes on the same patch. How do I fix this?

to make-people
create-people 4 [setup-turtles] 
end

to setup-turtles  ;;Turtle Procedure
set shape "person"
let y-coordinates (list -8 -7 -6 -5)
let remove-index random length y-coordinates
set ycor item remove-index y-coordinates
set y-coordinates remove-item remove-index y-coordinates
set xcor 19 



end


The rest of the code is the same as the sample model in Netlogo under social science called Traffic 2 Lanes, with a breed of people he only other difference.

Upvotes: 2

Views: 88

Answers (1)

Charles
Charles

Reputation: 4168

The problem is that each person is defining again the y-coordinates list for their own creation. The list does not carry over from the creation of one person to the next, so one person's removing one of the items from the list will not have any affect on the list that the next person defines anew when they are created. The easiest way around this is to define y-coordinates as a global variable so that each person will work on the same list. Thus when one person eliminates a coordinate, the next person will get that shortened list. Try

breed [people person]
globals [y-coordinates]

to make-people
  set y-coordinates (list -8 -7 -6 -5)
  create-people 4 [setup-turtles] 
end

to setup-turtles  ;;Turtle Procedure
  set shape "person"
  let remove-index random length y-coordinates
  set ycor item remove-index y-coordinates
  set y-coordinates remove-item remove-index y-coordinates
  set xcor 19 
  show y-coordinates
end

The show statement will show you that the y-coordinates list is indeed being shortened by each new turtle.

Upvotes: 3

Related Questions