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年前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ