Advertisement

Declaring System objects array

Started by November 17, 2014 10:01 AM
2 comments, last by WitchLord 10 years ago

It seems I have still problems managing arrays ...sad.png

What I am trying to do now is to register a global property which is an array

I have seen many examples where an array is registered with RegisterGlobalProperty as:

"array<Type> @TypeArray"

In my case I the registration returns successfully, but I have a crash in engine->allRegisteredTypes.Erase(cursor), down in an asASSERT(value < 1000000);

I am trying to set the array dimension too, without success.

I tried the following with the indicated outcome:


"array<Type> @TypeArray(10)"   error -10
"array<Type> TypeArray(10)"    error -10
"Type[] @TypeArray(10)"        error -10
"Type[] TypeArray(10)"         error -10
"array<Type> @TypeArray"       crash in engine->allRegisteredTypes.Erase(cursor)
"array<Type> TypeArray"        crash in engine->allRegisteredTypes.Erase(cursor)
"Type[] @TypeArray"            crash in engine->allRegisteredTypes.Erase(cursor)
"Type[] TypeArray"             crash in engine->allRegisteredTypes.Erase(cursor)

Does the "Type" object need to have some particular method or declaration to work?

What else am I missing?

Thanks for helping.

Mau.

Can you show me the code you have for registering the global property? What is the 'Type' type you have?

Even though RegisterGlobalProperty is returning success, it is still possible you're doing something wrong if you're giving it the wrong pointer.

If you're trying to register a property with the signature "array<Type> @TypeArray" then the second argument should be the address of the pointer to the array, and not to the array itself.

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

"Type" was an obvious generalization. My real object "SIVar".

The relevant code to register SIVar type is:


  r = engine->RegisterObjectType("SIVar", sizeof(int), asOBJ_VALUE | asOBJ_APP_CLASS_CD); assert(r >= 0);
  r = engine->RegisterObjectBehaviour("SIVar", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructSIVarDef), asCALL_CDECL_OBJLAST); assert(r >= 0);
  r = engine->RegisterObjectBehaviour("SIVar", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructSIVar),  asCALL_CDECL_OBJLAST); assert(r >= 0);
  r = engine->RegisterObjectMethod("SIVar", "const uint32 GetSelector()", asFUNCTION(GetSelectorSIVar), asCALL_CDECL_OBJLAST); assert(r >= 0);
  r = engine->RegisterObjectMethod("SIVar", "int opImplConv() const", asFUNCTION(IntConvertSIVar), asCALL_CDECL_OBJLAST); assert(r >= 0);
  r = engine->RegisterObjectMethod("SIVar", "uint32 opImplConv() const", asFUNCTION(UIntConvertSIVar), asCALL_CDECL_OBJLAST); assert(r >= 0);
//  r = engine->RegisterObjectMethod("SIVar", "bool opImplConv() const", asFUNCTION(BoolConvertSIVar), asCALL_CDECL_OBJLAST); assert(r >= 0);
  r = engine->RegisterObjectMethod("SIVar", "bool b() const", asFUNCTION(BoolConvertSIVar), asCALL_CDECL_OBJLAST); assert(r >= 0);
  r = engine->RegisterObjectMethod("SIVar", "int32 opAssign(int32)", asFUNCTION(IntAssignSIVar), asCALL_CDECL_OBJLAST); assert(r >= 0);
  r = engine->RegisterObjectMethod("SIVar", "uint32 opAssign(uint32)", asFUNCTION(UIntAssignSIVar), asCALL_CDECL_OBJLAST); assert(r >= 0);

I have a lot of objects of this type and arrays of them living in an external hardware, and the script is not allowed to create/destroy them but only access reading the value (through the implicit conversions to int and uint and possibly bool, when will be allowed) or writing (through the two opAssign).

After declaring this type, I tried to declare those external object withing the script using the RegisterGlobalProperty.

The actual code that does this, actually read the definitions of the objects from an xml file and builds on the fly the RegisterGlobalProperty parameters, so the real code fragment is useless here. But at the end RegisterGlobalProperty receives a string and a this pointer.

The code is:


r = engine->RegisterGlobalProperty(vDef.c_str(), (void*)VarSelector);

Where the VarSelector is actually a handle to each of the existing external object, and the vDef string contains something like:

"SIVar CurrentWs"

"SIVar UsedWs"

"SIVar SystemStart"

....

Finally I tried to register to the script arrays of such objects, and I tried the strings in the original post (just replace Type with SIVar), getting the described errors.

Thanks for any help.

Mau.

OK. So far everything seems to be OK.

You should be able to register an array of this type like this:


CScriptArray *globalTypeArray = 0;
void CreateAndRegisterGlobalArrayType(asIScriptEngine *engine)
{
   // I'm assuming the script array has already been registered in the engine
   
   // Get the type of the array that should be created
   asIObjectType *arrayType = engine->GetObjectTypeByDecl("array<SIVar>");
   
   // Create the array object and store the pointer globally so it will not go out of scope
   globalTypeArray = CScriptArray::Create(arrayType);
 
   // Register the global property referring to the global pointer
   // This option would allow the script to create a different object and reassign the handle to refer to the new array
   engine->RegisterGlobalProperty("array<SIVar> @SIVarArray", (void*)&globalTypeArray);
 
   // alternatively register the array without the @ symbol to prevent the reassignment of it. Observe
   // that this way the address informed to the engine is the address if the actual array object.
// engine->RegisterGlobalProperty("array<SIVar> SIVarArray", globalTypeArray);
}

Don't forget to call Release() on the globalTypeArray before shutting down the engine so the array still knows how the destructor should be called.

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