- Home /
Is it possible to control the order of a static's initialization?
I am aware that in order to create the idea of global variables, you need to use static ... but I'd use static properties, not variables so I can capture the change events. What I was planning on doing is having a dummy object in the corner of my scene that contained all of my statics.
The question is, when the scene is loading, is there any way to force the dummy object to always load first? Is there a declarative I could use, or perhaps the object name that guarantees it's first?
What are you trying to achieve? static
properties don't get "initialized" in any way you can cause side effects so I don't see how those are relevant in any way.
$$anonymous$$aybe you can give an example to make it more clear?
I am trying to initialize an interface to a database. So I have to open the database via a connection string, set up some parameters, initialize some of my parameters etc. Other statics will assume the database is loaded. This is part of the initialization of the game.
Or should I just implement some kind of lazy load and let the first call do the initialization?
Yes, DB always lazy. Also, this sounds like you're trying to keep the database connection open, which usually is a bad idea. You should just open a conenction if needed and then close as fast as posibble when the work is done. Database connection pooling is usually implemented a level above your application layer, e.g. by ADO.NET. And if you need to do extra work the first time, just set a flag and check it in the central method that returns a connection or somwhere similar.
I'll implement it without keeping the connection open. Thanks.
Answer by HappyMoo · Jan 19, 2014 at 01:05 PM
You don't need a dummy object.. statics work without objects... that's the idea. Just create your static class and that's it. You can access it everywhere... no need for a dummy object.
Fine, but how do I make sure those statics in that class are executed first before any other statics in any other class?
What are you trying to do? I have the feeling you're trying something that can be solved easier in another way. All static members get lazy-initialized before you try to access any static member or before any object of that class is instantiated. So, nothing is happening before you try to access the static members for the first time anyway.
Answer by jaroosh · Jan 22, 2014 at 08:40 PM
how statics work in .Net and I'm assuming youre using c# scripts is they will get defined (initialized) the first time they're used so you dont have to worry about initialization. However, if you're using statics for something more than just constant values (consts are also static internally), using statics is not the best choice imo as it makes the code less testable and hard to maintain when the project gets big so I'd say its way better to use a Singleton pattern and define those global variable scripts as singletons, add them to an empty object in the scene and use script execution order (http://docs.unity3d.com/Documentation/Components/class-ScriptExecution.html) to ensure that your variables are initializes when they need to