Skip to content

Accessing components

Mark Knol edited this page Oct 18, 2017 · 3 revisions

Grab a component from Entity

In this example an Entity has a Sprite. We try to grab it back out of the Entity. Sometimes, you can assume there is a class of given type in the Entity (based on your code architecture), but to be sure it is really there, you could do a null-check. Alternatively you can also choose to check with entity.has(Sprite).

var sprite = myEntity.get(Sprite); 
if (sprite != null) 
{ 
	trace('sprite found in entity: $sprite');
}
else
{
	trace('Warn: No sprite found.');
}

The fun part of this, is that you can swap the actual Sprite with any other Sprite (like FillSprite, ImageSprite, PatternSprite etc..). If you still use entity.get(Sprite), it returns that sprite. That allows you to create loose assumptions, which gives some flexibility.

Clone this wiki locally