Jump to content

  • Log In with Google      Sign In   
  • Create Account


Davaris1

Member Since 11 May 2007
Offline Last Active May 24 2013 07:06 PM

Topics I've Started

Python 3.3.2 Embedding

24 May 2013 - 08:03 AM

I am following this turorial and am not getting anywhere, with the code that was provided. I am running Python 3.3.2.

 

It returns null on

 

PyObject* pluginModule = PyImport_Import(name);

 

 

static const char* PLUGIN_NAME = "shout_filter";


String<> CallPlugIn(const String<>& ln)
{
    PyObject* name = PyBytes_FromString(PLUGIN_NAME);
    PyObject* pluginModule = PyImport_Import(name);
    Py_DECREF(name);
    if (!pluginModule)
    {
        PyErr_Print();
        return "Error importing module";
    }
    PyObject* filterFunc = PyObject_GetAttrString(pluginModule, "filterFunc");
    Py_DECREF(pluginModule);
    if (!filterFunc)
    {
        PyErr_Print();
        return "Error retrieving 'filterFunc'";
    }
    PyObject* args = Py_BuildValue("(s)", ln);
    if (!args)
    {
        PyErr_Print();
        Py_DECREF(filterFunc);
        return "Error building args tuple";
    }
    PyObject* resultObj = PyObject_CallObject(filterFunc, args);
    Py_DECREF(filterFunc);
    Py_DECREF(args);
    if (!resultObj)
    {
        PyErr_Print();
        return "Error invoking 'filterFunc'";
    }
    const char* resultStr = PyBytes_AsString(resultObj);
    if (!resultStr)
    {
        PyErr_Print();
        Py_DECREF(resultObj);
        return "Error converting result to C string";
    }
    String<> result = resultStr;
    Py_DECREF(resultObj);
    return result;
}

int RunThatPythonScript()
{
    Py_Initialize();
    PyObject* sysPath = PySys_GetObject((char*)"path");
    PyObject* curDir = PyBytes_FromString(".");
    PyList_Append(sysPath, curDir);
    Py_DECREF(curDir);
    String<> input = "Do Something With This Text";
    CallPlugIn(input);
    Py_Finalize();
    return 0;
}
 

 

I was getting the same issue with the official version demo:

 

http://docs.python.org/3/extending/embedding.html#embedding-python-in-c

 

Any suggestions?


Constructors that take parameters?

26 September 2011 - 06:17 AM

I want an object to behave like this in AngelScript, but am unable to get it to work.

// AngelScript Code:
	
ZedScriptW p(ptrToZedEntityID1);
ZedScriptW s(ptrToZedEntityID2);


This part seems to work.

// C++
r = engine->RegisterObjectType("ZedScriptW", sizeof(ZedScriptW), asOBJ_VALUE|asOBJ_APP_CLASS|asOBJ_APP_CLASS_CONSTRUCTOR|asOBJ_APP_CLASS_DESTRUCTOR); assert( r >= 0 );

r = engine->RegisterObjectBehaviour("ZedScriptW", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ZedScriptW_Constructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );
	
r = engine->RegisterObjectBehaviour("ZedScriptW", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(ZedScriptW_Destructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );


However I can't figure out how to create a constructor that accepts multiple parameters.

This is my attempt

// C++

r = engine->RegisterObjectBehaviour("ZedScriptW", asBEHAVE_CONSTRUCT,  "void f(ZedEntityID @)",  asFUNCTIONPR(ZedScriptW_Constructor_ZedEntityID, (ZedScriptW*,  ZedEntityID*), void), asCALL_CDECL_OBJLAST); assert( r >= 0 );

This is the code for the constructor that takes a parameter. The function is called when I run it, but ownerID points to garbage.

// C++

void ZedScriptW_Constructor_ZedEntityID(ZedScriptW *memory, ZedEntityID *ownerID)
{
   new(memory) ZedScriptW();

   if (ownerID) 
 	memory->SetComponent(ownerID->GetOwner());
}

Can anyone help?


Edit:
I think I have figured it out while writing this post. asCALL_CDECL_OBJLAST is the hint. So I have swapped the params around.


r = engine->RegisterObjectBehaviour("ZedScriptW",  asBEHAVE_CONSTRUCT,  "void f(ZedEntityID  @)",  asFUNCTIONPR(ZedScriptW_Constructor_ZedEntityID,  ( ZedEntityID*, ZedScriptW*), void), asCALL_CDECL_OBJLAST); assert( r  >= 0 


void ZedScriptW_Constructor_ZedEntityID(ZedEntityID *ownerID, ZedScriptW *memory)

Hopefully all of the above is correct. If not please let me know. :)

Assertion Error in scriptmgr.cpp

23 September 2011 - 08:18 AM

Hi WitchLord,
I was just testing your demo game and got an assertion of r = -10 at line 59 and line 87, in scriptmgr.cpp.

I downloaded the tarball from here:

http://angelscript.s...elscript/trunk/

It works fine when I use scriptstdstring.h, but when I use my version of it, those functions do not register.

Edit:
I have found my mistake. In my version of scriptstdstring.cpp I used "String" instead of "string".

One Module Per Entity, Or One Modual to Rule Them All?

18 September 2011 - 09:49 PM

What the best way to go about this? I have some code from an old tutorial and they are loading all the files into one module and then calling function names from that module.

// Load weapons script file
if (LoadScript( pScriptEngine, "common.as", MAIN_MODULE ) < 0)
{
pScriptEngine->Release();
cout << "Failed to load script file." << endl;
Pause();
return -1;
}

// Load weapons script file
if (LoadScript( pScriptEngine, "weapons.as", MAIN_MODULE ) < 0)
{
pScriptEngine->Release();
cout << "Failed to load script file." << endl;
Pause();
return -1;
}

// Load main script file that runs in synch with the game loop
if (LoadScript( pScriptEngine, "main.as", MAIN_MODULE ) < 0)
{
pScriptEngine->Release();
cout << "Failed to load script file." << endl;
Pause();
return -1;
}



So is it better to create an additional module for each creature in a game, or should I have only 1 module and have unique "main functions", based on each creature's name?

Creature1Main()
{
}

Creature2Main()
{
}

etc.

RegisterObjectType returns asINVALID_NAME

11 September 2011 - 02:38 PM

I'm trying to get my first script working using the C4 String class. Unfortunately it is hitting an assert and is returning asINVALID_NAME here:

r = engine->RegisterObjectType("String<>", sizeof(String<>), asOBJ_VALUE | asOBJ_APP_CLASS_CDAK); Assert( r >= 0 );


I have replaced string with String<> all through scriptstdstring.cpp, so I am not sure what I should do. Any suggestions?

PARTNERS