Skip to main content
GameDev.net gamedev.net
🔒 Locked

Type casting question

Started by mckracken Sep 6, 2007 at 1:16 PM 6 replies 2.4k views
Original Post
mckracken
mckracken
Hi there! I want to register fopen, fread, fwrite and fclose functions to the angelscript engine, so is there a way to cast an object to void* or char* so I can use it from the script? Or how can I workaround this problem? Thanks a lot for this fantastic library! :)
=====================================Regards,Juan Pablo (McKrackeN) Bettini Psychoban Official Site:http://www.psychoban.comPsychoban on iTunes App Store:http://itunes.apple.com/us/app/psychoban/id378692853?mt=8
WitchLord
WitchLord
Why do you want to cast an object to void* or char*?

I think you'd be best of registering a file class that provides this functionality, rather than registering the functions directly. You'll have better control over the life time of the file handle that way, and you'll be able to provide methods that can read and write the AngelScript array types.

fread and fwrite are not safe for the scripting environment since the script would control how much was read or written, which may cause memory invasion.

If I'll get some time over I'll write a file class as a standard add-on for the library.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
mckracken
mckracken
Could you give me an example on how to implement something like that? Because I don't know how to handle the script objects of unknown types in C++ or register a method or function of unknown types.

For instance, I want to write the binary information of a script class in a file:

// AngelScriptclass Test {    float x;    float y;    float z;}void main() {    write(test, size); // well, i don´t know it's size}// C++void write(void *o, int size) {    // whatever}


So, how can I do something like that?
=====================================Regards,Juan Pablo (McKrackeN) Bettini Psychoban Official Site:http://www.psychoban.comPsychoban on iTunes App Store:http://itunes.apple.com/us/app/psychoban/id378692853?mt=8
WitchLord
WitchLord
You should probably only permit reading/saving primitive types and maybe a select few registered types, such as the string type. How would you for example handle a script trying to read or save a structure with an object handle?

The script that declares the classes will also have to write a method for reading/writing that class.

You could also do something more advanced by implementing a read/write method that takes the var type (see the dictionary class for an example on that), you could then examine the class members via the asIScriptStruct interface and save/load them one by one. But then you'll have to dynamically handle references, either by raising an exception or perhaps serialize them.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
mckracken
mckracken
That's more complex than what I've expected. But well, I have to do it anyway. Thanks!!!
=====================================Regards,Juan Pablo (McKrackeN) Bettini Psychoban Official Site:http://www.psychoban.comPsychoban on iTunes App Store:http://itunes.apple.com/us/app/psychoban/id378692853?mt=8
mckracken
mckracken
Hi there again!

Is there a way to register a method or function that expects an array of a type? i.e:
void func (float *array, int size) {}


Thanks! :)
=====================================Regards,Juan Pablo (McKrackeN) Bettini Psychoban Official Site:http://www.psychoban.comPsychoban on iTunes App Store:http://itunes.apple.com/us/app/psychoban/id378692853?mt=8
kunitoki
kunitoki
actually, i think no.
anyway it is safer to wrap the float* to a std::vector or your own rolled container...

this way you can register a new type in the script, a "FloatArray" and registers also manipulation functions on it, like "size()", "[]" accessors and such...
WitchLord
WitchLord
You cannot register a function expecting a C/C++ array, i.e. a pointer to the first element of the array. But you can register a function that can receive an AngelScript array object.

// Registered as 'void func( float[] &array )'void func( asIScriptArray *array ){   int size = array->GetElementCount();   float *a = (float*)array->GetElementPointer(0);   for( int n = 0; n < size; n++ )   {      a[n] = float(n);   }} 


You can also follow kunitoki's advice and register a std::vector type. It can even overload the built-in array type if you wish. Though I recommend against that, unless you register it with the same methods as the built-in arrays, otherwise you may confuse the script writers.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.