XNA Creators Club Online
Page 1 of 2 (45 items) 1 2 Next >
Sort Posts: Previous Next

Passing Varaibles Between Classes

Last post 8/7/2009 6:36 PM by Kamikazi Uk. 44 replies.
  • 8/1/2009 11:50 AM

    Passing Varaibles Between Classes

    Hi,

    Basicly i can't figure out how to wire my code with game1. Basicly all i want to do is cleanup my code to separate classes and then that incorporates into game1 so i don't have a giant game1 class which i have to keep scrolling all the way to find certain code. So basicly all i need to know is how to make my class code go to game1 when it's compiled kinda thing.

    Thanks.
    3D Artist & Programmer:
    Portfolio - Team
  • 8/1/2009 12:49 PM In reply to

    Re: Wiring Code to Game1

    Hi there,

    Check out the replies to this post - it should give you some ideas.

    Cheers,

    Rod
    Rod MacDougall
    Gallus Games | Twitter
  • 8/1/2009 1:07 PM In reply to

    Re: Wiring Code to Game1

    I can't get this constructor working.

    Basicly what i'm wanting to do is send variables from one class to another but it's not working. Also tryed public variables and still not seeming to work.

    My Code:
    Player Class:
    http://pastebin.com/mf51115a
    Game1:
    http://pastebin.com/m80e2458

    So i'm having trouble to link it togeather so that i can use my player variables and stuff in my game1 class. I'm really only doing this to cleanup up more i don't like having one big class.

    Please help if you know what seems to be wrong....:s
    3D Artist & Programmer:
    Portfolio - Team
  • 8/1/2009 6:07 PM In reply to

    Re: Wiring Code to Game1

    Just at a glance, you definitely don't want your Player class to be inheriting from your game class. So change this line

    public
    class Player: Game1

    Player is definitely not a new instance of a game. You're going to introduce quite a few issues just by trying to do that. Second, if you want to change variables/object values in your Player class from your game class you just make them public.

    So player.ringsSpeed = 6 would change the value of that variable in your Player class. (also you might want to start naming your object more differently than just doing upper and lower case Player player has great potential for tripping you up when coding due to them only being different by an Upper case "P" give the variable name more of a difference to make it clearer. Player mainPlayer; for example would help make the distinction).


  • 8/2/2009 3:06 AM In reply to

    Re: Wiring Code to Game1

    You should really start with reading through everything in the Fundamentals box of C# Tutorials and How Do I Learn MSDN page. C# like everything else in life except maybe breathing, is something you must learn by studying, it's not like a piece of software that you can just throw away the manual and pick up as you go along. To be a successful programmer you need to learn as much as you can, even when you think your really good it's still a good idea to put aside at least a couple hours each week to studying new things about the language and libraries you use. Even after several years of working with C#, I am still learning new things almost every day, and each time I do things get a lot easier along with ideas I just couldn't tackle in the past becoming trivial.
  • 8/2/2009 1:30 PM In reply to

    Re: Wiring Code to Game1

    I have been reading tutorials on the web. I have been programming for over 2 years now doing c++ and recently being learning c#. I know most things what I'm doing as i have been reading learning xna 3.0. Just I'm confused how to make code in one class such as some public variables and use them in another class.
    3D Artist & Programmer:
    Portfolio - Team
  • 8/2/2009 2:03 PM In reply to

    Re: Wiring Code to Game1

    Kamikazi Uk:
    I have been reading tutorials on the web. I have been programming for over 2 years now doing c++ and recently being learning c#. I know most things what I'm doing as i have been reading learning xna 3.0. Just I'm confused how to make code in one class such as some public variables and use them in another class.


    In C#, use references instead of pointers.

    in C++

    Foo *fooPtr = new Foo();

    in C#

    Foo fooRef = new Foo();

    With 2 years of C++ the above should be all you need to get going.
    Game hobbyist hell-bent on coding a diabolical Matrix
  • 8/3/2009 12:06 PM In reply to

    Re: Wiring Code to Game1

    I'm not using any floats in my code craig ???.

    Anyway i got this sorta working i think. But my update class has a error:

    Error    1    'AnimatedSprites.Player.Update(Microsoft.Xna.Framework.GameTime)': cannot change access modifiers when overriding 'public' inherited member 'Microsoft.Xna.Framework.GameComponent.Update(Microsoft.Xna.Framework.GameTime)'    C:\Documents and Settings\MG1\My Documents\Visual Studio 2008\Projects\Other\Backups\Bomb It !! (Clean Code)\AnimatedSprites\Player.cs    46    30    AnimatedSprites

    Here is my code:

    Game1:
    http://pastebin.com/m767dad34

    Player Class:
    http://pastebin.com/m7501c3a6

    I have had a read up on constructors but don't exactly get how they are supposed to work. Do i make my constructor for example:

    public SpriteManager(Game game)
                : base(game)
            {
            }

    Then do another in game1 class

    public Game1(Game game)
                : base(game)
            {
            }

    Is that how they communicate between variables or not. I don't get how they work. Would mean a lot if you can maybe explain for me abit more please.

    Also what seems to be issue with my code.

    Thanks.
    3D Artist & Programmer:
    Portfolio - Team
  • 8/3/2009 1:39 PM In reply to

    Re: Wiring Code to Game1

    Hey man, just thought I would throw in my $.02 and point out a couple of things. First, You have this code in your post:

    public SpriteManager(Game game)
                : base(game)
            {
            }


    public Game1(Game game)
                : base(game)
            {
            }

    which is kinda weird. Game1 should inherit from Microsoft.Xna.Framework.Game and should pretty much be the only class in your game that does. Now, if you want to create a class, its fairly simple. Just go to your solution explorer, right click on the name of the project right under the solution, and select Add > Class. This should bring up a nice little window that lets you name the class, we'll call it SpriteManager.cs

    The IDE will generate a skeleton that looks something like this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace YourProjectName
    {
      class SpriteManager
      {
      }
    }


    Since you want to easily use this, just append your class like so:

    namespace YourProjectName
    {
      public class SpriteManager
      {
      }
    }


    lets give it something to hold and a constructor

    namespace YourProjectName
    {
      public class SpriteManager
      {
        //no one can access this
        private uint m_Data;
       
        //but anyone can call this
        public SpriteManager()
        {
           m_Data = 0;
        }
      }
    }


    Now, over in Game1.cs, you can just say somewhere:

    SpriteManager spriteMan = new SpriteManager();

    and, if you've followed me, you'll have a new SpriteManager sitting around which has private data you can't access. Fun, right? Let's say you want to be able to set that data, then we'll add this:

    namespace YourProjectName
    {
      public class SpriteManager
      {
        //no one can access this
        // This particular thing is called a Field.
        private uint m_Data;
       
        ///<summary>
        /// This allows you to get/set the data instead of hassling with c++ Getters and Setters.
        /// This particular thing is called a Property. and you get the groovy documentation stubbing by typing '///' without the quotes.
        ///</summary>
        public uint Data
        {
           get { return m_Data; }
           set { m_Data = value; }
        }

        //and here is your constructor
        public SpriteManager()
        {
           m_Data = 0;
        }

        //you can even overload the constructor, like this
       public SpriteManager(uint data)
       {
         m_Data = data;
       }
      }
    }


    once you've done all that, you can do more stuff over in Game1.cs, or any other .cs file in your project, like this

    uint someVal = 3;

    SpriteManager bill = new SpriteManager(someVal);

    uint anotherVal = bill.Data;

    anotherVal += someVal;

    bill.Data = anotherVal;


    and now bill's m_Data field will be equal to 6.

    anyway, I hope this helps. Good luck!
  • 8/3/2009 1:49 PM In reply to

    Re: Wiring Code to Game1

    That error is simple. Read it over again. Your player class is a sub class of GameComponent. GameComponent has a public virtual method called Update. In your player class, you have an override Update method, but it isn't public so the signature doesn't match GameComponent's. To make them match, make the Player Update method public.

    Constructors are functions that create instances of classes.

    public class A  
    {  
        // A field  
        private int foo;  
     
        // Constructor  
        public A(int foo)  
        {  
            // Fill the class' field with a value in the constructor  
            this.foo = foo;  
        }  
    }  
     
    public class B  
    {  
        // This is a field with a type of my custom class  
        private A a;  
     
        public B()  
        {  
            // Call the constructor and put the constructed instance  
            // into the field member of class B.  
            this.a = new A(16);  
        }  

    Constructors also bring along some extra syntax when you look at object inheritance as any sub-class will probably want to call a constructor from its base class.

    // My custom class inherits from GameComponent  
    public class A : GameComponent  
    {  
        private int foo;  
        private string bar;  
     
        public A(Game game) : base(game)  
        {  
            // GameComponent's constructor is called.  
            // Its signature requires a Game instance  
            // to be passed in, so that's what we give it.  
            // The GameComponent parent class gives us this  
            // Game member which we can use to reference  
            // other objects and values.  
            foo = Game.Components.Count;  
        }  
     
        public A(string bar, Game game) : this(game)  
        {  
            // This constructor calls the other constructor in  
            // this class. That constructor calls the base class,  
            // so we have a chain of constructors being called now.     
            this.bar = bar;  
        }  

    Constructors are going to be the first code that runs when you want to use a class. In XNA, you start by writing code into Game1 to set up your environment in pretty much the same way you start with int main(int args c, string[] args) in C++.

    public Game1()  
    {  
        // First class constructor  
        A a1 = new A(this);  
        // Add the component to the Components list  
        Components.Add(a1);  
     
        // Use the second constructor and add it  
        Components.Add(new A("Hello, World!"this));  
  • 8/3/2009 2:22 PM In reply to

    Re: Wiring Code to Game1

    I fixed almost all the problems now.

    "My player doesn't render though and there are no errors."

    Code:
    Game1:
    http://pastebin.com/m2af1bfc0
    Player:
    http://pastebin.com/m191b1b84

    Please help :P.
    Thanks so much so far.
    3D Artist & Programmer:
    Portfolio - Team
  • 8/4/2009 1:37 PM In reply to

    Re: Wiring Code to Game1

    Assuming you Player DrawableGameComponent is working you just need to add it to your game project.

    So you would need the following member field:

    Player player;

    And then, in the Game's constructor you need to add the following code:

    player = new Player(this);
    Components.Add(player);

    Hope this gets you further along,

    Chad



    Chad Carter
    Microsoft XNA Game Studio 3.0 Unleashed - Book contains information on 2D, 3D, HLSL, Content Pipeline, XACT, Particle Systems, AI, Physics, Game States, Performance, Zune and 4 full games (2 of which are multiplayer)!

    Twitter

    XNA Essentials
  • 8/4/2009 2:00 PM In reply to

    Re: Wiring Code to Game1

    Kamikazi Uk:
    I fixed almost all the problems now.

    "My player doesn't render though and there are no errors."

    Code:
    Game1:
    http://pastebin.com/m2af1bfc0
    Player:
    http://pastebin.com/m191b1b84

    Please help :P.
    Thanks so much so far.


    Yeah Chad is correct, I don't see where you are instancing your player class in your main game code. You need to create a new instance of this class for each player you need.
  • 8/4/2009 3:26 PM In reply to

    Re: Wiring Code to Game1

    Thanks a lot. I forget to add them lol.

    Anyway for some reason whatever i use in my player classes loadcontent() it doesn't find for some reason.

    Code:
    protected override void LoadContent() 
            { 
                spriteBatch = new SpriteBatch(Game.GraphicsDevice); 
                ContentManager Content = new ContentManager(base.Game.Services); 
     
                ringsTexture = Content.Load<Texture2D>(@"Images\plus"); 
     
                base.LoadContent(); 
            } 

    3D Artist & Programmer:
    Portfolio - Team
  • 8/4/2009 3:39 PM In reply to

    Re: Wiring Code to Game1

    Maybe set the RootDirectory property of the ContentManager?
    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 8/4/2009 3:55 PM In reply to

    Re: Wiring Code to Game1

    What do i need to set it to.

    Thanks.
    3D Artist & Programmer:
    Portfolio - Team
  • 8/4/2009 3:59 PM In reply to

    Re: Wiring Code to Game1

    Unless you're doing something out of the ordinary, just "Content".
    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 8/4/2009 4:10 PM In reply to

    Re: Wiring Code to Game1

    I don't fully understand what part to do.

    Do you mean change to this:
    ContentManager Content = new ContentManager(base.Game.Content);

    because it don't work.

    3D Artist & Programmer:
    Portfolio - Team
  • 8/4/2009 6:45 PM In reply to

    Re: Wiring Code to Game1

    Kamikazi Uk:
    I don't fully understand what part to do.

    Do you mean change to this:
    ContentManager Content = new ContentManager(base.Game.Content);

    because it don't work.



    He literally means:

    Content.RootDirectory = "Content";

  • 8/4/2009 6:52 PM In reply to

    Re: Wiring Code to Game1

    When i use that i get this error.
    + The name 'Content' does not exist in the current context (Line 42)
    + The name 'Content' does not exist in the current context (Line 44)

    Code:
    protected override void LoadContent() 
            { 
                spriteBatch = new SpriteBatch(Game.GraphicsDevice); 
                Content.RootDirectory = "Content"
     
                ringsTexture = Content.Load<Texture2D>(@"Images\plus"); 
     
                base.LoadContent(); 
            } 

    3D Artist & Programmer:
    Portfolio - Team
  • 8/4/2009 7:13 PM In reply to

    Re: Wiring Code to Game1

    You need to have a ContentManager object declared in the class that contains this code.
    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 8/4/2009 7:23 PM In reply to

    Re: Wiring Code to Game1

    K. Thanks a lot so far you guys are awesome giving me really fast replys.

    The code still doesn't draw my sprite though.

    Code:
    Player Class:
    http://pastebin.com/m6418102b
    Game1:
    http://pastebin.com/m5c5a951c
    3D Artist & Programmer:
    Portfolio - Team
  • 8/4/2009 7:46 PM In reply to

    Re: Wiring Code to Game1

    Put breakpoints on the first line of code in the Game's Draw and the Player's Draw methods. Which one fires first?
    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 8/4/2009 7:51 PM In reply to

    Re: Wiring Code to Game1

    The Game Draw Method goes first.
    3D Artist & Programmer:
    Portfolio - Team
  • 8/4/2009 7:57 PM In reply to

    Re: Wiring Code to Game1

    I knew that would be the case 1 second after I posted. :( Check the values for where you're drawing the player to make sure they're valid. If you can, upload the project somewhere as it's hard to tell just by looking at the code what could be wrong.
    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
Page 1 of 2 (45 items) 1 2 Next > Previous Next