Advertisement

void* declaration

Started by September 03, 2005 02:03 PM
8 comments, last by DaesDemon 19 years, 2 months ago
Hi me again, it doesn't take long until i come back ;) Sorry to disturb again but i don't find a way to declare a void* function. I would like to declare a function like that in AngelScript

void* getObject()
how can i manage that ?
I assume you're using void * to enable you to return any object. If so, take a look at the any type that AngelScript supports.

Search for the test_any.cpp file in the SDK for an example of how to use it.

HTH
Advertisement
Angelscript doesn't have pointer syntax. You could still bind void* as a type; but there are better ways to do this. Any will work, but if your objects have a common base type, a handle to that base type would be even better.
On a 32 bit system, a void * is 32 bits, the same as an unsigned int. You might be able to have some, albeit non-typesafe, success with registering the method as returning a uint if all you need is a pointer to an object.


I frequently use the conversion in my application for specifying memory addresses in the win32 Read and WriteProcessMemory API.
Thanks you very much, mates.

@desertcube
As the void* return really any object in my application and as my application object are not reference enabled, i think i cannot use @ handle on them. So as Any is only able to store handles , i will not be able to use it, if i get it well.

@Deyja and Raindog
If i declare a VoidPtr Type or like an uint, how can i transtype it to get the real type.
For example, let's declare my function like this:
engine->RegisterGlobalFunction("VoidPtr getObject(float)", asFUNCTION(getObject), asCALL_CDECL);
or
engine->RegisterGlobalFunction("uint getObject(float)", asFUNCTION(getObject), asCALL_CDECL);

If i know that is an object of type ObjectA, how can i transtype it in Angelscript, because i have read that
Quote: int, float, and bits can all be converted to and from each other. No other conversions are available.


However i think i found a turnover, but i am to much tired for today :)
You don't 'transtype' it (It's called casting, btw.) You just use the voidptr type you registered inside angelscript. In your C++ code, you use an actual void*.

void* foo() {};engine->RegisterType("VoidPtr",sizeof(void*));engine->RegisterEtc("VoidPtr foo()",asFUNCTION(foo),...);Inside a script -VoidPtr vp = foo();
Advertisement
Yes friend but as i must cast it to use it correctly, i am affraid i will not be able to use it like this.

If you like it better, i have a c++ class with a function which return different type of object without shared base class and i have a string that refer to the object type.
So as i can send to the script the exact declaration, i thought i could have used it like this:
MyRealObject mRealObject = (MyRealObject)myGenericClass.getObject();

But as casting is not possible, i will have to declare the object wanted directly in C++ to Angelscript with the correct type. So i will not have the use of the
void* getObject()
in AngelScript.

By the way, if i understand it correctly and use VoidPtr like you said , i would be unable so use it like this too:
void function(int *);VoidPtr p;fuction(p);


Perhaps an implementation that can be useful is the ability as many Script language to call an interpreter function inside the script (like PHP):
in AngelScriptstring type="RealClassName";string angelscript = type + " myVar = " + PtrToString(getObject());Engine.ExecuteString(angelscript);myVar.realClassFunction();

I don't know if it is really usable in AngelScript implementation but i know that for example in PHP, it can be really useful in some case.
Mainly it seems like a Registration of the engine inside the engine.
So engine object could be used inside script.
Just an idea like that ;)

[Edited by - DaesDemon on September 6, 2005 11:53:41 AM]
You couldn't pass a void* to a function expecting an int* in C++ anyway, so that's irrelevant. Yeah, I see you actually want to use the object afterwards and not just hand it off somewhere else. It would work with a suite of conversion functions, but I'd prefer a type safe solution.
AngelScript only does implicit conversions between primitive types. But that doesn't stop you from registering simple functions that convert for example a VoidPtr to an object. Your application would have to know how to convert the anonymous void* to the true object pointer though.

The application can easily register a function to execute a script string using ExecuteString(). AngelScript itself doesn't have to be modified for it.

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

Quote: Original post by DeyjaYou couldn't pass a void* to a function expecting an int* in C++ anyway
true but , in c++ i could cast it ;)


Quote: Original post by WitchLord
AngelScript only does implicit conversions between primitive types. But that doesn't stop you from registering simple functions that convert for example a VoidPtr to an object. Your application would have to know how to convert the anonymous void* to the true object pointer though.

The application can easily register a function to execute a script string using ExecuteString(). AngelScript itself doesn't have to be modified for it.

Oh yes , haven't think to that, really no need to register the complete engine, just a function that do that . Just executing the script in the same context would be needed. Easy ;)

Yes i see that as a possible solution and in this case i would use it like this:
string realType;          // get it externalVoidPtr p = getObject();string execute = realType + " myVar = p.castTo" + realType + "()";// or// string execute = realType + " myVar = " + p.castToString();ExecuteString(execute);   // registered to execute in the same context.myVar.realFunction();     // the function i want


Hehe that could be done and typesafe as long as realType is valid and as long the VoidPtr have the proper cast function defined.
Or the way of casting it to a string should work too, i guess.
But i think i will keep on my first solution that is to declare the object externally directly with the good type ( as long as i can ) as it seems more rigourous and simple.

Thanks for all the ideas and precisions, i will try to think more to wrapper function now, as it seems to allow many things ;)

[Edited by - DaesDemon on September 7, 2005 9:51:29 AM]

This topic is closed to new replies.

Advertisement