• 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
Gavin Williams

Constructing / Loading resources asynchronously

4 posts in this topic

Hi,

 

I've started using async / await as I'm taking a look at WinRT. But Im having a terrible time getting to grips with async usage. Suddenly all of my objects have 3 'constructor' methods ...

 

 

public someModel(string modelAsText) // the constructor
 : base(modelAsText, VertexElementTypes.Position, VertexElementTypes.Normal, VertexElementTypes.Texcoord)
{
    // now often empty
}
 
public override void Initialize(DeviceManager devices) // called when graphics data needs to be loaded and bound to the device
{
    // setup any programmatic/fast data
    ....
    // setup any loadable/slow data
    LoadData(devices) // calls the async method
}
 
private async void LoadData(DeviceManager devices) // the async method to do the actual loading and binding
{
    ...
    IsReady = true; // flag indicating that all data is now loaded.
}

 

 

One thing that's starting to annoy me about async is that all my constructors are emptying. And all the construction code is having to be moved into the Initialize and async LoadData methods. I can't understand why a constructor can't be asynchronous. In my mind, it absolutely should be because like any object being constructed, it is immediately identfiable but isn't ready to be used until all its parts are in place. It seems to me that the language isn't designed correctly to handle this. I've read comments on the topic of async in constructors and I understand the reasoning .. early disposal causing leaks and object usage before its ready, but I would argue that the reasoning is only necessary because the model is flawed. That discussion isn't really helping me solve the problem, but I think it's a part of the topic.

 

Another problem I am seeing is that even though the file accesses are asynchronous, the awaits in LoadData cause the LoadData method to be synchronous (don't they ?) and therefore the thread has to wait anyway for LoadData to complete, so what's the point. The initialize method in this case is synchronous, so all the asyncronous code in the world can't make the drive load data any faster.

 

I'm really not sure about this, because when I run the app, the GUI does come up and the model pops into existence when it's loaded. So it actually seems to be doing what I want. And if i remove the IsReady flag I get a few frames where the states and shaders aren't set yet. So it seems to be running asynchronously !

 

Q : does the LoadData call create it's own thread and does execution immediately continue ( in this case, returning from the Initialize method immediately ) while the LoadData method contiues on it's way ?

 

I'd like to know what patterns people are using to initialize models and resources asynchronously.

 

Thanks for any help or advice.  Gavin

Edited by Gavin Williams
0

Share this post


Link to post
Share on other sites

Thanks both for the ideas. I have been using a resource manager, really just a resource loader (in the project that I am now rewriting from scratch), but not exclusively, I also have some resources loaded and maintained by certain classes. I think it's messy to have both, but my opinion shifts on which is better as I explore different patterns.

 

@ y2kia - what's interesting is how you extend the idea of the resource manager to have a queue and and to be able to report progress / availability and deal with that if need be. Those are good extensions to the resource loader. And i can see how that's going to clear up my view of resource loading verses game-object creation. They can be, and are different processes. I was thinking last night about using an enumeration { init1, init2, ... , initN, IsReady } to track cumulatively object data loading states. But again, a resource manager might just make all that business easier to understand and will certainly tidy up my game-objects classes.

 

Some great suggestions. I'll play around tonight with some of these ideas.

0

Share this post


Link to post
Share on other sites

Gavin,

 

In my commercial programs (not games) disk access is passed off to a single thread of its own, and I just use Synch access within that thread. The Disk cant read two things at once, but you obviously dont want that disk access on your UI thread, so I just place the read into a Queue<>() on the DiskIO thread with a callback when its ready by passing somehting like this; (psuedocode)

 

struct FileRequest

{

   public string FileName;

   public void delegate CallBack;

}

 

My disk IO thread checks to see if a request is already queued for that file name - if it is I add the new CallBack to a list of callbacks for that request, then discard the new request, otherwise I add the new request to the queue.

 

Making this code synch inside the disk manager thread makes it easier to unit test as well, because it doesn't rely on asynch semantics or code structures. I can also batch together multiple calls into a batch request and only get a callback when they've all completed.

 

Maybe this will help your code be cleaner; maybe not.

 

Phillip

1

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