Getting adjacent objects to interact [C#] -


i'm doing basic game in c#, , i'm running on problem can't solve. here's (relevant) code:

public class gamemanager     {         public gamemap mainmap;         public entitymanager gameworld;         public systemmanager gamesystems;          public gamemanager()         {             entitymanager gameworld = new entitymanager();             systemmanager gamesystems = new systemmanager();             gamemap mainmap = new gamemap(61, 41);         }          public void inputhandler(string trigger)         {             switch (trigger)             {                 case "north":                     gamesystems.move(gameworld, mainmap, 0, 8);                     break;                  //etc              }         }     }      public class systemmanager     {        public rkcposition _position;         public systemmanager()        {        }         public bool move(entitymanager targetworld, gamemap targetmap, int targetid, int targetdirection)         {             rkcposition _position = targetworld.getposition(targetid);             // here, getposition returns instance of rkcposition             // pulled list<rkcposition> - seems problem point         }     } 

the problem i'm getting part try call gamesystems.move - jumps highlight last line of code included (w/rkcposition) , gives null ref exception. (rkcposition class i've not included in code snippet)

i'm trying have move function perform changes values within gameworld , mainmap objects. i'm beginning think i'm doing wrong, so...

if want run method on existing instances "gameworld" , "gamesystems" inputhandler function, how properly?

like sami kuhmonen said, problem in code passing around , reassigning objects supposed global. not require pass them parameters need them, incredibly error-prone (as have discovered).

instead of having these objects behave instance objects, utilize singleton design.

what means instead of instance members, these objects static members represent single global object else access.

public class gamemanager {     public static gamemap mainmap;     public static entitymanager gameworld;     public static systemmanager gamesystems;      static gamemanager()     {         gameworld = new entitymanager();         gamesystems = new systemmanager();         mainmap = new gamemap(61, 41);     }      //... } 

now in other classes, instead of having worry if passing around correct object, reference these singletons gamemanager.

public class systemmanager {     public rkcposition _position;      //...      public bool move(int targetid, int targetdirection)     {         rkcposition _position = gamemanager.mainmap.getposition(targetid);     } } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -