• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Jason Goepel

Reference Objects as Properties

6 posts in this topic

What is the best way to expose a Reference Type as an Object Property?  
 

class TypeA  // asOBJ_REF
{
};

class TypeB // asOBJ_REF
{
    TypeA m_A;  // ... RegisterObjectProperty("TypeB", "TypeA A", asOFFSET(TypeB, m_A), ...
};

class TypeC // asOBJ_REF | asOBJ_GC
{
    TypeA* m_pA;  // ... RegisterObjectProperty("TypeC", "TypeA@ A", asOFFSET(TypeC, m_pA), ...
};

 
If a script gets a handle to TypeB::m_A and the TypeB object goes out of scope, it seems like m_A would be prematurely destroyed.  How does one inform the garbage collector to not destroy the TypeB object until all references to it's member m_A have been released?
 
The TypeC scenario looks more straight forward, but there is no garuntee that m_pA points to a valid object since the script could assign to it an empty handle.
 
Thanks for the help.

0

Share this post


Link to post
Share on other sites

TypeB is complicated. TypeA is in this case used as a value type in C++ even though it is a reference type. Unless you implement the control to make sure TypeB stays alive as long as there are references to the member you'd do best not to expose this to the scripts at all. Instead I suggest that you use property accessors to allow the script to manipulate the TypeA member without directly accessing it.

 

For TypeC you're correct to use @. If you want to prevent that the pointer is reassigned, then ideally AngelScript should allow the member as &, but that is currently not supported. For now you'll need to use property accessors to make sure the script cannot reassign the handle. In this case you just need to expose a get accessor that returns the handle to the member.

1

Share this post


Link to post
Share on other sites

I'm not sure I entirely understand when the asOBJ_GC flag is needed.  Does the simple situation in my original post justify the asOBJ_GC flag?  Would the asOBJ_GC flag only be necessary if TypeA could reference TypeC?

0

Share this post


Link to post
Share on other sites

asOBJ_GC is necessary if the object can form circular references that will not be resolved by the application.

 

The classes TypeA, TypeB, and TypeC in your example can't form any circular references, 

 

If TypeA could hold a pointer to TypeC too, then a circular reference could be formed between TypeA and TypeC. If the application doesn't keep track of these and resolve them itself then the asOBJ_GC flag and related behaviours are necessary to avoid memory leaks.

1

Share this post


Link to post
Share on other sites

"Unless you implement the control to make sure TypeB stays alive as long as there are references to the member"

 

I wonder how one might do that.

maybe have all children add/subtract there refcount above 1 from there parent object? I had a few other ideas but this seems the simplest:

class RefCount
{
RefCount * mParent;
int mCount;
RefCount(){mCount = 1; mParent = NULL;}
Add()
{
mCount++; 
if (mParent) // We know count is above 1 or the object should of been deleted already, Add is not called for the initial 1 mCount value.
  {
  mParent->Add();
  }
}
Release()
{
if (mParent && (mCount > 1))
  {
  mParent->Release()
  }
mCount--;
if (mCount == 0) delete this;
}
};

Of course, One has to manualy set the mParent of each child 'RefType as a Value type' member in the constructor of the refTypes(your TypeB) object.

 

While I guess one could have RefCount derive from your existing ref counting class and make Add/Release virtual, I suspect the virtual call overhead would cost more then checking mParent and mCount again every release/addition. And one additional pointer per ref object is not a huge deal in most cases.

 

You would then have..

class TypeA : public RefCount // asOBJ_REF
{
};

class TypeB : public RefCount // asOBJ_REF
{
TypeA m_A; // ... RegisterObjectProperty("TypeB", "TypeA A", asOFFSET(TypeB, m_A), ...
TypeB()
  {
  m_A.mParent = this;
  }
};

Thoughts? Any problems with this solution?

Edited by BlackMoons
0

Share this post


Link to post
Share on other sites

On a subnote, the below Release() might preform a little better although might not be as clear.

Release()
{
mCount--;
if (mCount > 0)
  {
  if (mParent)
    mParent->Release()
  }
else 
  delete this;
}
0

Share this post


Link to post
Share on other sites

BlackMoons:

In principle that looks like it should work.  I chose to go with my "TypeC" implementation and property accessors, because the underlying C++ code would better mirror the use of the object in a script.  Your solution would avoid the extra function call involved in accessing the property, which in some scenarios would be valuable.  Thank you for your input.

0

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  
Followers 0