Note that there are some explanatory texts on larger screens.

plurals
  1. POStatic Class vs Private Instances - Pros/Cons - Read for more information
    text
    copied!<p>I'm trying to figure out which is the best way to design my game's structure.</p> <p>Currently I store class instances everywhere but I would prefer to use a straight up static access.</p> <p>For example I have a class called "<strong>GameSaveSystem.cs</strong>"</p> <p>This class handled obtaining a StorageDevice and all storage related information for all current players.</p> <p>So my current strategy is the following</p> <p>----------------MainClass.cs</p> <pre><code>public static void Main(string[] args) { new Main(); } </code></pre> <p>----------------Main.cs</p> <pre><code>public Main() { GameSaveSystem save_system = new GameSaveSystem(); singlePlayer = new SinglePlayer(save_system); multiPlayer = new MultiPlayer(save_system); } </code></pre> <p>----------------SinglePlayer.cs</p> <pre><code>SinglePlayer(GameSaveSystem save_system) { this.save_system = save_system; } </code></pre> <p>----------------MultiPlayer.cs</p> <pre><code>MultiPlayer(GameSaveSystem save_system) { this.save_system = save_system; } </code></pre> <p>The reason I do it like this is because in Java i could create a new .Java file and have it call "<strong>MainClass.main(args);</strong>"</p> <p>So then Inside that extra class I can gain access to any Static class members or public static instances which are stored throughout the class files.</p> <p>so what i'm wondering is would it be safer to use instances or for this type of situation should i just go with static classes? I want the game to have some kind of security against this types of issues.</p> <p>So below are the 3 ways I've seen for grabbing access to a class</p> <p><strong>Static access - were the person can access this class at anytime from anywhere (This is my preferred strategy for ease of use).</strong></p> <pre><code> ClassB.DoSomething(); </code></pre> <p><strong>Instances stored everywhere - Each every class will have the created instance of a class stored so they have access to it at anytime. Classes have all the instances they will need stored and nothing else.</strong></p> <pre><code>public class A(ClassB classB) { this.classB = classB; } classB.DoSomething(); </code></pre> <p><strong>Instance stored in one class - 1 Class creates a single instance of all the classes which would normally have only 1 instance created. Then you create an instance of this class and store it into all the classes which require access to the previously created instances. Only the classes which stored the main instance will have access to the other instances.</strong></p> <pre><code> public class A(Main main) { this.main = main; } main.getInstanceB().DoSomething(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload