Nelis
Nelis

Reputation: 91

Use a turtle as parameter variable in function netlogo

I'm trying to create a parameter variable of a turtle and use it for a function. In this simplified example I have two turtle breeds 'cars' and 'garages'.

breed [cars car]
breed [garages garage]

to setup
  clear-all
  create-cars 20 [
    set color red 
    set shape "car"
    set size random 4
    move-to one-of patches
  ]
  create-garages 5 [
    set color blue
    set shape "square 2"
    set size 4
  ]
end

to go
  move-cars
end

to move-cars
  ask cars with [size = 3] [
    let selected-garage one-of garages
    move-to-garage [selected-garage]       <<-- expected literal value here
  ]
end

to move-to-garage [selected-garage]
  let garageXcor [xcor] of selected-garage
  let garageYcor [ycor] of selected-garage
  setxy garageXcor (garageYcor + 3)
end

It gives an error 'expected literal value' for the parameter of the 'move-to-garage' function. How to fix this?

I know I can select the garage also in the function 'move-to-garage'. But I'd like to use the parameter for this. Is this possible?

Upvotes: 0

Views: 204

Answers (1)

LeirsW
LeirsW

Reputation: 2305

The problem with your code is your use of square brackets. You are correct in using [] while defining the move-to-garage procedure, but you don't need to use them again while calling the procedure, so the correct syntax within the move-cars procedure would be move-to-garage selected-garage.

to move-cars
  ask cars with [size = 3] [
    let selected-garage one-of garages
    move-to-garage selected-garage
  ]
end

to move-to-garage [selected-garage]
  let garageXcor [xcor] of selected-garage
  let garageYcor [ycor] of selected-garage
  setxy garageXcor (garageYcor + 3)
end

The reason for the "expected literal value" error is that one of the uses of square backets is defining lists of number or strings, which are literal values. You cannot create lists of variables (such as selected-garage) in this way. Hence the error.

Upvotes: 1

Related Questions