[Working on] Relative entity paths

In the next Wave Engine version, we are introducing a new feature that can help improving your productivity.

We have decided to add relative entity path in our engine, allowing to obtain an entity just knowing its path from another entity.

This will be really helpful when you’re creating components that reference other entities and:

  • You don’t know the target entity’s absolute path.
  • Your component is used several times in your scene.
  • You want to make your component / entity reusable.

Path representation

These are the path representation elements:

  • Entity separator:  
  • Current entity:      
  • Parent entity:        

Sample uses

Giving the next sample entity hierarchy:

 

  • The relative path from wheel1 to tire2 would be:
[parent].wheel2.tire2
  • The relative path from car to tire1:
.wheel1.tire1

or

[this].wheel1.tire1

Notice that  [this] is optional.

  • The absolute path of wheel1 entity would be exactly te one we’ve been using until now:
  • car.wheel1
  • When you want to get an entity that doesn’t belong to the source’s root entity, we just specify the target’s absolute path. For example, the relative path from tire1 to ground is just:
ground

Instead of

[parent].[parent].[parent].ground   (Incorrect path)
  • To figure out if a specific path is absolute or relative, we just have to read the first element. If it’s one of the special elements (‘.’, ‘[this]’ or ‘parent’) it’s a relative path. Otherwise, it’s an absolute one.
[parent].wheel1    Relative
.tire2             Relative
[this]             Relative
car.wheel2         Absolute
road               Absolute

Additional methods

In order to make easier the search of entities in code, we’ve added the Find method in the Entity class.

[code language=”csharp”]
public Entity Find (string path)
[/code]

 

This method finds an entity with the desired relative path respect the caller entity. If the relative path is not correct, it returns null.

We have also added an additional parameter in the Find method in EntityManager class, allowing to set the source entity and allowing to directly search there.

[code language=”csharp”]
public Entity Find (string path, Entity sourceEntity = null)
[/code]

For example (following with the above-mentioned hierarchy), if we want to search the tire1 entity from car we can call one of the next :

[code language=”csharp”]
Entity tire;
// We can find the entity either of these ways
tire = car.Find(".wheel1.tire1");
tire = this.EntityManager.Find(".wheel.tire1", car);
[/code]

Comparison

Until now, we had to use relative paths in code. This means that if we wanted to make the component robust, we should check for null.

Let’s imagine we are coding a component for the wheel1 entity and we want to use the tire2 class.

Before

We should either:

  • Find it using the absolute path, which makes it harder to deal with when we want to make it reusable.

[code language=”csharp”]
Entity tire = this.EntityManager.Find("car.wheel2.tire2");
[/code]

  • Find it using the relative path by code.

[code language=”csharp”]
// We are assuming the component owner is ‘car’
// We are ignoring any null check
Entity tire = this.Owner.Parent.FindChild("wheel2").FindChild("tire2");
[/code]

Now

We just have to search the entity:

[code language=”csharp”]
Entity tire = this.Owner.Find("[parent].wheel2.tire2");
[/code]

Stay tunned because we are about to add more tools for making it easier the task of creating your components.

Leave a Reply

Your email address will not be published. Required fields are marked *