Advertisement

Passing objects to functions?

Started by December 13, 2005 04:13 PM
3 comments, last by WitchLord 18 years, 11 months ago
I've been running into a problem when trying to call C++ methods from angelscript that take an object as a parameter. I get a "There is no copy operator for this type available" error. Chaning my object byte size from 0 to sizeof(class) fixes this. However, the documentation leads me to believe that I should not have to provide a size. What am I doing wrong? Example of what I'm trying to accomplish:
[source lang = "cpp"]
// C++ code

class CSound { ... };
CSound* sound_pointer;

void CGlobalScriptFunctions::PlaySound(CSound& sound)
{
    m_gameEngine->PlaySound(&sound);
}

// Assume I have some ScriptManager class that setups angelscript
void CScriptManager::SetupAS()
{
    // Register the CSound type
    m_scriptEngine->RegisterObjectType("CSound", 0, asOBJ_CLASS);

    // I keep all global methods in a "Global" type so that I don't have to write c-style wrappers for all my functions
    m_scriptEngine->RegisterObjectMethod("Global", "void PlaySound(CSound &in)", asMETHOD(CGlobalScriptFunctions, PlaySound), asCALL_THISCALL);

    // Register a sound for angelscript to see.  Typically this would be done for all entities in a level
    m_scriptEngine->RegisterGlobalProperty("CSound some_sound", sound_pointer);
}


// AS code

void main()
{
    // I have a single instance of type Global called "_g"
    _g.PlaySound(some_sound);
}

Thanks
In order for AngelScript to guarantee the life-time of the parameter reference that you're passing to the application function, it has to make a copy of the object. Since you've not told AngelScript the size of the CSound object it cannot make this copy.

If you could instead register the CSound object to use ADDREF and RELEASE behaviours, you could pass the object with an object handle to the PlaySound() method. In this case you will not have to define the size of the object.

In 2.5.0 have a slightly improved way of handling parameter references, e.g. if the type support object handles and the parameter is const ∈, then no copy of the object will be made. The &inout, also works more like expected in that it passes the true reference, it works best if the type support object handles though.

I'll try to clarify the manual in order to avoid this confusion in the future.

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

Advertisement
The lack of copy operator message made me think that it was probably copying the object. I thought maybe to try handles instead, and made a bogus addref/release function that does nothing. I have no problems with any declarations or anything, but how do I write a C++ function that takes an angelscript handle as a parameter? I tried using a pointer as a parameter, but the address comes back wrong (and pointing to junk data).

Thanks
I figured it out. For other users who may be interested:

1. The instance of CSound was declared as an object (not a handle).
2. The function definition for PlaySound was: PlaySound(CSound@)
3. The C++ function definition for PlaySound was: PlaySound(CSound*)
4. The call in Angelscript looks like: Playsound(@sound_instance)
[smile]

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

This topic is closed to new replies.

Advertisement