The Spark State library introduces a solution to manage effects' states by creating globally synchronized data signals and making them available within an effect's JavaScript.
- Download or upgrade to Spark AR Studio v128 or higher.
- Open your project in Spark AR Studio.
- Open the AR Library from within the Assets panel and select the Script Packages tab.
- Import the
spark-state
package to the project. - In the project's Properties, add the Scripting Writeable Signal Source capability.
-
Add a new Javascript script to the project from the Assets panel, or open an existing one.
-
At the top of the script, load the module using the following line of code:
const State = require('spark-state');
-
The current implementation also requires that you load the
Multipeer
andParticipants
modules in your script in order to enable the two associated capabilities:const Multipeer = require('Multipeer'); const Participants = require('Participants');
GlobalCounterSignal
is a wrapper object for the ScalarSignal
class from the Spark AR API's ReactiveModule
. However, the scalar value contained by the signal is synchronized globally across all peers in a multipeer effect.
Additionally, it's possible to subscribe to a GlobalCounterSignal
like you would with an EventSource
:
GlobalCounterSignal.monitor().subscribe((event) => {
// Code here will run when the value of the signal changes
});
Methods | Description |
---|---|
createGlobalCounterSignal(startValue: number, signalName: string) |
Creates a new GlobalCounterSignal with a globally unique name as specified by signalName , and with the initial value set by startValue . |
increment(i: number) |
Increases the value of the GlobalCounterSignal by the value of i . |
decrement(i: number) |
Decreases the value of the GlobalCounterSignal by the value of i . |
set(val: number) |
Sets the value of the GlobalCounterSignal to val . |
GlobalCounterSignal
extends theScalarSignal
class. As such, methods exposed byScalarSignal
can also be called onGlobalCounterSignal
.
Click to view example
const State = require('spark-state');
(async function () {
// Initializes a new global counter signal with the initial value: 1
const globalCounter = await State.creatCounterGlobalSignal(1, 'globalCounter');
// Increments the counter signal value by 2
globalCounter.increment(2);
})();
GlobalStringSignal
is a wrapper object for the StringSignal
class from the Spark AR API's ReactiveModule
. However, the string value contained by the signal is synchronised globally across all peers in a multipeer effect.
Additionally, it's possible to subscribe to a GlobalStringSignal
like you would with an EventSource
:
GlobalStringSignal.monitor().subscribe((event) => {
// Code here will run when the value of the signal changes
});
Methods | Description |
---|---|
createGlobalStringSignal(startValue: string, signalName: string) |
Creates a new GlobalStringSignal with a globally unique name as specified by signalName , and with the initial value set by startValue . |
set(val: string) |
Sets the value of the GlobalStringSignal to val . |
GlobalStringSignal
extends theStringSignal
class. As such, methods exposed byStringSignal
can also be called onGlobalStringSignal
.
Click to view example
const State = require('spark-state');
(async function () {
// Initializes a new global string signal with the initial value: 'Hello'
const globalString = await State.creatStringGlobalSignal('Hello', 'globalString');
// Sets the value of the signal to 'Hello world'
globalString.set('Hello world');
})();
GlobalScalarSignal
is a wrapper object for the ScalarSignal
class from the Spark AR API's ReactiveModule
. However, the scalar value contained by the signal is synchronised globally across all peers in a multipeer effect.
Additionally, it's possible to subscribe to a GlobalScalarSignal
like you would with an EventSource
:
GlobalScalarSignal.monitor().subscribe((event) => {
// Code here will run when the value of the signal changes
});
Methods | Description |
---|---|
createGlobalScalarSignal(startValue: number, signalName: string) |
Creates a new GlobalScalarSignal with a globally unique name as specified by signalName , and with the initial value set by startValue . |
set(val: number) |
Sets the value of the GlobalScalarSignal to val . |
GlobalScalarSignal
extends theScalarSignal
class. As such, methods exposed byScalarSignal
can also be called onGlobalScalarSignal
.
Click to view example
const State = require('spark-state');
(async function () {
// Initializes a new global scalar signal with the initial value: 0
const globalScalar = await State.creatScalarGlobalSignal(0, 'globalScalar');
// Sets the value of the signal to 42
globalScalar.set(42);
})();
GlobalPeersMap
is a key-value pair data type which contains the IDs of all participants in a multipeer effect as keys, and their global signals as values.
Values of types GlobalCounterSignal
and GlobalStringSignal
are supported.
The participantId
parameters in the method calls refer to each effect participant's unique ID string as returned by the Participant.id
property from the Spark AR API.
Methods | Description |
---|---|
createGlobalPeersMap(participantsStartValue: number | string, signalName: string) |
Creates a new GlobalPeersMap with a globally unique name as specified by signalName , and with the initial value set by participantsStartValue . |
get(participantId: string) |
Returns the GlobalCounterSignal or GlobalStringSignal from the Participant specified by participantId . |
set(participantId: string, value: number | string) |
Sets the value of the GlobalCounterSignal or GlobalStringSignal to the value specified by value , for the Participant specified by participantId . |
keys() |
Returns all of the keys from the GlobalPeersMap , as participantIds . |
setOnNewPeerCallback(callback: Function) |
Sets a callback function to call whenever a new peer is added to the GlobalPeersMap . |
Click to view example
const State = require('spark-state');
const Participants = require('Participants');
(async function () {
// Initializes a new global peer map
const points = await State.createGlobalPeersMap(0, 'points');
// Retrieve the ID for the self participant
const myParticipantId = (await Participants.self).id;
// Get the GlobalCounterSignal from the specified participant
const pointCounter = await points.get(myParticipantId);
})();
Full Spark AR API documentation is available on the main documentation site.
The sample code found in the script.js
file shows practical usage for all of the global types currently supported by the Spark State library.
The following resources are available on the Spark AR Studio documentation site:
ParticipantsModule
MultipeerModule
- Scripting your first multipeer effect
- Creating turn-based experiences
The Spark State library is MIT licensed.