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
已提問 2 年前檢視次數 240 次
2 個答案
1
已接受的答案

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
已回答 2 年前
0

Awesome, thanks!

batfink
已回答 2 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南