locked
Global pointer to a class RRS feed

  • Question

  • To keep it simple ill just show the code that i would like to transform or get some info on how to transform to c#;


    In C++ i could do this.

    player.cpp file will looks like this.

    #include sprite.h
    #include player.h

    player *PLAYER = NULL;
    class player : public sprite
    {
       player():sprite()
        {
          PLAYER = this;
        }
    }

    enemy.cpp file


    #include player.cpp
    class enemy : public sprite
    {
        public void update(){enemy.vel.X = PLAYER->GetVelocity().X}
    }
    dumbed down and simplified i know but this is what i would like. My game is only going to have a single player. I dont want to pass a pointer to the player in everyones update function.
    Tuesday, September 20, 2011 9:37 PM

Answers

  • I really wished there was a global variables
    There are:

    public class Globals 
        public static Player player; 

    Friday, September 23, 2011 12:14 AM

All replies

  • in c# classes are passed by reference therefore if you do this
    playerclass Player; 
     
    //then in your class 
     
    public void SetClass(ref playerclass P) 
    P = this
     
     
     
    One thing to not is that c# and xna do not have gloable variables as such so accessing variables outside ones call can be done by passing in the parent class.
    Tuesday, September 20, 2011 10:00 PM
  • Well thats what I had to end up doing I really wished there was a global variables.
    Friday, September 23, 2011 12:04 AM
  • I really wished there was a global variables
    There are:

    public class Globals 
        public static Player player; 

    Friday, September 23, 2011 12:14 AM
  • I would still have to pass that global class around in functions. Also if I pass a player class to a funtion like

    void update1(Player player)  is that a copy in c# or a refference

    do i need to have it like this?

    void update1(ref Player player)

    Saturday, September 24, 2011 2:34 AM
  • I would still have to pass that global class around in functions.
    No, you wouldn't. You can access it from anywhere.

    Also if I pass a player class to a funtion like

    void update1(Player player)  is that a copy in c# or a refference

    Classes are reference types, which means they're passed by reference.
    Saturday, September 24, 2011 4:13 AM
  • So i would just call Global.Player and get the player class? Also how do you put your code in the scroll box?
    Wednesday, September 28, 2011 12:27 AM
  • So i would just call Global.Player and get the player class?


    Well, strictly speaking, that get's the player object, but yes, it's that simple!

    Also how do you put your code in the scroll box?


    Go into your profile (by clicking you name/pic at the top of the page), find the option to set your post editor, I think it is, Rich Text (I'd tell you exactly how, and what it's called, but I can't get into my profile!), then when you edit/author a post you'll get an editor with a toolbar across the top, one of the buttons on said toolbar is called 'Format Code Block' - that's what you use to 'put your code in the scroll box' as you put it.
    Wednesday, September 28, 2011 12:42 AM
  • Thanks man you've been a big help! For people that dont know the format code block looks like a paint brush.
    Thursday, September 29, 2011 9:10 PM