Programmatically referring to entities in a Sumerian scene

0

I'm using LoadEntityInstanceAction as recommended here. However, rather than manually nominate a specific entity to copy, I would like to specify it dynamically so I can reuse the script to generate multiple new entities via a for loop.

The drag and drop fields used in Sumerian show the name, but if I type the name instead of dropping it in, it doesn't work, so the name only doesn't seem sufficient. Similarly, if I don't nominate a entity to copy, the console output shows a missing 'ID' value, but how do I format the entity ID hex string as a parameter? I'm hoping for something like:

ctx.start(
	[s.entity.LoadEntityInstanceAction, { 
		entity: entityId='4ba69fc04dd94c1f904921e7e800c3a9'
		onCreate: onSpawnedEntityReady
	}]
);

I've been looking through the API documentation and can't see the answer, so if I'm just missing something, I apologise! Thanks

batfink
demandé il y a 2 ans241 vues
2 réponses
1
Réponse acceptée

EntitySets in Sumerian provide a convenient way to query your scene for entities that match certain criteria. You can query entities by name (as displayed in the "Entities" outline) or by tag. You can use literal strings or Regular Expressions to define your match criteria.

Below is some example code that creates an EntitySet by searching your scene for two entity name strings ("MyBox" and "MySphere") and then loops over those entities to create copies of them.

import * as s from 'module://sumerian-common/api';

export default function(ctx) {
	
	// Create an EntitySet containing the source entities
	// we care about.
	const spawnableEntities = ctx.world.entities.withName("MyBox", "MySphere");
	
	// Loop through each source entity in our EntitySet
	// and spawn a copy of each.
	spawnableEntities.forEach(entity => {
		ctx.start(
			[s.entity.LoadEntityInstanceAction, { 
				entity: entity, 
				onCreate: onSpawnedEntityReady
			}]);
	});
	
	// This function will be triggered as soon as each new entity instance 
	// has completed its initialization and is ready for use.
	function onSpawnedEntityReady(spawnedEntity) {
		
		// Here we're just moving the new entity to a random location.
		const x = Math.random() * 2;
		const y = Math.random() * 2;
		const z = Math.random() * 2;
		spawnedEntity.position.setDirect(x, y, z);
	}
}

An alternative to using the .withName() method is to use the .withTag() method. With that approach, you could tag multiple entities with a tag name of your choosing, for example "copy_me". Then you would use ctx.world.entities.withTag("copy_me") to create your EntitySet.

profile pictureAWS
Kris
répondu il y a 2 ans
0

Awesome, thanks!

batfink
répondu il y a 2 ans

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions