Advertisement

Question about casting references

Started by July 05, 2007 01:13 PM
4 comments, last by WitchLord 17 years, 4 months ago
Hi there! Don't worry, it isn't a bug report this time! ;) Suppose I have a class declared in AngelScript. Something like:
[source lang=cpp]
class Packet {
    float x,y;
}

And a C++ function registered in the script engine that expects a char* and a size (let's say "void sendPacket(char*, int size)"). Is there a way to do (in AngelScript) something like:
[source lang=cpp]
class Packet {
    float x, y;
}

void Main (){
    Packet p;
    sendPacket((void@)@p, sizeof(p));
}

Or is it a work-around for that kind of problem?
=====================================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'll not be able to use the C++ function sendPacket directly like that. However, you should be able to write a function that takes parameter and then transforms it into the format you need for sendPacket.

One possible solution:

// AngelScriptinterface Packet;class MyClass : Packet{   float x,y;}void Main(){  MyClass c;  sendPacket(@c);}


On the C++ side you have:

// C++void sendPacket(asIScriptStruct *pkg){  // Use the methods of asIScriptStruct to obtain the information about members to build the package  sendPacket(buffer, size);}// Register the functionengine->RegisterGlobalFunction("void sendPacket(Packet @)", asFUNCTIONPR(SendPacket, (asIScriptStruct *), void), asCALL_CDECL);


Another option is to use the 'any' type, which is a container class that can hold an object handle of any type. The C++ function then receives a asIScriptAny instead of a asIScriptStruct and the script looks like:

// AngelScriptclass Packet{   float x,y;}void Main(){  Packet p;  any a;  a.store(@p);  sendPacket(a);}


A third option would be to register a package class from the application and have the script itself add the information it wishes to inside the package before sending it, i.e. package::AddInt, AddFloat, AddString, etc.

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

Advertisement
Thanks!

But how can I know the size of the struct in each case?
=====================================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'll have to enumerate the properties of the script class to determine that. You'll most likely want to encode your packages in some specific way, so it won't be much use for you to have AngelScript tell you how much memory a script class uses. Besides, the script class holds a lot of hidden members that you don't want to pass into the package (since they wouldn't make any sense anywhere else).

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

But is there a way to cast again to the original type?

That is if I send a packet, I want to recieve it from another client. So I will receive a "Packet" or "any" type and I want to cast it to it's original type.
=====================================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 may want to look into the concept of serialization. This is what you're trying to do.

Basically it's a matter of storing enough information to be able to reconstruct a representation of the original object.

You'll have to store the name of the script class you're storing in the packet so that on the other end you'll be able to create a new script class of that type, and then fill it's members with the values stored in the packet. You'll create the new object by using the methods GetTypeIdByDecl and CreateScriptObject.

Remember that the type id may not be the same in both engines, which is why I suggested storing the name of the type. You could also use a mapping of the type id which may be more efficient but also more complex to implement.

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

This topic is closed to new replies.

Advertisement