🎉 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!

C++ classes and script

Started by
2 comments, last by WitchLord 16 years, 4 months ago
So I've been using Angel Script off and on for over a year now, and so far i've been very impressed with the advancements and the current state of it. I still have a few things that I just can't figure out how to do. Heres my goal I want to create a generic State Machine driven C++ system that calls into more specialized Script. So the way my engine is setup right now everything is a state with certain events that are called on that state. I'd like to move my state system to script where I could do something like this. Just to use a simple example GameState RocketLevel; RocketLevel::StateEnter( ) { } I thought about doing something like this at one point string className = "RocketLevel"; GameState RocketLevel = new GameState( className ); Then in source generate the event calls to RocketGame::StateEnter and treat them as global functions, while passing in a pointer to the actual object being used. My main goal for doing this is to be able to write AI and eventually be able to queue up the events it responds to. So in the StateInitialize event I would add from script to source what events the AI entity will use. Has anyone built a state based script system using AngelScript? Any advice on how to approach this would be very appreciated. Adam Larson Game Programmer Green Bay Wisconsin
Adamhttp://www.allgamedevelopment.com
Advertisement
Do you intend to implement something like the object states in UnrealScript?

This could be implemented with something like:

// AngelScriptclass MyObject{  MyObject()  {     // Set the initial state to sleeping     SetState("Sleeping");  }  void Sleeping_OnTouch()  {     // Do something when touched while sleeping     ...       // Move the state to awake     SetState("Awake");  }  void Awake_OnTouch()  {     // Do something different when touched while awake     ...  }}


SetState is an application registered function that performs the state change.

From the application side you would determine the function to call by concatenating the state name with the event name, e.g. state = "Awake" and event = "Touch" -> "Awake_OnTouch".

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

Thank you thats almost exactly what I was hoping to do.

I'm also wondering about one additional thing. I've heard reference to an interface with AngelScript. I guess I'm wondering if I could take an abstract C++ class or one that at least has virtual inheritance and derive from it in script. So something like this.

C++

class GameState{public:GameState( ) { }virtual GameState( ) { }virtual void Update( float dt ) = 0;virtual void Enter( ) = 0;virtual void Exit( ) = 0;}


AngelScript


class TitleScreen : public GameState
{
TitleScreen( )
{
LoadScene( "title.as" );
}

void TitleScreen::Update( float dt )
{
if( KeyPressed( "ENTER" ) )
{
ChangeState( "MainMenu" );
}
}

void TitleScreen::Exit( )
{

}
}


I want to be able to create this object in script and have C++ call back to these script functions.

Thanks again

Adam
Adamhttp://www.allgamedevelopment.com
AngelScript can't inherit from C++ classes. Not yet at least. In fact AngelScript doesn't have support for inheritance yet. However, AngelScript does support interfaces, which will allow you to use common polymorphing techniques.

// C++engine->RegisterInterface("GameState");engine->RegisterInterfaceMethod("GameState", "void Update( float dt )");engine->RegisterInterfaceMethod("GameState", "void Enter( )");engine->RegisterInterfaceMethod("GameState", "void Exit( )");


// AngelScriptclass MyGameState : GameState{  MyGameState() {}  void Update( float dt ) { /* do something */ }  void Enter( ) { /* do something */ }  void Exit( ) { /* do something */ }}


All script classes that implement an interface like this are guaranteed to have the method that the interface declares. And you can call the methods on a handle to an interface without having to worry about which object type it is under the interface.

// Create the script objectasIScriptStruct *gamestate = (asIScriptStruct *)engine->CreateScriptObject(module, "MyGameState");// Determine the function id for the interface methodint intfId = engine->GetTypeIdByDecl(0, "GameState");int funcId = engine->GetMethodIDByDecl(intfId, "void Update(float)");// Call the interface method on the script objectasIScriptContext *ctx = engine->CreateContext();ctx->Prepare(funcId);ctx->SetObject(gamestate);ctx->SetArgFloat(0, time);ctx->Execute();




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