Newb Class Registration
I've gone through these forums several times and found little bits and pieces here and there, and looked through the manual, but I'm still a little lost! Please don't hurt me if this has been asked before. :)
I have a very simple class:
class delugeKB {
public:
delugeVideo *vEngine;
delugeKB(delugeVideo *vEngine_in);
bool isKeyDown(int keyCode);
};
That I wish to register. I have this function below the class declaration:
void Constructor (void *memory, delugeVideo *vEngine_in) {
new(memory) delugeKB(vEngine_in);
}
That I'm not sure is set up correctly or not.
And finally, to register it, I have this function as well:
void RegisterScriptDelugeKeyboard(asIScriptEngine *engine) {
int r;
r = engine->RegisterObjectType("keyboard", sizeof(delugeKB), asOBJ_VALUE | asOBJ_APP_CLASS_CONSTRUCTOR); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("keyboard", asBEHAVE_CONSTRUCT, "void f(delugeVideo)", asFUNCTION(Constructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );
//r = engine->RegisterObjectMethod("keyboard", "bool isKeyDown()", asMETHODPR(delugeKB, isKeyDown, (int), bool), asCALL_THISCALL); assert( r >= 0 );
As well, not totally sure if this is correct. I've spent about two hours doing different things trying to get it to work. Right now it is erroring with "Invalid Configuration".
Thanks for any help you can give,
Wil
The flag asOBJ_APP_CLASS_CONSTRUCTOR can't be used without the asOBJ_APP_CLASS flag. The correct call would be:
asOBJ_APP_CLASS_C is declared as (asOBJ_APP_CLASS | asOBJ_APP_CLASS_CONSTRUCTOR) in angelscript.h.
r = engine->RegisterObjectType("keyboard", sizeof(delugeKB), asOBJ_VALUE | asOBJ_APP_CLASS_C); assert( r >= 0 );
asOBJ_APP_CLASS_C is declared as (asOBJ_APP_CLASS | asOBJ_APP_CLASS_CONSTRUCTOR) in angelscript.h.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
Ahh, yes, I see...
Now it is complaining of missing behaviors. :(
In case it wasn't completely obvious, I'm hoping to do something like this in script:
delugeVideo video;
keyboard myKeyboard(video);
Starting to think it might just be easier to just avoid using classes. Never had much luck with them.
Now it is complaining of missing behaviors. :(
In case it wasn't completely obvious, I'm hoping to do something like this in script:
delugeVideo video;
keyboard myKeyboard(video);
Starting to think it might just be easier to just avoid using classes. Never had much luck with them.
Don't give up so easily. :)
Manual: Register a value type
According to the manual, when registering a value type, you need to register both the constructor behaviour (to initialize the memory) and the destructor behaviour (to clean up the memory). You only registered the constructor behaviour, that's why it is complaining about missing behaviours.
Alternatively, if there is no initialization or cleanup necessary, then you can register the type with the flag asOBJ_POD (for plain-old-data). Though from your class declaration I do not recommend this, as you need to guarantee that the member pointer is correct.
Let me know if you have any further doubts.
Regards,
Andreas
Manual: Register a value type
According to the manual, when registering a value type, you need to register both the constructor behaviour (to initialize the memory) and the destructor behaviour (to clean up the memory). You only registered the constructor behaviour, that's why it is complaining about missing behaviours.
Alternatively, if there is no initialization or cleanup necessary, then you can register the type with the flag asOBJ_POD (for plain-old-data). Though from your class declaration I do not recommend this, as you need to guarantee that the member pointer is correct.
Let me know if you have any further doubts.
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
Thanks for the reply!
Okay, I have a new question, if you don't mind!
How would I do something like this:
void loadConfig(dVideo *video) {
video.resources_loadConfig("resources.cfg");
video.resources_init();
}
void main() {
dVideo video;
dKeyboard keyboard;
loadConfig(&video);
}
I looked through the object handles section of the manual and I believe I found where it says you use @ in replacement of * and &, but that just errors with "Object handle is not supported for this type"... so perhaps I need to do some more registers?
Thanks once again!
Wil
Okay, I have a new question, if you don't mind!
How would I do something like this:
void loadConfig(dVideo *video) {
video.resources_loadConfig("resources.cfg");
video.resources_init();
}
void main() {
dVideo video;
dKeyboard keyboard;
loadConfig(&video);
}
I looked through the object handles section of the manual and I believe I found where it says you use @ in replacement of * and &, but that just errors with "Object handle is not supported for this type"... so perhaps I need to do some more registers?
Thanks once again!
Wil
Indeed. To support object handles, you must register the type as a reference type.
Manual: Registering a reference type
The difference between value types and reference types is that, references types are allocated on the heap, while value types are allocated on the stack. This allows reference types to live on after the variable's declaration has gone out of scope.
To control the life time of the reference type reference counting is used, so you must register the AddRef and Release behaviours. Also, to allow proper memory management, the reference types use a Factory behaviour instead of a Constructor for initialization.
Regards,
Andreas
Manual: Registering a reference type
The difference between value types and reference types is that, references types are allocated on the heap, while value types are allocated on the stack. This allows reference types to live on after the variable's declaration has gone out of scope.
To control the life time of the reference type reference counting is used, so you must register the AddRef and Release behaviours. Also, to allow proper memory management, the reference types use a Factory behaviour instead of a Constructor for initialization.
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
Does "ref" need to be a new class, or can the reference type be located within the dVideo class that is already registered?
I'm sorry, but I don't understand your question.
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
Popular Topics
Advertisement