Advertisement

How to properly declare array parameters in methods/functions

Started by May 29, 2006 11:36 AM
4 comments, last by WitchLord 18 years, 6 months ago
Hi, I want to declare a function such that users can pass a string array from scripts like that : STRING[] strArray; // (STRING is a string factory, based on wxString from wxWidgets) MyClass obj; // purpose is obj filling a string array, returning array length (same as using strArray.length() after the call int n = obj.BuildArray( strArray ); I registered it in C++ : r = pEngine->RegisterObjectMethod( "MyClass", "int BuildArray( STRING[]& out destArray ) const", asMETHODPR( cMyClass, BuildArray, ( asIScriptArray& ) const, int ), asCALL_THISCALL ); with method : int cMyClass::BuildArray( asIScriptArray& destArray ) const { // validate array type if( (m_pEngine) && (destArray.GetArrayTypeId() == m_pEngine->GetTypeIdByDecl( 0, "STRING[]" )) ) { destArray.Resize( 2 ); *((STRING*)destArray.GetElementPointer( 0 )) = "one"; *((STRING*)destArray.GetElementPointer( 1 )) = "two"; } return destArray.GetElementCount(); } Then all works fine the first time I execute the script, but if I execute it twice (re-compiling it twice), I run into an assert in asCCompiler::ConvertToVariableNotIn(), on expression ctx->type.dataType.IsReference() I guess it's related to object handles or such (but make some tries without success). Any Idea ? BTW, Is there a tutorial on how receive an array reference (here a string array, but for any other types) in a registered method/function ? Thanks, Lbas
Lbas
There doesn't seem to be anything wrong with your code. I'll have to make some tests to verify that this isn't a bug in AngelScript.

What version of the library are you using?

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
Hi,
Sorry, I didn't mention I use 2.6.0
The problem I encounter sounds like a memory "patch", so it probably comes from my part. I'll try to reproduce it using provided string factory for example, trying to isolate the problem.
Regards,
Seb
Lbas
Hi,
I made the test using your test framework, into test_array.cpp, and it worked, so it confirms the problem comes from my part.
I'm using unsafe references, so I'm looking in that way... I'll let you know.
Regards,
Lbas
Lbas
Hi,
I investigated more on it and found that my function declaration (the registered one) was cleared into the asCScriptEngine from the RemoveArrayType() method (called from ClearUnusedTypes() itself called from Discard() :)). In fact, after running a script, I call Discard(). That's explain why my script works the first time and not the second.

So here's the call stack I got where the array type is cleared:

-->asCScriptEngine::RemoveArrayType(asCObjectType *)
asCScriptEngine::ClearUnusedTypes()
asCScriptEngine::Discard(const char *)
MyEngine::RunScript()

Here's the way I'm compiling and running a script (skiping unrelated parts), Is this correct ? :

asIScriptEngine* pEngine;

.. init stuff, registering etc ..

pEngine->AddScriptSection( NULL, scriptName, scriptText, scriptLen, 0 );
pEngine->Build( NULL )

asIScriptContext* pContext = pEngine->CreateContext();
pContext->SetExceptionCallback( ... )
pContext->Prepare( pEngine->GetFunctionIDByName( "", "main" ) )
pContext->Execute()
pContext->Release()
pEngine->ResetModule( NULL );
pEngine->Discard( NULL );

Is this correct ? In particular, do I need to call both ResetModule() & Discard() between two running scripts ?

Regards,
Lbas
Lbas
ResetModule() reinitializes all the global variables in the module to their original values that they had when you first compiled the script.

Discard() tells AngelScript that you do not intend to use that module any more and that it can free up the resources held by the module.

There is almost never any reason to explicitly call Discard(), as it will be called automatically when the script engine is released, or if you compile a new script into the same module.

You should never run into any assert() so there is obviously a bug in AngelScript, that I'll work to fix as soon as possible.

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