Advertisement

Switching control between angelscript and c++

Started by June 24, 2010 02:06 AM
2 comments, last by philipbennefall 14 years, 5 months ago
I'm currently trying to figure out how to integrate angelscript into my engine.

(You may skip the entire part and jump down to the question: It only explains why I think I need this, however if you can think of a much better way to implement this, then please tell me).

I've settled with the one-module approach, depending if it works or not. Furthermore I've implemented a small entity system like supposed in these forums a few months (or years) ago:

class entity {};class component {};class component_system{     hash_map<entity*,component*>   components};


I've implemented some components in c++ and had to write quite alot code to make them as flexible as possible. I figured stuff would be easier if I would expose the component interface to angelscript and implement those components in angelscript anways, since they don't do heavy stuff (they're simple wrappers around RenderNode, PhysicsActor, etc...). Ofcourse there must be an ai_component that contains the logic for the agents.

//c++void ai_component_system::update(){    for(auto i = components.begin(); i != components.end(); ++i)    {        // interesting part    }}


Now to the interesting part. I imagine that within angelscript I create an actor at some point and add an ai_component to it. The ai_component gets added to the sequence of components and every frame the script calls ai_component_system::update() (indirectly by updating the engine once a frame).

(Actual question)
Is it possible that I return control to the same asIScriptContext that called my function in the first place? I do know the pointer to the object I want to call a method on (which I also know).
What I don't know is if angelscript supports this or not.
// interesting partasIScriptContext* context = (the current context);ai_component* comp = static_cast<ai_component*>(i->second);context->prepare(my_function_id);context->setObject(comp);context->execute();


Second question: Is this a good way to implement what I want to achieve? Maybe I'm looking at this in the wrong way.
If I understand your question, then you can expose a function to AngelScript that calls asGetActiveContext() to get the current context and then calls Suspend() on that context. That should return program control to after the Execute() call.
Advertisement
I think my question was not formulated well (didn't find the right word for it).
Actually I was referring to nesting calls where my application calls a script which in return calls a function of my application which then calls another function of the same script, using the same context. Currently this doesn't seem to work (calling asIScriptContext::Prepare returns that the context is currently executing). So it seems I need to create another context that executes the inner script function.
I had a similar problem a while back where I wanted to accomplish the same thing for the purposes of implementing nested callbacks. I asked Andreas about it and was told that nested calls on the same context are not currently possible, but that he'd consider it for the future and that the best way currently was to create a context pool.

Kind regards,

Philip Bennefall

This topic is closed to new replies.

Advertisement