Spawning Entities in Amazon Sumerian

Spawn Entities in Amazon Sumerian

Programatically generate Entities using the Scripting API. ...

Spawning Entities in Amazon Sumerian (e.g programatically / dynamically) can be a difficult thing to figure out using Amazon’s Scripting API documentation.

The solution for this is simple once you know what to do, here is the simplified process on how to programmaticaly instantiate an instance of a predefined Entity:

Step 1

Define an Entity by itself and name it “Box 2” (see the cube on the right)
Define an Entity that you’d like to spawn, name it “Box 2” and create a second Entity called “Test”.

Step 2

Create a script with the code below and drag the Entity onto it
Assign the “Test” Entity with the generateQuad.js script (see below) and drag the “Box 2” Entity onto it.

Step 3

Dynamically generate an instance of the “Box 2” Entity (shown in the center)
Play the scene to dynamically generate an instance of the “Box 2” Entity (the middle box).

I’ve posted this script below (with comments) so you can now begin spawning entities within Amazon Sumerian. Enjoy! 😀

generateQuad.js

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

// creating a parameter in the GUI for the Script, that lets you to drag an entity that you'd like to spawn onto it (in this case "Box 2")
export const PROPERTIES = {
    // the name of the field that appears within the GUI
    entityToSpawn: {
        // the type of file that the parameter is expecting
        type: s.type.Entity,
        description: 'This can be an entity that exists in your scene or an entity that was dragged into the Assets panel for reuse.'
    }
};

// the default function that is called automatically
export default function(ctx, {entityToSpawn}) {
	// defining the class of the new entity that can be called upon start
	const spawnEntityInstance = new s.entity.LoadEntityInstanceAction({
        entity: entityToSpawn, // use the entity that you've dragged onto the GUI
        onCreate: (entity) => {
			// on create set the position of the entity
			entity.position.setDirect(0,0,40);
		}
    });
	
	// when the context starts, call the class that spawns an instance of the entity
	ctx.start(spawnEntityInstance);
}

Share this post