Cast object to int Handle and back??

Home Forums Monkey 2 Programming Help Cast object to int Handle and back??

This topic contains 3 replies, has 2 voices, and was last updated by  sascha217 2 months, 2 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #10073

    sascha217
    Participant

    Hallo,

    Is it possible to convert an object to integer and back?

    The following code creates a ‘Memory access violation’:

    Another question: How do I recieve default parameter values from a function DeclInfo?

    #10084

    Mark Sibly
    Keymaster

    This isn’t really recommended as the GC is not aware of ‘handles’ so may reclaim the object at any time if there is not a variable somewhere containing the object while you’re using the handle. Also, an Int wont be big enough to hold an object address on all targets. You’re better off using a Ptr type which will always be exactly the right size.

    But here’s a way to do it anyway. This relies on the fact that, while you can’t currently cast an object to a Void Ptr or similar (perhaps one day), you *can* use Varptr with object variables.

    Note that the ‘New C’ is assigned to a global, not a local. This ensures that the new object is ‘kept alive’ and not prematurely GC’d while you’re hacking around with its ‘handle’.

    I use a similar technique in mojo3d-physics to set the bullet rigidbody ‘user data’ ptr to the Entity the rigibody is attached to. This allows me to quickly find the Entity involved when bullet reports a collision between rigibodies or something.

    It’s safe because the Entity is being kept alive by being in an internal global Scene stack. Destroying an Entity both removes it from its stack (allowing it to be GC’d) and sets its rigidbody ‘user data’ ptr to null.

    This is pretty much the most efficient way to do this sort of ‘binding’ of monkey2 objects with external objects that provide a ‘user data’ ptr or similar, so I think it’s worth people knowing about, but it *is* a bit dangerous so be careful!

    But in general, most games will store Entities in a global stack or list somewhere, in which case it’s generally safe to use ObjectToPointer() on them – until, that is, they are removed from their global stack/list, usually via explicit Destroy() call or similar. After this, the Entity becomes ‘usafe’ as it may be collected by GC at any time, so any pointers to it should be considered invalid. But you don’t generally need the pointer any more anyway as the Entity has been destroyed!

    #10085

    Mark Sibly
    Keymaster

    Another question: How do I recieve default parameter values from a function DeclInfo?

    You can’t, DeclInfo doesn’t know about default parameters.

    #10282

    sascha217
    Participant

    Thank you very much!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.