NetLogo GbCC 3.0 Code Snippets

Hello World

NetLogo Code

to gbcc-on-enter [ user-id role ]
  show (word "Hello world. User " user-id " entered.")
end

Share Global Variables

NetLogo Code

to share
  gbcc:broadcast-plot "populations"
  gbcc:store-globals
end

to gbcc-on-select [ user-id role ]
  gbcc:restore-globals-from-user user-id
end

Model with Sample Code

Wolf Sheep

Use Direction Buttons to Move Turtle and it's GeoGebra Point

NetLogo Code

to setup
  ;; note choose one of the following
  graph:show-graph [ ] ;; if you want to show the default graph
  graph:import-file "geogebra-default.ggb" ;; if you want to a specific ggb file
end 

to create-turtle-and-point
  create-turtles 1 [
    set my-turtle-number who
    set patch-coords (list xcor ycor)
    set graph-coords graph:patch-to-graph patch-coords
    ;; point names must only contain letters
    graph:create-point "mypoint" graph-coords
  ]
end

to go-up
  go-forward 0
end

to go-down
  go-forward 180
end

to go-left
  go-forward 270
end

to go-right
  go-forward 90
end

to go-forward [ direction ]
  ask turtle my-turtle-number [
    set heading direction
    fd 1
    set graph-coords graph:patch-to-graph (list xcor ycor)
    graph:set-xy "mypoint" graph-coords
  ]
end

Model with Sample Code

Triangle

Share Code and Run It

NetLogo Code

to share
  gbcc:broadcast-view "my-view"
  gbcc:set "code" my-code
end

to gbcc-on-select [ user-id role ]
  set code-example gbcc:get-from-user user-id "code"
end

to run-code
  run code-example 
end

Model with Sample Code

Catch A Planet

Turtles Walk Around at the Same Time

NetLogo Code

to setup 
  create-turtles 1 [ 
    set my-turtle-number who 
    gbcc:set "shape" shape
    gbcc:set "color" color	
    gbcc:broadcast-avatar shape color ""
  ]
end

to go-up
  go-forward 0
end

to go-down
  go-forward 180
end

to go-left
  go-forward 270
end

to go-right
  go-forward 90
end

to go-forward [ direction ]
  ask turtle my-turtle-number [
    set heading direction
    fd 1
    gbcc:set "xcor" xcor
    gbcc:set "ycor" ycor 
  ]
end

to gbcc-on-select [ user-id role ]
  if (user-id != gbcc:who-am-i) [	
    create-turtles 1 [
      set id user-id
      set shape gbcc:get-from-user user-id "shape"
      set color gbcc:get-from-user user-id "color"
      set xcor gbcc:get-from-user user-id "xcor"
      set ycor gbcc:get-from-user user-id "ycor"
    ]
  ]
end

to gbcc-on-deselect [ user-id role ]
  if (user-id != gbcc:who-am-i) [
    ask turtles with [ id = user-id ] [ die ]
  ]
end

to gbcc-on-go [ user-id role ]
  ask turtles with [ id = user-id ] [
    set xcor gbcc:get-from-user user-id "xcor"
    set ycor gbcc:get-from-user user-id "ycor"
  ] 
end

Add forces to a bristlebot

NetLogo Code (NOT TESTED)

to setup
  physics:create-rectangle "belly" "my-bristlebot"
  physics:set-body-xy "my-bristlebot" [ 5 5 ]
  physics:set-rectangle-relative-coords "belly" [ 0 0 ] [ 4 4 ]
  physics:create-polygon "head" "my-bristlebot"
  physics:set-polygon-coords "head" [ [ -1 -1 ] [ 0 0 ] [ 1 1 ] ]
  physics:set-behavior "my-bristlebot" "dynamic"
  physics:create-target "target-0" "my-bristlebot"
  physics:set-target-relative-xy "target" [ 0 0 ]
end

to apply-force 
  physics:apply-force-relative-angle "target-0" my-angle my-force
end

to shift-target-up
  let center physics:get-target-relative-center "target-0"
  physics:set-target-relative-center "target-0" (list item 0 center (item 0 center + 1) )
end

to shift-target-down
  let center physics:get-target-relative-center "target-0"
  physics:set-target-relative-center "target-0" (list item 0 center (item 0 center - 1) 
end

to go
  every 0.1 [
    tick
  ]
end

Import Data, Adopt Canvases

NetLogo Code

;; To export data, press the "Data" button in the top right corner.
;; You will download a zip file that includes our-data.txt 

;; To import data, you can use the following code which will prompt you to pick a file.
;; Choose an exported file like our-data.txt 
to import 
  gbcc:import-our-data
	auto-assign-vacant-canvases
end 

;; auto assign any existing students to vacant canvases
to auto-assign-vacant-canvases
  foreach gbcc:get-active-user-list [ 
    x -> 
    if (member? x gbcc:get-canvas-list = false) [
      if (length  gbcc:get-vacant-indices > 0) [
        let random-id random length gbcc:get-vacant-indices
        gbcc:adopt-canvas x item random-id gbcc:get-vacant-indices
      ]
    ]
  ]
end

;; when a new user enters, assign a vacant canvas, if it exists
to gbcc-on-enter [ user-id role ]
  if (user-id  = gbcc:who-am-i and length gbcc:get-vacant-indices > 0) [
    ;; choose a random vacant canvas
    let random-id random length gbcc:get-vacant-indices
    ;; assign new user to that vacant canvas
    gbcc:adopt-canvas user-id item random-id gbcc:get-vacant-indices
  ]
end

Model with Sample Code

Import Data and Adopt Canvas Demo

Share Geogebra Worlds

NetLogo Code

	
extensions [ gbcc graph ]

to show-graph 
  graph:show-graph
end

to share 
  gbcc:broadcast-text "my-name" my-name 
  gbcc:broadcast-view "my-view"
  gbcc:store-state
end 

to gbcc-on-select [ user-id role ]
  gbcc:restore-state-from-user user-id
end

to gbcc-on-deselect [ user-id role  ]
  gbcc:restore-state 
end

Update Turtles to match Map

NetLogo Code

to update-turtles
  graph:update-graph
  ask turtles [
    let graph-coords graph:get-xy point-name
    let patch-coords graph:graph-to-patch graph-coords 
    setxy item 0 patch-coords item 1 patch-coords
  ]
end

Model with Sample Code

Graph Test

Share Lists of Points

NetLogo Code

to share 
  ;; Share points, but make them unique. I prefixed my points with "butterfly".
  let points (list) 
  let new-name ""
  let new-point (list)
  foreach graph:get-points [ x ->	
    set new-name (word "butterfly" item 0 x)
    set new-point (list new-name item 1 x)
    set points lput new-point points
  ]
  gbcc:set "my-points" points
	gbcc:broadcast-view ""
end 

to gbcc-on-select [ user-id role ]
  let points gbcc:get-from-user user-id "my-points"
  graph:create-points points
end 

to gbcc-on-deselect [ user-id role ]
  let points gbcc:get-from-user user-id "my-points"
  foreach points [ x -> 
    graph:delete-object item 0 x
  ]
end

Shadows on a Map

NetLogo Code

to setup
  create-turtles 1 [
    set turtle-number who
    set marker-name word "marker-" who
    set latlng maps:patch-to-latlng (list xcor ycor)
    maps:create-marker marker-name latlng		
    set shadow-length 3
    set size shadow-length
  ]
end

to update-shadow [ turtle-number length-of-shadow ]
  ask turtle turtle-number [
    set shadow-length length-of-shadow 
    set size shadow-length
  ]
end

to update-position [ turtle-number new-xcor new-ycor ]
  ask turtle turtle-number [
    setxy new-xcor new-ycor 
    set latlng maps:patch-to-latlng (list xcor ycor)
    maps:set-latlng marker-name latlng
  ]
end

to share
  set marker-shadow-list (list)
  let data (list)
  ask turtles [
    set data (list marker-name (maps:get-latlng marker-name) shadow-length)
    set marker-shadow-list lput data marker-shadow-list
  ]
  gbcc:set "marker-shadow-list" marker-shadow-list
end

to gbcc-on-select [ user-id role ]
  set marker-shadow-list gbcc:get-from-user user-id "marker-shadow-list"
  foreach marker-shadow-list [ x ->
    create-turtles 1 [
      set turtle-number who
      set marker-name item 0 x
      let latlng item 1 x 
      let shadow-length item 2 x 
      set patch-coords maps:latlng-to-patch latlng
      setxy item 0 patch-coords item 1 patch-coords
      maps:create-marker marker-name latlng
      set size shadow-length
    ]
  ]
end

to gbcc-on-deselect [ user-id role ]
  set marker-shadow-list gbcc:get-from-user user-id "marker-shadow-list"
	foreach marker-shadow-list [ x ->
    ask turtles with [ marker-name = item 0 x ] [
      maps:delete-marker marker-name
      die
    ]
  ]
end

;; click on the Export button, saves data from each student saved with gbcc:set 

;; click on the Import button, import gbcc file and create canvases for each user, with all data from gbcc:get