1

I just started using netlogo and I'm trying to transition away from OOP, so I apologize if my coding paradigm is the source of my issue.

Problem

Inside an ask turtle procedure I have hatched a turtle. I want to create a link to the hatched turtle with the turtle that hatched it. I do not need to remember family ties.

Attempts to solve the problem

ask turtles [
    setxy ( mean [pxcor] of my-territory ) ( mean [pycor] of my-territory )
    show my-territory
    let parent-node [hatch 1]
    [ set color red
      if parent-node != nobody
      [ create-link-with parent-node [ set color green ]
        move-to old-node ;; position the new node near its partner
        fd 8
      ]]]

But the hatch gives me the error the it expects a literal value. 1 is a literal, correct? What would be the best way of thinking about how to solve this?

Community
  • 1
  • 1
Gabriel Fair
  • 4,081
  • 5
  • 33
  • 54

2 Answers2

1

It looks like you want

if parent-node != nobody [
  ask parent-node [
    hatch 1 [create-link-with myself init-child]
  ]
]

where init-child holds your initializations.

Alan
  • 9,410
  • 15
  • 20
1

I'm not really clear what you're trying to do. What is parent-node? Is that the node doing the hatching? If so, you don't need it at all since the ask turtles throws you into the turtle context (ie the turtle doing the commands is the parent-node). And what is old-node? hatch will place the new turtle in the same location as the turtle hatching anyway.

If all you are trying to do is have a turtle hatch another, link the hatcher with the hatchee, and then have the child move forward, try this:

ask turtles
[ setxy ( mean [pxcor] of my-territory ) ( mean [pycor] of my-territory )      
  hatch 1
  [ set color red
    create-link-with myself [ set color green ]
    forward 8
  ]
]

Note that myself refers to whatever is doing the asking.

JenB
  • 17,620
  • 2
  • 17
  • 45