Sign in to follow this  

Object Communication

This topic is 1796 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites

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();
   }
}

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this