Denaes Posted March 25, 2005 Posted March 25, 2005 I'm doing a game and there are many variables that need to be shared across many classes (and in some cases across libraries). I do not want to just declare things as global and I don't want to deal with a nightmare of passing an object around as an argument every time something needs to get done. I just fear two different classes updating the object at near the same time and it just hurts my head. How about a shared Class? Or whats it called where you don't even have to instantiate it? Just have a class that everyone can access. I'm not quite up to this step, but I'm going to have to seriously plan for it soon. Any advice? Quote
mskeel Posted March 25, 2005 Posted March 25, 2005 This may indicate a need for some refactoring. It may also just be the way it is... Shared in VB, static in other languages, is when the thing you declare as shared is not really associated with a specific instance of an object -- it is shared across all objects of that type. You should use this with care... It sounds like your classes/libraries are ill organized if you have to pass this much data around. I recommend refactoring a lot of your code. If you just want to hack it, what about putting these variables in a module? I think that will give you the global/semi-global (not across libraries) data the you are looking for. Quote
Denaes Posted March 25, 2005 Author Posted March 25, 2005 Theres not really anything to refactor yet. I'm planning out how to have all the objects interact. I need to have a class which drives the game which drives it through the phases (it's turn based with multiple phases in each turn). Maybe I'll have that as my SubMain and load the form from there. I suppose that's where all of the settings will need to be and it can expose them for other classes to set/get as literally about a hundred classes can change/use these settings. As a background, it's a turn based card game which creates a map using one deck of cards... it's basically a card game crossed with a board game. Each player takes on a persona with special attributes and special abilities. Each turn is made up of the following breakdown: 1. Upkeep - everything resets for the turn. Players can't really do anything in this phase. 2. Card - Players may play cards on other players/characters in this phase - rarely on themselves. Any card you play will have a unique effect wich might be instant or perminent. 3. Build - this is where someone takes a card from the build deck and places it to expand the board. 4. Player - this is the phase you move your character around on the board, play cards to help your character and possibily initiate a challenge of sorts with other players in the same room. 5. Discard/Draw - you discard a card and draw so that you have 5 cards. So basically I need to setup each phase and then Wait for the player to do something. throughout the phase they'll be getting/setting information (dealing damage, moving, etc) All the while I need to keep it modular to allow for optional rules. I sense much refactoring going on as I develop. Actually I see this working out with a Game Class with a subclass for each phase, exposing properties and methods specific to each phase and then the Game Class exposing properties and methods specific to the game itself... Right now it's going to be hotseat but I want to at least attempt to compile it for the monoframework for OSX/Linux users and also allow for cross network/internet play. Quote
mskeel Posted March 25, 2005 Posted March 25, 2005 It sounds like you are in a good position to easily make changes, then...the desing phase is the best place to figure this stuff out. I'm sure you figure something out. Have you ever tried using UML or other modelling tools to help with design? That kind of stuff is fairly usefull, as long as you don't go overbaord... Quote
Denaes Posted March 25, 2005 Author Posted March 25, 2005 I can't affort Visio right now, do you know of a free/inexpensive tool for this? I'm planning on getting the MSDN Universal Subscription soon (next few months) which includes Visio. I have two books on this and I know the basics to at least get classes down on diagram (not really sure about their interaction in diagram yet). I've heard Visio can be an unbelievably awesome tool for use with Visual Studio as you and import/export UML into/from classes in the project Quote
Wile Posted March 25, 2005 Posted March 25, 2005 Start with pen and paper :). Seriously. I've got both visio and rational XDE (like rational rose but build into dev studio) available at work, but I start work on sequence and classdiagrams on a whiteboard. I prefer to put down the basic diagrams as a draft first on something where I can quickly rework them. Pen and paper/whiteboard is a lot easier and faster for this than any tool. Only after I'm pretty confident with the design I start to work them into the official XDE design of the project. But it could be just me that starts out in one direction and then ends up going in another direction. About different classes updating the same object at the same time: as long as you purely react on the user interface (your hot-seat scenario) you shouldnt have this problem. I think that supporting network for multiplayer requires a different design than the hot-seat scenario. For network support you probably need a good client / server design where each has its clear responsibilities. Quote Nothing is as illusive as 'the last bug'.
Denaes Posted March 25, 2005 Author Posted March 25, 2005 Maybe it's not good form, but I'm constantly adding private variables as I'm coding and/or changing their names... sometimes changing the public variables. Like for example, each room has 4 potential doors. This is hardcoded as it won't change. I started off with: Exit1 Exit2 Exit3 Exit4 Which really didn't make any sense. I quickly changed it to: ExitNorth ExitEast ExitSouth ExitWest This was more descriptive. Then I realized each door had properties such as a type and potential difficulty (lock/trap). Now I'm at a point where I realize it's far easier to display using: ExitUp ExitDown ExitLeft ExitRight And I'm contemplating creating an entire class to handle the 8 variables and a method or two: ExitInformation +UpType +DownType +LeftType +RightType +UpTarget +DownTarget +LeftTarget +RightTarget getUpString() getDownString() getLeftString() getRightString() -Flip (a method that internally flips Up with Down) Actually that looks like a pretty well developed class. I feel that I can cut it down further by making each of the directions a class also and instantiate them within the ExitInformation Class... Of course this change will play hell with my interfaces :) I suppose I should really do more writing down... and planning and refining before coding. Quote
Denaes Posted March 25, 2005 Author Posted March 25, 2005 To answer my answer, I think I need to have the Game Class be the main class which launches a form. EVERYTHING that needs to be requested or given goes through the Main class. This lets information hang out wherever, but so long as main knows how to get it, then anyone in the application can ask for it. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.