Advertisement

C++ call AS class member function

Started by January 19, 2008 06:55 PM
14 comments, last by RsblsbShawn 16 years, 8 months ago
Take a look at the \tests\test_feature_source\test_interface.cpp file in the SDK zip. It will probably have all the answers you need. Though the code is not meant to be a tutorial, so it may not be the nicest looking code ;)

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

I looked at that file before, (that's how I got it working this far =)), it still never calls a C++ function passing an AS class that derrived from the interface.

Basically I need to:
C++
void registerEntity(ASInstance i) {  collection.push(i);}void updateAllEntities() {  for each (entity in collection) {    entity->callAngelScriptMethod("update");  }}

AngelScript:
class ASEntity : IEntity {  ASEntity() {    registerEntity(this);  }  void update() {    //whatever  }};


I have no idea how to do that =\ I've tried using any, but I can't cast the any back to an object that can call methods on it. Hopefully this isn't too hard =)

Thanks a lot,
Shawn.
Advertisement
Ah, you're right. My mistake.

Well, it's really quite simple:

void registerEntity(asIScriptStruct *entity){   collection.push(entity);}engine->RegisterGlobalFunction("void registerEntity(IEntity @)", asFUNCTION(registerEntity), asCALL_CDECL);


When you're done with the entity, i.e. when you're removing it from the collection, you need to remember to call the Release method on the asIScriptStruct so that AngelScript can free the memory.

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

Fantastic. Working beautifully.

Thanks so much for your help. If I can interface the rest of my engine with AS and performance/stability is acceptable I'll be donating some $ =)

Thanks again,
-Shawn.
Great!

Let me know if you come upon any performance issues or anything else. I'm always looking for ways to improve the library.

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

edit: wrote a completely nonsensical question because of my misunderstanding of the file to module relationship.

Thanks =)

[Edited by - RsblsbShawn on January 29, 2008 9:40:50 AM]

This topic is closed to new replies.

Advertisement