scagbackbone
scagbackbone

Reputation: 791

Can I modify NetLogo commands with variables?

To start with example:

to make-new-car [freq x y head ] if (random-float 100 < freq) and not any? turtles-on patch x y [ create-cars 1 [ setxy x y set heading head set color one-of base-colors ] ] end

yet I want to have more breeds for cars - not just one car breed. I also want to keep it simple and insted of doing this(first function is the same as one above):

to make-new-car [freq x y head ] if (random-float 100 < freq) and not any? turtles-on patch x y [ create-cars 1 [ setxy x y set heading head set color one-of base-colors ] ] end

to make-new-carSE [freq x y head ] if (random-float 100 < freq) and not any? turtles-on patch x y [ create-carsSE 1 [ setxy x y set heading head set color one-of base-colors ] ] end

and being redundant by repeating the same procedure just with different breed name I wan to do something like this(put breed-name as an argument and use it with create- command):

to make-new-car [freq x y head breed-name] if (random-float 100 < freq) and not any? turtles-on patch x y [ create-breed-name 1 [ setxy x y set heading head set color one-of base-colors ] ] end

However Netlogo is complaining that create-breed-name is not defined. Any ideas ?

Upvotes: 3

Views: 172

Answers (2)

JenB
JenB

Reputation: 17678

Easiest way to do this is to do create-turtles then set breed breed-name. Here's an example.

breed [ testers tester ]

to make-turtles [ breed-name ]
  create-turtles 1 [ set breed breed-name ]
end

to setup
  make-turtles testers
end

You can also do probably do something with run after constructing an appropriate string, but I think the above is more straightforward.

Upvotes: 6

Nicolas Payette
Nicolas Payette

Reputation: 14972

Go with Jen's answer. It is, by far, the most straightforward way to accomplish what you need.

Just for the sake of it, however, here is one way to do it with run:

to make-new-car [freq x y head breed-name]
  let commands [ ->
    setxy x y
    set heading head
    set color one-of base-colors
  ]
  if (random-float 100 < freq) and not any? turtles-on patch x y [
    run (word "create-" breed-name " 1 [ run commands ]")
  ]
end

Notice that I put the commands that should be executed by the newly created turtle inside an anonymous procedure (using NetLogo 6.0.1 syntax) and then run that inside the string passed to run. You could also put everything in one big string, but then you would loose compiler checking, syntax highlighting, speed, etc.

But, anyway, don't do any of this. Use Jen's method.

Upvotes: 2

Related Questions