Jump to content
  • Advertisement
Sign in to follow this  
beun

Missing behaviours

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

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.

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Indeed. Angelscript doesn't permit assignment to temporary objects.

Glad you worked out a solution though.

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!