🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Type casting question

Started by
6 comments, last by WitchLord 16 years, 9 months ago
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
Advertisement
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 - Tower - free puzzle game

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
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 - Tower - free puzzle game

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
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
actually, i think no.
anyway it is safer to wrap the float* to a std::vector<float> 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...
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 - Tower - free puzzle game

This topic is closed to new replies.

Advertisement