Sign in to follow this  

C++ classes and script

This topic is 3565 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Do you intend to implement something like the object states in UnrealScript?

This could be implemented with something like:


// AngelScript
class 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

Share this post


Link to post
Share on other sites
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

[code]
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

Share this post


Link to post
Share on other sites
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( )");



// AngelScript
class 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 object
asIScriptStruct *gamestate = (asIScriptStruct *)engine->CreateScriptObject(module, "MyGameState");

// Determine the function id for the interface method
int intfId = engine->GetTypeIdByDecl(0, "GameState");
int funcId = engine->GetMethodIDByDecl(intfId, "void Update(float)");

// Call the interface method on the script object
asIScriptContext *ctx = engine->CreateContext();
ctx->Prepare(funcId);
ctx->SetObject(gamestate);
ctx->SetArgFloat(0, time);
ctx->Execute();




Regards,
Andreas

Share this post


Link to post
Share on other sites

This topic is 3565 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this