Module building error

Started by
12 comments, last by luizribeiro 18 years, 8 months ago
You can register the constant like this:

const float pi = float(M_PI);r = engine->RegisterGlobalProperty("const float PI", (void*)π); assert( r >= 0 );


You need to cast to void*, in order to remove the const from the variable. Since the property is registered as const with the script engine, this is still safe.

As for your question about passing an object pointer to the script function: Deyja already gave you a couple of possible solutions, I'll give you a couple more. If the script function is registered to receive the particle object by reference, you can pass the pointer directly, as pointers and references are basically the same thing. Just make sure that the pointer is pointing to a valid object. If you don't want to deal with object handles (which requires a slight overhead of reference counting), nor wish to use the old 1.10.1d version that support pointers, you could register a special type that will hold the pointer to the particle object. You can then register methods on this type that manipulates the object directly. Here's a short example on how that could be done:

engine->RegisterObjectType("particle_ptr", sizeof(Particle*), asOBJ_PRIMITIVE);engine->RegisterObjectBehaviour("particle_ptr", "void f()", asFUNCTION(constructor), asCALL_CDECL_OBJLAST);engine->RegisterObjectMethod("particle_ptr", "void SetPos(float x, float y)", asFUNCTION(setPos), asCALL_CDECL_OBJLAST);void constructor(Particle**p){  // Set the pointer to 0, so that an uninitialized variable won't point to invalid particle objects  *p = 0;}void setPos(float x, float y, Particle**p){  if( *p )    (*p)->SetPos(x,y);}


Although this is fully valid, you'll have to be careful to make sure that the scripts do not access invalid particle pointers. A script could for example store a particle_ptr in a global variable, and if the application then deletes the particle object, the particle_ptr in the global variable would be pointing to an invalid object.

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
Thanks for the replies... But I'm having this problem:
67: const float pi = float(M_PI);68: engine->RegisterGlobalProperty("const float PI", (void*)pi);

While compiling, it gives me this error:
test.cpp: In function `int main(int, char**)':test.cpp:68: error: cannot convert `3.1415927410125732421875e+0f' from type `float' to type `void*'scons: *** [test.o] Error 1


Thanks again,
Luiz Ribeiro
Just a guess, but you probably have to register it with the address of pi, and not the value.

engine->RegisterGlobalProperty("const float PI", (void*)π);

Edit : Now I see why witchlords reply looks like that. & p_i gets changed to &pi. Use the following without the '_':

engine->RegisterGlobalProperty("const float PI", (void*)&p_i);
hah... I didn't knew about this pi 'macro' of the forum... hehe
Thanks ;)

This topic is closed to new replies.

Advertisement