FOnline Development > Questions and Answers

AngelScript questions (not related to FOnline API)

<< < (3/5) > >>

Bartosz:
Nope, you still need to do call using provided API.

wladimiiir:
Yeah, I found out that my assumption was incorrect. I actually have no inheritance relationship between my C++ interface and AS implementation class. And it is not possible to create one, I guess.
But the best solution I can do is to create an implementation class of my C++ IFoo interface in C++, that would wrap asIScriptObject and it would call the functions using the API. Something like this:

--- Code: ---class Foo : IFoo
{
    protected:
        asIScriptObject* asFoo;

    public:
        FooScript(asIScriptObject* obj)
       {
            asFoo = obj;
            obj->AddRef();
       }
        ~FooScript() { asFoo->Release(); }

       void FooMethod()
       {
            asIObjectType* type = asFoo->GetObjectType();
            int id = type->GetMethodIdByDecl("void FooMethod()");
            asIScriptContext* ctx = ASEngine->CreateContext();
            ctx->Prepare(id);
            ctx->SetObject(asFoo);
            ctx->Execute();
            ctx->Release();
       }
}
--- End code ---

Then I would just wrap retrieved ScriptObject and use the IFoo's interface methods. ;)

--- Code: ---void RetrieveFoo(asIScriptObject* obj)
{
    if(obj == NULL)
   {
        Log("No object has been passed");
        return;
   }

   IFoo* foo = new Foo(obj);
   foo->FooMethod();
}
--- End code ---

Bartosz:
Yeah, this is similar to how mono' DLR handles AngelScript calls.

I am wondering however, if the whole approach needs such heavy interfacing - maybe tackling it in different manner would help .

wladimiiir:

--- Quote from: scypior on June 10, 2013, 04:39:23 pm ---I am wondering however, if the whole approach needs such heavy interfacing - maybe tackling it in different manner would help .

--- End quote ---
I personally prefer interfaces, to be able to easily extend or change implementation of some feature. Imagine in my case I will have FooScript implementation which will call ScriptObject functions via provided API. But everywhere except my new FooScript constructor I will use interface instead. One day, Andreas Jonsson (author of AS) will implement possibility to register relation between C++ classes and AS classes, so it will allow me to solve my issue like I indented at the beginning. What needs to be done then is to create an instance of other implementation in one line of code. ;)

wladimiiir:
How to set an argument of executed script function from C++, while type of the argument is Critter (or some other class)?

I have this script function:

--- Code: ---void Start(Critter@ critter);
--- End code ---

And I would like to call it from C++, while I have pointer to Critter (Critter*) I would like to pass as the argument and what I am trying to do is this:

--- Code: ---asIScriptContext* ctx = ASEngine->CreateContext();
asIScriptFunction* func = GetObjectType()->GetMethodByDecl("void Start(Critter@)");

ctx->Prepare(func);
ctx->SetArgObject(0, critter);
ctx->Execute();

--- End code ---

But I am getting exception state of the context, with message "Null pointer access". Does anyone know see what I am missing?
(When I call argument-less function, it works flawlessly.)

EDIT:
Additional information from server dump file:

--- Code: ---Exception
Signo   Segmentation fault (11)
Code    Invalid permissions for mapped object, SEGV_ACCERR (1)
Errno   Success (0)
--- End code ---

EDIT (SOLVED):
Me stupid monki. I forgot to set object which the function should be executed on:

--- Code: ---asIScriptContext* ctx = ASEngine->CreateContext();
asIScriptFunction* func = GetObjectType()->GetMethodByDecl("void Start(Critter@)");

ctx->Prepare(func);
ctx->SetObject(GetObject());
ctx->SetArgObject(0, critter);
ctx->Execute();

--- End code ---

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version