Advertisement

Object Communication

Started by February 14, 2013 10:20 PM
4 comments, last by Edgyr45 11 years, 6 months ago

Hi,

I'm integrating AngelScript into our game engine and I'm facing one problem I would like to ask here.

Lets say I have a GameObject class, which can own a asIScriptObject* and call function in it. (Like in the "game" sample).

I have two GameObjects, "Go1" with "player.as" and "Go2" with "zombie.as"

If a player kills a zombie, a function "void Kill()" must be called from player.as inside the zombie.as script.

I would like to avoid using a Send/Receive message function but to directly call Kill() inside player.as (a lots like in Unity) :


class player
{
    //This is call from the engine on the GameObject's controller script:
    void DoSomething()
    {
        GameObject go2 = Engine.FindObjectByName("Go2");

        //What I would like to do, or something like that:
        Controller go2Controller = go2.GetController("zombie");
        go2Controller.kill();
    }
}

The "void Kill()" function is unknown from the engine, but an "instance" of zombie.as is created in Angelscript's engine. Can I get that instance and call function on it? I'm lost here.

Thank you very much

Is there a reason casting wouldn't work here?

clb: At the end of 2012, the positions of jupiter, saturn, mercury, and deimos are aligned so as to cause a denormalized flush-to-zero bug when computing earth's gravitational force, slinging it to the sun.
Advertisement

Either the engine can register the Kill() method on the IController interface, or you can have the script implement an extra interface that all script objects know about.

// Declare the interface that living entities must implement
// Make it shared so all modules share the exact same implementation
shared interface ILiving
{
   void Kill();
}
 
// The player class and all enemies implement both the IController and the ILiving interfaces
class Player : IController, ILiving
{
   void DoSomething()
   {
     GameObject go2 = engine.GetGameObject("Go2");
     ILiving @ctrl = cast<ILiving>(go2.GetController());
     ctrl.KillI();
   }
}

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thanks for the reply!

I cannot predict in advance what functions the "production" programmer will want to use. In your example, you wrote : go2.GetController(). What type does it returns exactly? An asIScriptObject* from a script that implement IController ?

Does that mean, like InvalidPointer suggested, that I can simply cast it?


Zombie @ctrl = cast<Zombie>(go2.GetController());

And let the production programmer call whatever they want in Zombie.as? Or I tell them to use interfaces?

Thank you very much

Yes, the C++ implementation of go2.GetController() should return a asIScriptObject *. It should be registered in AngelScript as returning a IController @.

If you plan on having separate modules for the entity controllers as in the game sample, then you need to tell your programmers to use shared interfaces like I posted earlier, because one module doesn't know the actual class type in the other module. If on the other hand you put all the scripts in the same module, then you can cast directly to the actual class without the need for an extra interface.

Regards,

Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

I'll run some test with this info.

Thank you very much

This topic is closed to new replies.

Advertisement