Jump to content
  • Advertisement
Sign in to follow this  

How to make classes and objects from another module accessible?

This topic is 1599 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



I know I asked a similar question before but I still have one doubt.


So, my game has N states (individual screens like pause, menu, game etc) and each one lives in its own module and communicates with other states exclusively using a central queue, so all information gets to the destination indirectly.


But now, I also added behaviors to the engine, where each behavior is a script that controls an entity's behavior. For example, one behavior script might give an artificial intelligence to the character, another just make it follow a path etc.


For now, each behavior is a custom class, such as class MyBehavior : Behavior {}, and is also living in its own module and loaded from its own script file, which works fine to deal with its attached entity and give it behavior. However, now I need that my game state script is able to retrieve data from any of this behaviors currently instanced, like:


// this is the game state script

behaviorClass@ behaviorObject = cast<behaviorClass@>( GetBehavior("entityName", "behaviorClass") );

behaviorObject.x = ; // set or get variables declared in the external file of the given behavior


I'd like to add that the behaviors are hot reloadable, so I basically shut them down and re create them once the script source has changed, in order to have the most recent version running in the game without re-running the process. Is there a way to do something similar to this with AngelScript? I am not sure if the game state script module could either dynamically bind to these external behavior classes or just remove and recompile the code sections respective to the behavior scripts so they are in scope, while operating on instanced objects of behavior classes created in the other module.




As a second question, just wanted to ask if there is a way to associate member variables with the pure angelscript interfaces. I basically want that when I implement a new behavior, as it inherits the interface Behavior, it would automatically inherit a member Entity@ attachedEntity, so even if my custom behavior class has no members it still knows about which entity that instance is attached to. I am asking as I didn't see any C++ api function to do this, interfaces seemed to only be able to define the virtual methods and actual interface name, 


The solution I thought about was to define myself a base class that is actually a angelscript class rather than an interface, this way I could do anything to it and pre-implement functions etc, is this the way to go?


Is there any chance you can implement in the future some kind of explicit and/or implicit conversion mechanism when calling a function? Like MyBehavior@ a = getBehavior<MyBehavior>() or have it return instead an abstract handle to the base type that can be automatically casted to the expected type of 'a'? I am talking about doing these with only script classes here

Edited by Grimshaw

Share this post

Link to post
Share on other sites

I believe what you're looking for is 'shared entities', and possibly also the use of 'mixin classes'.


Shared entities allow you to declare classes, interfaces, and functions as shared between the modules. This makes it possible to transport these entities between modules and still allow both modules to understand each other. One note of warning, because shared entities are shared they can be difficult to modify through hotloading, so restrict it to use it only for those types that really must be shared, e.g. an interface that a class can implement, or small container classes for carrying data. 


Mixin classes is useful for allowing code reuse similar to multiple inheritance in C++, but without actually being multiple inheritance. You can define a mixin class with the default implementation of methods and prototypes, and even allow it to implement interfaces. When a mixin class is then included in an class declaration the class automatically receives all this from the mixin class.



Implicit and explicit casts are already supported in the script language. There is also the generic handle type as an add-on. I'm not sure what exactly you had in mind with this. Perhaps you want something like template functions in C++?

Edited by Andreas Jonsson

Share this post

Link to post
Share on other sites

Thanks Andreas,good answer.


Shared Entities seem to be what I am looking for but apparently they don't support hot reloading as I can't reload the code with another module referencing it, but its okay, it makes sense; back to the drawing board :)

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!