realXtend / tundra

realXtend Tundra SDK, a 3D virtual world application platform.
www.realxtend.org
Apache License 2.0
84 stars 70 forks source link

Tundra crashes if component is not found via script #570

Open lznt opened 11 years ago

lznt commented 11 years ago

It seems that this snippet crashes tundra everytime we enter CheckAnims. The entity this.me has animationcontroller component and its found if we print it for example. The crash message is animationcontroller is not an object and is "undefined". Same thing happens if I add dynamiccomponent checks inside EnableAnims, but then it ofc crashes to dynamiccomponent being undefined. Entity has that one also.

function EntityScript(entity){
  this.me = entity;
  frame.Updated.connect(this, this.Update);
}

EntityScript.prototype.CheckAnims = function(){
  if(this.me.animationcontroller.GetAvailableAnimations().length > 0){
        this.EnableAnims();
  }else
    frame.DelayedExecute(1.0).Triggered.connect(this.EnableAnims);
}

EntityScript.prototype.EnableAnims = function(){
    this.me.animationcontroller.EnableAnimation('Animation');
} 

EntityScript.prototype.Update = function(frametime){
  if(server.IsRunning()){

  }else{
    if(this.me.dynamiccomponent.GetAttribute('Placed') == true)
      this.CheckAnims();  
  }
}
jonnenauha commented 11 years ago

You probably have to change the signal connection line to frame.DelayedExecute(1.0).Triggered.connect(this, this.EnableAnims);

Otherwise this wont probably mean what you think it will when you enter the callback. I see you already do this in the frame.Update connection. Anyhow this.me.animationscontroller should not crash even if its not there, but always good to put some if (this.me.animationcontroller != null) checks in place. If that says null the component is not there even if you say it is. The this or this.me is something else that you think it is :) Printing these things helps a lot for simple debugging. print(this.me) for example at the start of CheckAnims will print the entity name to the console, so you can be sure.