Angelscript: Circular references between scripts and app-declared types/functions.

Started by
0 comments, last by Mirquoid 9 years, 4 months ago

I have been playing around with Angelscript and I have come across a problem that I am not quite sure how to overcome.

I have a base class, ScriptComponent, declared in AS, from which I derive other AS classes (which represent a component for a game object system). These script component objects are created on the application side, and I want to be able to refer to them in other scripts via a function GetScriptComponent(), which returns a ScriptComponent@ (In the script I cast to the appropriate component type).

I have declared a global function which returns a asIScriptObject* in C++ (a pointer to an instance of a ScriptComponent derived type), and a ScriptComponent@ in AS. (I am not sure if this works yet).


// C++

RegisterGlobalFunction("ScriptComponent@ GetScriptComponent(uint, const string &in, int index = 0)", asMETHOD(EntityManager, GetEntityScriptComponent), asCALL_THISCALL_ASGLOBAL, this);


The problem is that at the point of declaring GetScriptComponent() on the application side, ScriptComponent needs to have been previously declared. I am loading all of the scripts via the CScriptBuilder add-on. If I load the scripts first, before the application-side declarations, I have undefined references to those types and functions. If I load the scripts after the declarations, specifically GetScriptComponent, I have an undefined reference to ScriptComponent (as it is declared in a script).

Can I do some sort of forward declaration of ScriptComponent, or is there some simpler way I am not seeing. I thought about declaring ScriptComponent on the application side, but I don't think I would be able to derive script classes from it, is that true? Is there some functionality in CScriptBuilder I can use? Or perhaps I need to look at using multiple modules? I'm not quite sure how to proceed, I'll keep looking, but if anyone can point me in the right direction, that would be handy. Thank you.

Here is an example of how I am using the scripts:


// Angelscript

class ScriptCompoent
{
}

class Health : ScriptComponent
{
    ...
}

class Wander : ScriptComponent
{
    bool OnLoad(uint uiEntity)
    {
        @kTransform = cast<Transform>(GetComponent(uiEntity, "Transform"));
        @kHealth = cast<Health>(GetScriptComponent(uiEntity, "Health"));
 
        return true;
    }
 
    void OnUpdate(float fDT)
    {
        // Move towards random location
        // If bleeding, loose a bit of health (probably a bad example, you would do this in Health itself).
    }
 
    Transform@ kTransform;
    Health@ kHealth;
}
Advertisement

I solved the problem by registering ScriptComponent as a class interface, before registering GetScriptComponent, and then loading the scripts.

This topic is closed to new replies.

Advertisement