Netlogo: Says its in turtle context when it is not - netlogo

I have to make a button that colors in the patches in the bottom half of the grid randomly with darker shades of a given color based off of a slider, and the top of half of the grid randomly with lighter shades of a given color based off of the slider. When I wrote my code it says that I can't use my command because it is in patch-context because by command is in turtle-only context. I am confused because I am not using turtles, I am only using patches. Inside of my button I wrote "colorRandomShade2" which is the name of my command. The button is also in patch context and its display name is "colorRandomShade2".
Code:
to colorRandomShade2
if ycor > 0 [ set pcolor ( main_color + random 5 ) ]
if ycor < 0 [ set pcolor ( main_color + random 9 ) ]
end

Patch coordinates are pxcor and pycor, but you have used xcor and ycor, which are the variable names for coordinates of a turtle. So you are pressing the button to call the code and the first thing that NetLogo sees is a request to look at the ycor or y-coordinate of a turtle.

Related

How to set patch color in the diagonal Netlogo

Want to draw a river diagonal to the center of the of the world, however ive found that i would have to do a list with all the patches i want to color, is there a faster way to color the diagonal?
Thanks
I need this diagonal, form the middle of the map upwards, Its even tougher to me as its going backwords and doesnt end in the top most middle.
This code:
ask patches with [pxcor = pycor] [
set pcolor green
]
Draws this:
And here's a version that does the diagonal in the upper right corner:
ask patches with [pxcor + pycor = max-pxcor] [
set pcolor green
]

Coloring a route in Netlogo

I want some of my turtles to leave at trace of the steps they make. I want them to change the color of the patches they pass by on their move. Similar to what the "pen-down" command does - just with the effect, that the patches change color. It is esay for me to change the color of the patch, the turtle reach - but I want all the patches colored - like if you were walking on a lawn, and the grass under you momentaneouesly turns red :-) Nut just the steps with every tick, but the continous route.
Is there a way? I would be glad to se a small coded example. Thanks very much - this homepage does a great job.
Magellancruiser
Here is one basic solution. The key is realizing that instead of having the turtle do something like forward (random 10) + 1 you can instead have it go forward 1 a number of times equal to (random 10) + 1. Because the distances are based on path sizes (1 = 1 patch across), if you color the patches as you go forward 1 at a time, you should "draw" the color on the patch.
turtles-own [ my-color ]
to setup
clear-all
create-turtles 10 [
set my-color color ; the turtles will have random colors, store them to use later
set color white ; but they're easier to see if they're white
]
reset-ticks
end
to go
ask one-of turtles [
left (random 50) - 25 ; wiggle a bit to not just go in a straight line
let d (random 10) + 1 ; the turtle will move 1 to 10 steps
repeat d [
forward 1
set pcolor my-color ; the turtle can directly set the patch's pcolor variable to its own
]
]
tick
end
You can either use setup and go from the command center or add buttons for them.

Netlogo: Creating a square area of patches in the center of the grid

I am trying to create a square of brown patches centered at the origin of my grid. Previously, I had a slider on the interface called "sink-patch-radius" that goes from 0 to 20. Then in my code, I created a circular set of patches centered at the origin that were colored brown + 2 and had a radius of "sink-patch-radius", and the surrounding patches were green. Here is the code that worked for that (thank you to JenB for this!):
;;create the 'sink'
let sink-centre patch 0 0
set sink-patches [patches in-radius sink-patch-radius] of sink-centre
ask sink-patches [ set pcolor brown + 2 ]
;; create the 'source'
set source-patches patches with [pcolor != brown + 2]
ask source-patches [ set pcolor green ]
So now, I want that idea to stay the same, but instead of a circular "sink area", I want it to be a square. I know the above code will have to change, maybe even quite a bit, because you cannot use "in-radius" to make a square. I'm thinking of maybe changing the slider to be "sink-patch-length" so that it adjusts the length of the sides of the square. My question would then be: how do I incorporate that into my code, so that I get a square of brown patches centered at the origin? For example, if "sink-patch-length" is set to 20, then I would want a 20x20 square (400 cells) centered at the origin to have a pcolor of brown + 2.
Any help is greatly appreciated! Thank you.
Use the inbuilt coordinate system. So you want (something like) patches with [abs(pxcor) <= sink-patch-length and abs(pycor) <= sink-patch-length]. If you want it centred somewhere other than the middle, you will need to do some fiddling to make the boundaries correct.

How can I simulate a moving patch?

I'm trying to make a Frogger-like game where the patches move around and the turtle can only move onto a safe patch. If I have a few patches that are red, how can I 'move' them around as if they were turtles? I currently have this, but it seems to move more than 1 patch at a time occasionally and as a result, some red patches will be destroyed if there are more than one that is red:
if pcolor = red
[ ask patch-at 0 1
[ set pcolor red]
set pcolor black ]
You could ask the neighbor patch (left or right) to be painted red and the actual patch to be painted back to black or whatever the default color is.
For this you'll want to get the actual patch coordinates. Using patch-at asks the patch at 0,1 relative to the whole world.

Dynamic charting in Netlogo

In my model, the number of turtle is dynamic based on the value defined by the user using a slider. The slider can take values between 2 and 10. Each turtle has its own set of co-ordinates and characteristics, hence i used the following code to create them.
create-parties 1
[set color red set label-color red set label who + 1 set size 3 setxy party1-left-right party1-lib-con ]
create-parties 1
[set color green set label-color red set label who + 1 set size 3 setxy party2-left-right party2-lib-con ]
if Num-of-parties >= 3
[ create-parties 1
[set color blue set label-color red set label who + 1 set size 3 setxy party3-left-right party3-lib-con ] ]
I have repeated the above till Num-of-parties =10.
In one of the modules i have created a condition where if a certain value of the turtle reaches 0 it will die.
In the later part of the model i have used set-current-plot to create a chart using the below code:
set-current-plot "Voter Support"
set-current-plot-pen "Party1"
plot 100 * [my-size] of turtle 0 / sum[votes-with-benefit] of patches
set-current-plot-pen "Party2"
plot 100 * [my-size] of turtle 1 / sum[votes-with-benefit] of patches
if Num-of-parties >= 3 [ set-current-plot-pen "Party3"
plot 100 * [my-size] of turtle 2 / sum[votes-with-benefit] of patches ]
so on and so forth for all ten possible turtles.
The problem is if the user has defined 5 turtles and turtle 3 dies at tick 10, then chart portion of the code is throwing an error since there is no turtle 3 but num-of-turtles slider defined by the user has a value of 5.
Please advise on how to solve this. Thanks, appreciate the help.
Regards
When writing model code, you should try to apply the DRY principle: Don't Repeat Yourself. Creating each turtle separately and then trying to do something with each of them by addressing them separately as turtle 0, turtle 1, etc. will lead to all sorts of problems. What you're experiencing with plotting is only the tip of the iceberg.
Fortunately, NetLogo gives you all the facilities needed to deal with a "dynamic" number of turtles. ask is the primitive you will use most often for this, but there are plenty of other primitives that deal with whole agentsets. You can read more about agentsets in the programming guide.
In the case of plotting, you can ask each of your parties to create a "temporary plot pen". We'll use the who number to give a unique name to each of these pens. (That's one of the very few legitimate uses of the who number in NetLogo.)
Put this code in the "Plot setup commands" field of your plot:
ask parties [
create-temporary-plot-pen (word "Party" (who + 1))
set-plot-pen-color color ; set the pen to the color of the party
]
(Note that you won't need the plot pens that you previously defined anymore: you can just delete them. New plot pens will be dynamically created every time you set up your plot.)
To do the actual plotting, we can use very similar code. Put this code in the "Plot update commands" field of your plot:
ask parties [
set-current-plot-pen (word "Party" (who + 1))
plot 100 * my-size / sum [ votes-with-benefit ] of patches
]