Sign in to follow this  
saejox

Create script object handle without an object instance

Recommended Posts

saejox    714
Hi,

here is what i mean
[CODE]
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];
}
}
}
[/CODE]

i use it script like this:
[CODE]
HashTest @t = HashTest();
t.x = 888;
hashmap_int<HashTest@> m;
m[0] = t;
[/CODE]

i am sure it is simple, but just cant figure it out [img]http://public.gamedev.net//public/style_emoticons/default/angry.png[/img]

Thanks.

Share this post


Link to post
Share on other sites
WitchLord    4677
[code]
if (subTypeId & asTYPEID_OBJHANDLE) // handle
{
// equivalent to this, assertion at asASSERT( other.objType->DerivesFrom(objType) );
// values[key] = malloc(sizeof(size_t));

return values[key];
}
[/code]

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:


[code]
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];
}
[/code]

The script also has a problem:

[code]
m[0] = t;
[/code]

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.

[code]
@m[0] = t;
[/code]

Regards,
Andreas Edited by Andreas Jonsson

Share this post


Link to post
Share on other sites
saejox    714
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.

Share this post


Link to post
Share on other sites
saejox    714
i have another related question;

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

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?

Share this post


Link to post
Share on other sites
TechRogue    722
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. Edited by Jake Albano

Share this post


Link to post
Share on other sites
TheAtom    330
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.

Share this post


Link to post
Share on other sites
WitchLord    4677
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this