user3005072
user3005072

Reputation: 159

NetLogo: create a dynamic number of breeds

how can i create a dynamic number of breeds at runtime? I want my user to be able to choose the amount of breeds.

I thought about something like

to setup_breeds

  let j 1

  while[j <= n_groups][

    breed[j]

  ]

end

where n_groups is the number of breeds whick is taken from a slider. But unfortunatly i cannot use breed inside a method...

Any ideas?

Thanks!

Upvotes: 3

Views: 422

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

You need to explicitly declare each breed with the breed keyword, so the short answer is: no, you can't have a dynamic number of breeds.

But do you really need actual NetLogo breeds? The main purpose of having different breeds is to have different variables for each breed. If that is not case, perhaps you can get away with having a group-id turtle variable. To create a certain number of turtles for each of n_groups, you could just do something like:

turtles-own [ group-id ]

to setup
  clear-all
  let n_groups 10
  let n-turtles-per-group 5
  foreach n-values n_groups [ ? ] [
    create-turtles 10 [ set group-id ? ]
  ]
  ask turtles [ set label group-id ]
  ; do something with only turtles of, e.g., group 2:
  ask turtles with [ group-id = 2 ] [
    fd 5
  ]
end

If you think you really need breeds, edit your question to tell us why, and we'll see if we can find a solution for you.

Side note:

I used foreach n-values n_groups [ ? ] to loop through your n groups. That's the equivalent of:

let i 0
while [ i < n_groups ] [
  set i i + 1
]

...but arguably more "NetLogo-ish".

Upvotes: 4

Related Questions