Interoperation between traits

What is most effective way to exchange data between traits scripts?
Basically I need to notify trait on the same object to play another animation.

Found it is possible to do it this way:

package arm;
import iron.system.Input;
import iron.system.Time;
import iron.object.Object;
import iron.object.BoneAnimation;
import arm.RootMotion;

class PlayerMotion extends iron.Trait {
	var anim: BoneAnimation;
	var rm: RootMotion;
	function findAnim(obj: Object)
	{
		for (c in obj.children) {
			trace("t0 " + c.name);
			if (c.animation != null) {
				trace("t1 " + c.name);
				anim = cast(c.animation, BoneAnimation);
				if (anim != null)
					trace("t2 " + c.name);
			}
		}
		if (anim != null)
			return;
		for (c in obj.children) {
			findAnim(c);
			if (anim != null)
				break;
		}
	}
	public function new() {
		super();


		notifyOnInit(function() {
			findAnim(object);
			if (anim == null)
				return;
			anim.play("Default");
			rm = object.getTrait(RootMotion);
		});
		notifyOnUpdate(function() {
			if (anim == null)
				return;
			var kbd = Input.getKeyboard();
			if (kbd.down("w")) {
				rm.update_anim("man-walk01");
			} else {
				rm.update_anim("Default");
			}
		});

	}
}

So basically we import our custom trait script as arm.Traitname (arm.RootMotion in above example).
Then we create reference var using var rm = object.getTrait(Traitname) (RootMotion in above example)

Now we can call public functions and set public variables in referenced trait.

1 Like