Advertisement

Missing behaviours

Started by May 21, 2009 08:42 AM
2 comments, last by WitchLord 15 years, 9 months ago
Hello, I'm trying to register some classes for AngelScript, but I'm receiving some very vague error messages. I'd like to register my 'property' class which handles the different properties/settings for my program. In CPP, I can do the following thing:
property("test") = "bla";
...
std::cout << property("test").value(); // prints out 'bla'
Here's the source code for the property class. Maybe it isn't a very nice solution, but this way I don't need to have a property instance for every property.
#include <string>
#include <boost/unordered_map.hpp>

class property {
public:

    property(std::string name) : m_name(name) { }

    void operator=(const std::string value)
    {
        m_hashmap[m_name] = value;
    }

    void set(const std::string value)
    {
        operator=(value);
    }

    std::string value() const
    {
        return m_hashmap[m_name];
    }

private:
    std::string m_name;
    static boost::unordered_map<std::string, std::string> m_hashmap;
};
I'm not sure how I should register this for AngelScript, but I thought that since the class only has 1 string member, it should be a value type. I also thought I only needed to register a constructor and a assignment behavior, so here's how I register it (as_check checks the return value, m_as is a pointer to asIScriptEngine):
void new_property(std::string name, property *memory)
{
    new(memory) property(name);
}
...
as_check(m_as->RegisterObjectType("property", sizeof(property), asOBJ_VALUE | asOBJ_APP_CLASS_CA));
as_check(m_as->RegisterObjectBehaviour("property", asBEHAVE_CONSTRUCT, "void f(const string)", asFUNCTION(new_property), asCALL_CDECL_OBJLAST));
as_check(m_as->RegisterObjectBehaviour("property", asBEHAVE_ASSIGNMENT, "void f(const string)", asMETHOD(property, set), asCALL_THISCALL));

When I execute this (AS):
void main()
{
   property("test") = "42";
}
I get the following error message:
Quote:
Error (line 0, col 0): Type 'property' is missing behaviours Error (line 0, col 0): Invalid configuration
which isn't helping me a lot. So I'm wondering what I'm doing wrong. I've looked at the documentation and the vector-addon, but I don't know what's causing the problem.
I realize the error message is vague. I'll see if I can improve it for a future version of AngelScript.

Anyway, you're missing the asBEHAVE_DESTRUCT behaviour. All types registered with asOBJ_VALUE require both the default constructor and the destructor, unless the type is also registered with the flag asOBJ_POD, in which case the engine assumes no special initialization/unitialization is needed.

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
Great, thanks.

I added a destructor behaviour and I also needed to add a default constructor behaviour (with no arguments). However, I got the error message 'Error: reference is temporary' when I executed 'property("test") = "bla";'. Apparently, something like 'object() = "test";' isn't valid in AngelScript (maybe the assignment behaviour is assumed to do an actual normal assignment). Instead, I renamed the property class to 'c_property' in AS and registered this function:

property &prop(std::string s){    static property p("");    p.set_name(s);    return p;}...as_check(m_as->RegisterGlobalFunction("c_property &property(string)", asFUNCTION(prop), asCALL_CDECL));


And now I can do things like 'property("test") = "bla";' in AS.
Indeed. Angelscript doesn't permit assignment to temporary objects.

Glad you worked out a solution though.

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