Jump to content

  • Log In with Google      Sign In   
  • Create Account


- - - - -

Cannot get C++ polymorphism working with angelscript


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 kyc   Members   -  Reputation: 184

Like
0Likes
Like

Posted 08 July 2004 - 04:53 PM

I've been using WIP#4, and cannot get even the base class working. Here's what I'm doing;
// C++

// in Mesh.h
class Mesh
{
public:
  Mesh();
  virtual Render();
  virtual ~Mesh();
  // Assume overloaded "=" and "new" operators.
};
// end of Mesh.h

// in game.cpp
map<string, list<kee2d::Mesh *> > g_meshes; // Global meshes.
// usage: g_meshes["background"].push_back(pDogMesh);

// Function to be used in the script.
void AddMeshX(const char **layer, const Mesh &mesh)
{
  const char *l = *layer;
  Mesh *pMesh = new Mesh(mesh);
  g_meshes[l].push_back(pMesh);
}

void RegisterGame // Register functions for AngelScript.
{
  engine->RegisterGlobalFunction("void AddMeshX(const bstr &, const Mesh &)", asFUNCTION(AddMeshX), asCALL_CDECL);
}
// end of game.cpp
// in Script.
void main()
{
  Mesh mesh;
  AddMeshX("foreground", mesh);
}
This causes a crash, but I do not know how to make the object in the script become a pointer. Also, now that WIP#5 is available do I still need to overload the new operator? How can I have it like engine->RegisterGlobalFunction("void AddMeshX(const bstr &, Mesh *)", asFUNCTION(AddMeshX), asCALL_CDECL); Note the "Mesh *" and assume AddMeshX(const char **, Mesh *)

Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3226

Like
0Likes
Like

Posted 09 July 2004 - 09:05 AM

You didn't say how you register the mesh type. I assume you do the following:


engine->RegisterObjectType("Mesh", sizeof(Mesh), asOBJ_IS_COMPLEX);


asOBJ_IS_COMPLEX is a new flag for WIP5. It should be used to tell AngelScript that the object in C++ is using either constructor, destructor, or assignment operator. This is important when registering functions that return objects of this type since C++ often treat these objects differently than normal values. The opposite is asOBJ_IS_NOT_COMPLEX.

Since your object needs a constructor you ought to register one with AngelScript as well.


engine->RegisterTypeBehaviour("Mesh", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructMesh), asCALL_CDECL_OBJLAST);

void ConstructMesh(Mesh *empty)
{
// Call the constructor to initialize the memory
new(empty) Mesh();
}

void *Mesh::operator new( unsigned int, void *mem )
{
return mem;
}


Note, that if your C++ class doesn't need to call the constructor you don't have to register this behaviour.

The destructor would be registered in a similar way, however the function could call the destructor directly with ~Mesh().

Once you have the mesh object registered you can register functions that use that type.

AngelScript doesn't have an operator for taking the address of a variable, but I don't think you have to. If you want your function to receive a pointer to the object, you can register the function to take a reference, but have the C++ function receive the reference as a pointer.



engine->RegisterGlobalFunction("void AddMeshX(const bstr &, const Mesh &)", asFUNCTION(AddMeshX), asCALL_CDECL);

void AddMeshX(const char **layer, const Mesh *mesh);



I'm not sure I understood what your question really was. Tell me if what I said help you.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS