Note that there are some explanatory texts on larger screens.

plurals
  1. POSingleton & Inheritance C#
    primarykey
    data
    text
    <p>First i'll just state that I'm using singletons that won't actually be around throughout the entire app life time. It's more a way of encapsulating what is going on from the user.</p> <p>I have several GameStates, eg InGame or MainMenu, and these have some quite similar function calls so I thought to use inheritence to stop copy / pasting. The following code is what I have, but it does not work as I intend. Here it is:</p> <p>BaseState.cs</p> <pre><code>abstract class BaseState { protected static BaseState mHandle = null; protected static BaseState Handle { get { return mHandle; } set { mHandle = value; } } public static GameState UpdateState(GameTime gameTime) { GameState g = GameState.MainMenu; try { Handle.Update(gameTime); } catch (Exception e) { } return g; } public static void DrawState(GameTime gameTime, SpriteBatch spriteBatch) { Handle.Draw(gameTime, spriteBatch); } public static void Release() { mHandle = null; } protected abstract GameState Update(GameTime gameTime); protected abstract void Draw(GameTime gameTime, SpriteBatch spriteBatch); } </code></pre> <p>InGame.cs</p> <pre><code>class InGame : BaseState { private InGame() { } protected static new BaseState Handle { get { if (mHandle == null) { mHandle = new InGame(); } return mHandle; } set { mHandle = value; } } protected override GameState Update(GameTime gameTime) { return GameState.Quit; } protected override void Draw(GameTime gameTime, SpriteBatch spriteBatch) { } } </code></pre> <p>You can probably tell that I want to be able to use the <code>get</code> and <code>set</code> of <code>InGame</code> in the <code>BaseState</code> so I can simply call Handle.Update() and no matter if I called it from InGame or Menu or whatever it would know which code to use.</p> <p>Obviously I need to brush up on my OO skills. But if anyone could suggest a way to get this to do what I would like it to, or to suggest a different way of going about it I would be grateful. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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