Search code examples
netlogoagent-based-modeling

Writing an if statement depending on an attribute of an agent to command agents of a different agentset


I am trying to make an agentset do something, if an agent(of a different agentset) has a particular shape.

Here, if the shape of a particular

  • ghost (say Ghost 1) is circle,

  • then all rabbits are supposed to move forward 1. (<-This is the intended behavior)

where

  • ghosts are agentset A
  • rabbits are agentset B

I have tried along these lines:

ask rabbits
[
 if (shape ghost 1 = "circle")
  [
   forward 1
  ]
]

For this code I get,

"Expected a closing paranthesis here."

with a highlighter on ghost. I'm aware that this code is wrong but I can't think of how else this should be written to get the desired result.


Solution

  • This will (I think - can't test) get the syntax correct:

    ask rabbits
    [
     if ([shape] of ghost 1 = "circle")
      [
       forward 1
      ]
    ]
    

    but you also have an ordering error and will have every rabbit check the shape of chost 1. I think what you really want is:

    if ([shape] of ghost 1 = "circle")
    [ ask rabbits
      [ forward 1
      ]
    ]