Advertisement

Create script object handle without an object instance

Started by July 01, 2012 12:17 PM
6 comments, last by WitchLord 12 years, 5 months ago
Hi,

here is what i mean

void *operator[](const T &key)
{
if (values.count(key)) // if exists, just return
return values[key];
else // if not, create
{
if (subTypeId & asTYPEID_OBJHANDLE) // handle
{
// equivalent to this, assertion at asASSERT( other.objType->DerivesFrom(objType) );
// values[key] = malloc(sizeof(size_t));

return values[key];
}
else if (subTypeId & asTYPEID_MASK_OBJECT) // an object
{
values[key] = engine->CreateScriptObject(subTypeId);
return values[key];
}
else // primitive
{
int size = engine->GetSizeOfPrimitiveType(subTypeId);
values[key] = malloc(size);
return values[key];
}
}
}


i use it script like this:

HashTest @t = HashTest();
t.x = 888;
hashmap_int<HashTest@> m;
m[0] = t;


i am sure it is simple, but just cant figure it out angry.png

Thanks.

if (subTypeId & asTYPEID_OBJHANDLE) // handle
{
// equivalent to this, assertion at asASSERT( other.objType->DerivesFrom(objType) );
// values[key] = malloc(sizeof(size_t));

return values[key];
}


The values[key] needs to be populated with a null pointer, and then the address of that null pointer should be returned. I'm not too sure about what your code does, but I'd guess something like this would work:



if (subTypeId & asTYPEID_OBJHANDLE) // handle
{
// Allocate space to hold the pointer (handle)
values[key] = malloc(sizeof(void*));

// Clear the value of the pointer
*(void**)values[key] = 0;

return values[key];
}


The script also has a problem:


m[0] = t;


This script would do a value assignment, i.e. call the objects opAssign method. Since you're returning a null handle, it would result in a script exception.

To do a handle assignment, you need to prefix the left hand expression with @, i.e.


@m[0] = t;


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

Advertisement
oh ok. so much time wasted trying to solve a nonexisting bug.
single @ causes so much grief.
i always think m[0] is already an handle, with @m[0] i am dereferencing it, and getting the value.
but it is the opposite.

thank you.
i have another related question;


values[key] = malloc(sizeof(void*));
*(void**)values[key] = 0;
return values[key];


in here we are returning an empty handle to the script, then this empty handle points to an actual ref counted object.
now c++ has an handle of the object without calling AddRef.

if this is so;
how should i even call AddRef on this object?
is VM wise enough to know it is dealing with an empty handle and increase its ref in our stead?
If the handle is empty (NULL), it doesn't have a refcount. You can't call AddRef, Release, or any other method on a null value.
Yes, the VM is smart enough not to call Release() on an empty handle.

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
By the way, could overloading of the unary prefix * operator be added? It would allow for a more natural syntax of stl-like iterators exposed to the scripts.
The unary * operator is currently not part of the AngelScript syntax. Introducing the operator just to provide overloading for it doesn't feel like a good solution. It would probably just confuse the script writer.

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