Sign in to follow this  

Unity Process-wide global

Recommended Posts

I am continuing a much older discussion. Motivation: I need to store some global data for a Win32 app framework. There can only be one copy of this global data throughout an entire application. There may be DLLs loaded with their own separate heaps, and I'd like to cleverly redirect their calls to the one 'true' instance. I would like to do this all without delegating the data storage to a dedicated DLL. Construction: I envision users making some sort of Application object in WinMain, thus christening that thread as the main thread of execution, and that module as the executing module. Now I'd like code in other modules to 'magically' find that single instance of data when they need to use it. I'd use a Meyers Singleton to do the lookup and cache the result (read on): The best idea I've had so far is to use SetEnvironmentVariable() and store a text representation of the pointer in there. Unfortunately you cannot just stuff any number in there as SetEnvironmentVariable() dereferences it to copy the string out. So I could turn the address into a string, store it in there, and then turn it back into an address anytime someone needs to access the one and only instance. Seems really hacky. Another good suggestion is to use the registry but that would be even more code. All I really want is to change four bytes such that I can get to this information wherever I am in the process. Thread local storage seems like an ideal solution, except we arrive back at the same problem: how do we determine what index to use for TLS? TlsAlloc() may not always return the same index! Nor can the atom functions be used; they only let you store a key instead of letting you associate a value with it. One idea that may work is to exploit the fact that the HMODULE for the executable is actually a pointer to the exe mapped onto memory. Now this is getting dangerous....:)

Share this post

Link to post
Share on other sites
I figured out a workable solution.

The executable module hosting the framework exports a single function to return a pointer to the process-wide global. If it isn't found in the main loaded module, all modules of the process can be enumerated and a search made. After the proper module is found, the result is cached and used in subsequent operations.

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  

  • Forum Statistics

    • Total Topics
    • Total Posts
  • Similar Content

    • By CpawsMusic
      Hey there! My name is Cpaws and I compose music, sound effects and overall sound design for video games and film. I'm looking to work on a horror game mainly for fun and as an addition to my portfolio. I've used Unity & Wwise before for audio implementation.
      If you're interested in working together, don't hesitate to contact me at
      Here's a few demo reels of my past projects: 
      Here's some snippets of some quick game music I've composed:
      Here's my portfolio/website:
    • By muhamad rabee
      My first mobile game made by unity



      I appreciate every suggestion
    • By Simplepg
    • By Simplepg
    • By ForgedInteractive

      Who We Are
      We are Forged Interactive, a small team of like-minded game developers with the sole purpose of making games we love! We're a team of artists, animators, programmers, level designers, writers, composers, producers, and other creative minds. We want to make games that you, the modern gamer want to play! We hope to build a community that enjoys our games as much as we love creating them. With your feedback and support we will be able to achieve that.

      About the Game
      GAME NAME is a fun, action-packed army builder with unique characters, challenges and engaging levels. Set forth on an adventure to protect friends, family and countrymen from new adversaries. Once defeated your enemies turn coat and join you in your adventures. Players can enjoy a range of troops and abilities based on their gameplay style which become more important as maps introduce more challenging terrain, enemies and bosses. Strong orc knights, dangerous shamans, and even a dragon are out on the prowl. Knowing when to fight and when to run, and how to manage your army is essential. Your actions alone decide the fate of this world.

      Previous Work by Team
      Although we are working towards our first game as a team, our team members themselves have past experience in the industry.
      This includes members who have worked on titles including:
      Final Fantasy Kingsglaive, FIFA, Xcom 2 and Civilization.

      Who are we looking for? 3D Modellers Concept Artists Marketing Specialists Level Designer

      What do we expect? Reference work or portfolio. Examples what have you already done and what projects you have worked on academic or otherwise. The ability to commit to the project on a regular basis. If you are going on a two-week trip, we don't mind, but it would be good if you could commit 10+ hours to the project each week. Willingness to work with a royalty based compensation model, you will be paid when the game launches. Openness to learning new tools and techniques
      What can we offer? Continuous support and availability from our side. You have the ability to give design input, and creative say in the development of the game. Shown in credits on websites, in-game and more. Insight and contacts from within the Industry.
      If you are interested in knowing more or joining. Please email or PM us on Skype. Myself or Colin will reply to you within 48 hours.

      Skype: ForgedInteractive

      David and Colin

      Follow us on:

      Twitter: @ForgedInteract
  • Popular Now