Browse Source

Added the script-only function Entity::createScriptObject() for convenience. This is for cases where only the script object is significant, instead of the ScriptInstance component that is also created at the same time.

Lasse Öörni 15 years ago
parent
commit
c73c30fde1

+ 10 - 16
Bin/Data/Scripts/GameObject.as

@@ -90,10 +90,8 @@ class GameObject : ScriptObject
     {
     {
         Entity@ newEntity = scene.createEntity();
         Entity@ newEntity = scene.createEntity();
 
 
-        // Create the ScriptInstance with specified class
-        ScriptInstance@ instance = newEntity.createComponent("ScriptInstance");
-        instance.setScriptClass(cache.getResource("ScriptFile", "Scripts/NinjaSnowWar.as"), className);
-        GameObject@ object = cast<GameObject>(instance.getScriptObject());
+        // Create the script object with specified class
+        GameObject@ object = cast<GameObject>(newEntity.createScriptObject("Scripts/NinjaSnowWar.as", className));
         if (@object != null)
         if (@object != null)
             object.create(position, rotation);
             object.create(position, rotation);
 
 
@@ -110,9 +108,7 @@ class GameObject : ScriptObject
         emitter.setPosition(position);
         emitter.setPosition(position);
 
 
         // Create a GameObject for managing the effect lifetime
         // Create a GameObject for managing the effect lifetime
-        ScriptInstance@ instance = newEntity.createComponent("ScriptInstance");
-        instance.setScriptClass(cache.getResource("ScriptFile", "Scripts/NinjaSnowWar.as"), "GameObject");
-        GameObject@ object = cast<GameObject>(instance.getScriptObject());
+        GameObject@ object = cast<GameObject>(newEntity.createScriptObject("Scripts/NinjaSnowWar.as", "GameObject"));
         if (@object != null)
         if (@object != null)
             object.duration = duration;
             object.duration = duration;
 
 
@@ -123,13 +119,6 @@ class GameObject : ScriptObject
     {
     {
         Entity@ newEntity = scene.createEntity();
         Entity@ newEntity = scene.createEntity();
 
 
-        // Create a GameObject for managing the sound lifetime
-        ScriptInstance@ instance = newEntity.createComponent("ScriptInstance");
-        instance.setScriptClass(cache.getResource("ScriptFile", "Scripts/NinjaSnowWar.as"), "GameObject");
-        GameObject@ object = cast<GameObject>(instance.getScriptObject());
-        if (@object != null)
-            object.duration = duration;
-
         // Create the sound channel
         // Create the sound channel
         PositionalChannel@ channel = newEntity.createComponent("PositionalChannel");
         PositionalChannel@ channel = newEntity.createComponent("PositionalChannel");
         channel.setPosition(position);
         channel.setPosition(position);
@@ -138,6 +127,11 @@ class GameObject : ScriptObject
         Sound@ sound = cache.getResource("Sound", soundName);
         Sound@ sound = cache.getResource("Sound", soundName);
         channel.play(sound, sound.getFrequency());
         channel.play(sound, sound.getFrequency());
 
 
+        // Create a GameObject for managing the sound lifetime
+        GameObject@ object = cast<GameObject>(newEntity.createScriptObject("Scripts/NinjaSnowWar.as", "GameObject"));
+        if (@object != null)
+            object.duration = duration;
+
         return newEntity;
         return newEntity;
     }
     }
 
 
@@ -153,7 +147,7 @@ class GameObject : ScriptObject
         // If the other entity is scripted, perform object-to-object collision
         // If the other entity is scripted, perform object-to-object collision
         GameObject@ otherObject = cast<GameObject>(otherEntity.getScriptObject());
         GameObject@ otherObject = cast<GameObject>(otherEntity.getScriptObject());
         if (@otherObject != null)
         if (@otherObject != null)
-            objectCollision(otherEntity, otherObject, eventData);
+            objectCollision(otherObject, eventData);
     }
     }
 
 
     void worldCollision(VariantMap& eventData)
     void worldCollision(VariantMap& eventData)
@@ -188,7 +182,7 @@ class GameObject : ScriptObject
             isSliding = false;
             isSliding = false;
     }
     }
 
 
-    void objectCollision(Entity@ otherEntity, GameObject@ otherObject, VariantMap& eventData)
+    void objectCollision(GameObject@ otherObject, VariantMap& eventData)
     {
     {
     }
     }
 
 

+ 1 - 3
Bin/Data/Scripts/NinjaSnowWar.as

@@ -159,9 +159,7 @@ void startGame()
         gameScene.removeEntity(scriptedEntities[i]);
         gameScene.removeEntity(scriptedEntities[i]);
 
 
     Entity@ playerEntity = gameScene.createEntity("Player");
     Entity@ playerEntity = gameScene.createEntity("Player");
-    ScriptInstance@ instance = playerEntity.createComponent("ScriptInstance");
-    instance.setScriptClass(cache.getResource("ScriptFile", "Scripts/NinjaSnowWar.as"), "Ninja");
-    GameObject@ object = cast<GameObject>(instance.getScriptObject());
+    GameObject@ object = cast<GameObject>(playerEntity.createScriptObject("Scripts/NinjaSnowWar.as", "Ninja"));
     object.create(Vector3(0, 90, 0), Quaternion());
     object.create(Vector3(0, 90, 0), Quaternion());
     object.health = object.maxHealth = playerHealth;
     object.health = object.maxHealth = playerHealth;
     object.side = SIDE_PLAYER;
     object.side = SIDE_PLAYER;

+ 1 - 1
Bin/Data/Scripts/Potion.as

@@ -41,7 +41,7 @@ class Potion : GameObject
         body.addChild(model);
         body.addChild(model);
     }
     }
     
     
-    void objectCollision(Entity@ otherEntity, GameObject@ otherObject, VariantMap& eventData)
+    void objectCollision(GameObject@ otherObject, VariantMap& eventData)
     {
     {
         if (healAmount > 0)
         if (healAmount > 0)
         {
         {

+ 1 - 1
Bin/Data/Scripts/SnowBall.as

@@ -76,7 +76,7 @@ class SnowBall : GameObject
             duration = snowballGroundHitDuration;
             duration = snowballGroundHitDuration;
     }
     }
 
 
-    void objectCollision(Entity@ otherEntity, GameObject@ otherObject, VariantMap& eventData)
+    void objectCollision(GameObject@ otherObject, VariantMap& eventData)
     {
     {
         if (hitDamage > 0)
         if (hitDamage > 0)
         {
         {

+ 36 - 0
Engine/Engine/RegisterScene.cpp

@@ -24,6 +24,7 @@
 #include "Precompiled.h"
 #include "Precompiled.h"
 #include "RegisterTemplates.h"
 #include "RegisterTemplates.h"
 #include "Scene.h"
 #include "Scene.h"
+#include "ScriptFile.h"
 #include "ScriptInstance.h"
 #include "ScriptInstance.h"
 
 
 static void ConstructComponentRef(ComponentRef* ptr)
 static void ConstructComponentRef(ComponentRef* ptr)
@@ -117,6 +118,40 @@ static Component* EntityCreateComponentWithName(const std::string& type, const s
     }
     }
 }
 }
 
 
+static asIScriptObject* EntityCreateScriptObject(const std::string& scriptFileName, const std::string& className, Entity* ptr)
+{
+    try
+    {
+        ResourceCache* cache = getEngine()->getResourceCache();
+        ScriptFile* file = cache->getResource<ScriptFile>(scriptFileName);
+        if (!file)
+            return 0;
+        // Try first to reuse an existing, empty ScriptInstance
+        const std::vector<SharedPtr<Component> >& components = ptr->getComponents();
+        for (std::vector<SharedPtr<Component> >::const_iterator i = components.begin(); i != components.end(); ++i)
+        {
+            if ((*i)->getType() == ScriptInstance::getTypeStatic())
+            {
+                ScriptInstance* instance = static_cast<ScriptInstance*>(i->getPtr());
+                asIScriptObject* object = instance->getScriptObject();
+                if (!object)
+                {
+                    instance->setScriptClass(file, className);
+                    return instance->getScriptObject();
+                }
+            }
+        }
+        // Then create a new component if not found
+        ScriptInstance* instance = ptr->createComponent<ScriptInstance>(ptr->getUniqueComponentName());
+        instance->setScriptClass(file, className);
+        return instance->getScriptObject();
+    }
+    catch (Exception& e)
+    {
+        SAFE_RETHROW_RET(e, 0);
+    }
+}
+
 static void EntityRemoveComponent(const std::string& type, Entity* ptr)
 static void EntityRemoveComponent(const std::string& type, Entity* ptr)
 {
 {
     ptr->removeComponent(ShortStringHash(type));
     ptr->removeComponent(ShortStringHash(type));
@@ -243,6 +278,7 @@ static void registerEntity(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Entity", "void setPredictionFrom(Entity@+)", asMETHOD(Entity, setPredictionFrom), asCALL_THISCALL);
     engine->RegisterObjectMethod("Entity", "void setPredictionFrom(Entity@+)", asMETHOD(Entity, setPredictionFrom), asCALL_THISCALL);
     engine->RegisterObjectMethod("Entity", "Component@+ createComponent(const string& in)", asFUNCTION(EntityCreateComponent), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("Entity", "Component@+ createComponent(const string& in)", asFUNCTION(EntityCreateComponent), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("Entity", "Component@+ createComponent(const string& in, const string& in)", asFUNCTION(EntityCreateComponentWithName), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("Entity", "Component@+ createComponent(const string& in, const string& in)", asFUNCTION(EntityCreateComponentWithName), asCALL_CDECL_OBJLAST);
+    engine->RegisterObjectMethod("Entity", "ScriptObject@+ createScriptObject(const string& in, const string& in)", asFUNCTION(EntityCreateScriptObject), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("Entity", "void addComponent(Component@+)", asMETHOD(Entity, addComponent), asCALL_THISCALL);
     engine->RegisterObjectMethod("Entity", "void addComponent(Component@+)", asMETHOD(Entity, addComponent), asCALL_THISCALL);
     engine->RegisterObjectMethod("Entity", "void removeComponent(Component@+)", asMETHODPR(Entity, removeComponent, (Component*), void), asCALL_THISCALL);
     engine->RegisterObjectMethod("Entity", "void removeComponent(Component@+)", asMETHODPR(Entity, removeComponent, (Component*), void), asCALL_THISCALL);
     engine->RegisterObjectMethod("Entity", "void removeComponent(const string& in)", asFUNCTION(EntityRemoveComponent), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("Entity", "void removeComponent(const string& in)", asFUNCTION(EntityRemoveComponent), asCALL_CDECL_OBJLAST);