signalsStateMachineTidbit

Sumerian State Machine Signals

Use JS to store variables globally and trigger other scripts. ...

Building on from my experiment with visualising wordpress posts in Amazon Sumerian, I’ve begun to use State Machine Signals to store global variables and trigger other scripts in response to programatic events.

This is done by assigning a State Machine to an Entity that you’d like to be interactive, then setting up two states with assigned scripts. The first state’s script saves a variable globally and calls an onSuccess() function, that triggers the second function to execute.

Step 1

Sumerian State Machine Signals
Create an Entity called ‘Box’ and assign a State Machine to it with a behaviour called ‘loadBlog’.

Step 2

Sumerian State Machine Signals
Create an initial state called ‘Load all posts’ (with getBlogPosts.js attached) and link it to a second state called ‘loopPosts’ (with postLoop.js attached) code for both files are below.

Step 3

Sumerian State Machine Signals
Play the scene and check the console to see the variable that was stored globally in the first script and logged by the second.

I’ve posted this scripts below (with comments) so you can now begin using State Machine Signals and scripts to organise your scene’s behaviour within Amazon Sumerian. Enjoy! 😀

getBlogPosts.js

// import that preview version of the Sumerian Scripting API as a variable called 's'
import * as s from 'module://sumerian-common/api';

// A statement to Sumerian to expect SIGNALS
export const SIGNALS = {
	// stating onSuccess as a signal to end the script when wordpess posts have been retrieved
  	onSuccess: {  description: 'Wordpress posts have been retrieved' }
};

export default async function(ctx, options) {

	// get wordpress posts
	window.posts = await getPosts();
	
	// once the script has stopped
	ctx.onStop(() => {
		// send the onSuccess signal
		ctx.signal(options.onSuccess);
	});
}

// get posts from wordpress
function getPosts() {	
  let response = fetch('https://jamesmiller.blog/wp-json/wp/v2/posts')
  .then(response => {
	  return response.json();
  });
	
  return response;
}

postLoop.js

// import that preview version of the Sumerian Scripting API as a variable called 's'
import * as s from 'module://sumerian-common/api';

export default async function(ctx, options) {
	// log posts that have been declared in getBlogPosts.js
	console.log(window.posts);
}

Share this post