<aside> 🧟 This page is still a draft!

</aside>

In almost all cases you should not assign a new value to a variable that stores a p5.party shared object. Let’s look at an example.

Imagine we want to share a single point: an x and a y value.

We might do this (wrong):

let shared;
function preload() {
  partyConnect(server_url, "hello_party");
  shared = partyLoadShared("shared");
}

function setup() {
  shared = {x: 10, y: 10} // WRONG!!!
}

This code runs, but won’t do what we want. Here is what it does do:

  1. It declares shared in the global scope.
  2. In preload() it loads a shared object with partyLoadShared() and assigns that object to shared.
  3. In setup() it creates a new object (that isn’t shared) with x and y properties and assigns that new object to the shared.
  4. Shared now holds to the new object and nothing holds to the shared object.

<aside> 🧟 Since there are no variables that hold the shared object, it will get garbage collected.

</aside>

We might do this (right):

function preload() {
  partyConnect(server_url, "hello_party");
  shared = partyLoadShared("shared");
}

function setup() {
  shared.x = 10; // RIGHT!!!
  shared.y = 10; // RIGHT!!!
}