Search code examples
netlogo

Netlogo: "expected a literal value" in a "ifelse" block


I want to check if the turtle is a sheep or wolf, and set commands seperately. But Netlogo highlights the set and tells me

expected a literal value

Here is edited code, I added more basic information and did some simplification for quick understanding.

breed [sheep a-sheep]
breed [wolves wolf]


sheep-own [SEnergy]
wolves-own [WEnergy]

to setup
  clear-all
  create-sheep 100
  ask sheep [set SEnergy 100]
  
  create-wolves 100
  ask wolves [set SEnergy 100]
  
  reset-ticks
end

to go
  ask turtles[
    (ifelse
      is-a-sheep? [set SEnergy SEnergy - 1]
      is-wolf? [set WEnergy WEnergy - 1])
  ]
  tick
end

I've red the example in Netlogo dictionary

(ifelse boolean1 [ commands1 ] boolean2 [ commands2 ] ... [ elsecommands ])

Thus I think [set SEnergy SEnergy - 1] in my code is expected a command. Why Netlogo tells me a literal value is needed?

Thanks in advance.


Solution

  • The problem lies in your syntax for the is-a-sheep? and is-wolf? statements. is-breed? takes a single argument, the identity of the agent you are testing. Your code should therefore be

    to go 
      ask turtles[
        (ifelse
          is-a-sheep? self [ set SEnergy SEnergy - 1 ]
          is-wolf? self [ set WEnergy WEnergy - 1 ])
      ]
    end
    

    Then each turtle is asking itself what it is.

    Of course, you could just ask each agentset separately.

    ask sheep [set SEnergy SEnergy - 1]
    ask wolves [set WEnergy WEnergy -1]
    

    Charles