set a custom turtle variable

198 views
Skip to first unread message

Simone Gabbriellini

unread,
May 30, 2012, 5:42:31 AM5/30/12
to netlog...@googlegroups.com
Hello List,

how can I specify a turtle variable like turtle-own [myvariablename]?

I see that turtle.setTurtleVariable(1, "red") sets the color of the
turtle... is there any table with all variables numbers? How can I add
a new variable then?

thanks for your advices,
Simone

--
Dr. Simone Gabbriellini

DigitalBrains srl
Amministratore
Head R&D

mobile: +39 3403975626
mail: simone.ga...@gmail.com

Simone Gabbriellini

unread,
Jun 1, 2012, 4:29:10 AM6/1/12
to netlog...@googlegroups.com
Hi,

I think I have figured out how to set a variable value:

Turtle turtle = world.createTurtle(breed, 10, random.nextInt(360));
int turtlesOwnIndexOf = world.turtlesOwnIndexOf("MYVARIABLENAME");
turtle.setTurtleVariable(turtlesOwnIndexOf, myvalue);

still have a problem about how to add my turtle custom variable, in
order to retrieve its index in the array of turtle variables. I think
this should be done before, like in NetLogo when I specify turtles-own
[myvariablename], but I cannot locate the right method to accomplish
this. Is have tried

world.breedOwns(breed, "MYVARIABLENAME");

which resemble the NetLogo turtles-own, but that is just a boolean
which tells if the variable already exists or not... do you have any
hint?

thanks,
Simone

2012/5/30 Simone Gabbriellini <simone.ga...@gmail.com>:

Seth Tisue

unread,
Jun 4, 2012, 1:45:41 PM6/4/12
to netlog...@googlegroups.com
>>>>> "Simone" == Simone Gabbriellini <simone.ga...@gmail.com> writes:

Simone> still have a problem about how to add my turtle custom
Simone> variable, in order to retrieve its index in the array of turtle
Simone> variables. I think this should be done before, like in NetLogo
Simone> when I specify turtles-own [myvariablename], but I cannot
Simone> locate the right method to accomplish this.

I don't think there's a straightforward way to do this. The turtles-own
stuff is set up at compile time by compiler.StructureParser and then
initialized at runtime by World.realloc(), and hooks to modify their
behavior don't exist.

If your extension just needs to associate some additional information
with each turtle, and you don't actually need it to be visible in model
code as a real turtle variable, then I'd suggest using a
WeakHashMap<Turtle, ?>. ("Weak" so it won't prevent a turtle from being
garbage collected after it dies.)

Your extension could include turtle primitives to read and set the map
entries, at which point you'd have something very like a turtle
variable.

If you really want/need to add an actual turtle variable, you could try
looking at StructureParser and World.realloc(), see what they do, and
try to duplicate the parts you need.

--
Seth Tisue | Northwestern University | http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/

Sven Anderson

unread,
Jul 6, 2020, 12:59:54 PM7/6/20
to netlogo-devel

In an extension I'm wanting to extend the patch variable so that each patch will have additional data stored with it.  
This is like patches-own [myvariablename], but built into the extension.  Below I found this response, which makes it look fairly difficult.  However, the reply is from 2012, and I'm wondering whether this is still handled the same way, or has it become an easier change?

Thanks!
-sven  

Jeremy Baker

unread,
Jul 6, 2020, 3:09:52 PM7/6/20
to Sven Anderson, netlogo-devel

Hey Sven,

 

If you just want to store extra data for a patch, but you don't need that data available in Netlogo code as a normal patches-own variable, it isn't too hard.  As Seth said in his answer, you just need to make a map from patches to your new data to store in your extension.  Off the top of my head, the Physics extension does something like this to store physics data for patches and turtles:  https://github.com/Loafie/netlogo_phys_extension/blob/master/src/PhysExtension.scala#L29

 

I should point out you can still give access to the data in NetLogo code, just not as `[my-var] of patch 0 0` but using a custom reporter defined by your extension, like `my-ext:my-var-of patch 0 0`. 

 

If you do want to let NetLogo code access your custom patch data like a totally normal patches-own variable, then that would still be a pretty involved process, which Seth laid out the very basic steps of.

 

Another option that might work would be to have a way to let the users register a patch variable as the one for the extension to store data in, `my-ext:set-patch-var "my-var"`.  This would let the variable be referenced as normal in code, and you're extension could use the user-provided value for whatever its doing.  One downside here is I think "my-var" would have to just be a string, meaning it won't be compile-time checked for correctness.

 

I hope that helps.

 

-Jeremy

--
You received this message because you are subscribed to the Google Groups "netlogo-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-deve...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-devel/d5df52d3-d460-4b7d-94e3-06ab5cf8107ao%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages