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년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠