Browse Source

Execute DelayedStart() in ScriptInstance, LuaScriptInstance & LogicComponent before first FixedUpdate() if it comes sooner than Update(). Fix long-standing bug in NinjaSnowWar where the ninja orientation would be overwritten by yaw=0 from controls.

Lasse Öörni 9 years ago
parent
commit
d1096470d5

+ 7 - 0
Source/Urho3D/AngelScript/ScriptInstance.cpp

@@ -816,6 +816,13 @@ void ScriptInstance::HandlePhysicsPreStep(StringHash eventType, VariantMap& even
     if (!scriptObject_)
         return;
 
+    // Execute delayed start before first fixed update if not called yet
+    if (methods_[METHOD_DELAYEDSTART])
+    {
+        scriptFile_->Execute(scriptObject_, methods_[METHOD_DELAYEDSTART]);
+        methods_[METHOD_DELAYEDSTART] = 0;  // Only execute once
+    }
+
     using namespace PhysicsPreStep;
 
     VariantVector parameters;

+ 8 - 0
Source/Urho3D/LuaScript/LuaScriptInstance.cpp

@@ -683,6 +683,14 @@ void LuaScriptInstance::HandlePostUpdate(StringHash eventType, VariantMap& event
 
 void LuaScriptInstance::HandleFixedUpdate(StringHash eventType, VariantMap& eventData)
 {
+    // Execute delayed start before first fixed update if not called yet
+    if (scriptObjectMethods_[LSOM_DELAYEDSTART])
+    {
+        if (scriptObjectMethods_[LSOM_DELAYEDSTART]->BeginCall(this))
+            scriptObjectMethods_[LSOM_DELAYEDSTART]->EndCall();
+        scriptObjectMethods_[LSOM_DELAYEDSTART] = 0;  // Only execute once
+    }
+
     using namespace PhysicsPreStep;
     float timeStep = eventData[P_TIMESTEP].GetFloat();
 

+ 7 - 0
Source/Urho3D/Scene/LogicComponent.cpp

@@ -205,6 +205,13 @@ void LogicComponent::HandlePhysicsPreStep(StringHash eventType, VariantMap& even
 {
     using namespace PhysicsPreStep;
 
+    // Execute user-defined delayed start function before first fixed update if not called yet
+    if (!delayedStartCalled_)
+    {
+        DelayedStart();
+        delayedStartCalled_ = true;
+    }
+
     // Execute user-defined fixed update function
     FixedUpdate(eventData[P_TIMESTEP].GetFloat());
 }

+ 5 - 6
bin/Data/Scripts/NinjaSnowWar/Ninja.as

@@ -47,14 +47,13 @@ class Ninja : GameObject
         aimY = 0;
     }
 
-    void Start()
-    {
-        SubscribeToEvent(node, "NodeCollision", "HandleNodeCollision");
-        aimX = node.rotation.yaw;
-    }
-    
     void DelayedStart()
     {
+        SubscribeToEvent(node, "NodeCollision", "HandleNodeCollision");
+        
+        // Get horizontal aim from initial rotation
+        aimX = controls.yaw = node.rotation.yaw;
+
         // Start playing the idle animation immediately, even before the first physics update
         AnimationController@ animCtrl = node.children[0].GetComponent("AnimationController");
         animCtrl.PlayExclusive("Models/NinjaSnowWar/Ninja_Idle3.ani", LAYER_MOVE, true);