Advertisement

`RegisterObjectMethod` failed to register `set_opIndex` property

Started by September 06, 2024 07:44 AM
4 comments, last by WitchLord 2 months ago

I'm trying to make my own script string. I want to register a convenient interface for replacing part of string.

Here is my declaration void set_opIndex(int idx, const string &in) property, and its signature in C++ is void string_set_opIndex_str(std::string& this_, asINT32 idx, const std::string& str) . The script string is registered as a value class. The expected outcome is that I can write script code like str[1] = “inserted”.

But I keep getting error message like this: Failed in call to function 'RegisterObjectMethod' with 'string' and 'void set_opIndex(int idx, const string &in) property' (Code: asINVALID_DECLARATION, -10)

None

I'll need to investigate this. It seems like it should work the way you're doing it.

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

Did you register the get_opIndex to return a different type than string before this? That is one reason why this would fail like this.

(I'll need to improve the error message to say why the declaration is invalid.)

If this is not your case, then I'll need more information from you so I can reproduce it. Can you debug into the RegisterObjectMethod and understand why it is returning the error code asINVALID_DECLARATION (-10)?

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

It seems that the reason is exactly that I have registered a get_opIndex returning a different type before. (I tried to implement a get_opIndex that returns a uint containing the code point of the indexed character in string in order to handle non-ASCII characters.)

My simplest code for reproducing the problem:

#include <angelscript.h>
#include <cassert>
#include <cstdint>
#include <iostream>

void log_msg(asSMessageInfo* msg, void*)
{
    std::cerr << msg->message << std::endl;
}

std::uint32_t get_opIndex(const std::string&, int)
{
    return 0;
}

void set_opIndex(const std::string&, int, const std::string&)
{
    /* empty */
}

int main(int argc, char** argv)
{
    asIScriptEngine* engine = asCreateScriptEngine();

    engine->SetMessageCallback(asFUNCTION(log_msg), nullptr, asCALL_CDECL);

    int r = engine->RegisterObjectType(
        "string", sizeof(std::string), asOBJ_VALUE | asOBJ_APP_CLASS_CDAK
    );
    assert(r >= 0);

    r = engine->RegisterObjectMethod(
        "string",
        "uint get_opIndex(int idx) const property",
        asFUNCTION(get_opIndex),
        asCALL_CDECL_OBJFIRST
    );
    assert(r >= 0);

    // Exploded here
    r = engine->RegisterObjectMethod(
        "string",
        "void set_opIndex(int idx, const string &in) property",
        asFUNCTION(set_opIndex),
        asCALL_CDECL_OBJFIRST
    );
    assert(r >= 0);

    engine->ShutDownAndRelease();
    return 0;
}

If I remove the line for registering get_opIndex, the error will disappear.

None

Yes, that is exactly what I suspected.

This is currently not supported. Both get and set accessors must be using the same type in order to be considered valid.

I will see what to do about this. Either the error message must be improved to be clear about why it failed, or potentially I can add support for different types and multiple overloads.

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