Jump to content
  • Advertisement
Sign in to follow this  
saejox

Create script object handle without an object instance

This topic is 2209 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

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 Edited by Andreas Jonsson

Share this post


Link to post
Share on other sites
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
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?

Share this post


Link to post
Share on other sites
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!