Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: [C++] help with variables  (Read 2276 times)

alfie275

  • Bay Watcher
    • View Profile
[C++] help with variables
« on: September 22, 2009, 01:41:11 pm »

Ok, in my game there is a problem with variables, in the attack function. If I declare the variable in the C++ file instead of the header then it works, but then, well its not in the header.
http://www.mediafire.com/?sharekey=1b36191cf64826d80f83d91f6dff7c38e04e75f6e8ebb871
Logged
I do LP of videogames!
See here:
http://www.youtube.com/user/MrAlfie275

timmeh

  • Bay Watcher
    • View Profile
    • My Portfolio
Re: [C++] help with variables
« Reply #1 on: September 22, 2009, 02:11:23 pm »

Well, first off, there are two files at that link.  I only tried downloading the first one (ASCII man zip file), and since it has a function called Attack() I'm assuming that's the one you meant.

If that's the case, there are a couple variables in that function, which one did you want in the header file and why?
Logged
On the Wall is a Masterfully engraved carving of Urist McHardcastle and Goblins. Urist McHardcastle is surrounded by the Goblins. The Golbins are stamping on Urist McHardcastle. Urist McHardcaste is laughing at the Goblins. The carving related to the prolonged and bloody death of Urist McHardcastle in the Fall of 1659, the Winter of 1659, and the Spring of 1660. On the engraving is an image of Cheese.

alfie275

  • Bay Watcher
    • View Profile
Re: [C++] help with variables
« Reply #2 on: September 22, 2009, 04:11:24 pm »

It doesnt have to be in header? I was under the impression that to be part of the class it had to be in a header because if I put variables in .cpp it goes all wierd.
« Last Edit: September 22, 2009, 04:17:21 pm by alfie275 »
Logged
I do LP of videogames!
See here:
http://www.youtube.com/user/MrAlfie275

timmeh

  • Bay Watcher
    • View Profile
    • My Portfolio
Re: [C++] help with variables
« Reply #3 on: September 22, 2009, 04:34:58 pm »

You can create variables inside of class functions all you want, they just won't be kept once the function is done running.  But again, which variable exactly are you talking about?  We may not be on the same page at the moment, and "it" isn't particularly specific...

What I can't say I understand is how it's getting the key pressed, I thought to get a variable into a function you had to pass it or declare it external, and g_pressedKey is neither... it works, cause when I build and test it pressing 'a' does trigger an attack (regardless of the distance between ASCII-man and the duck, I might add...) I just can't figure out how you did it... do global variables work like that by default?

Sorry if any of that came across as aggressive, it was definitely not intended as such.  Just trying to give constructive criticism, and doing a pretty mediocre job of it :P
Logged
On the Wall is a Masterfully engraved carving of Urist McHardcastle and Goblins. Urist McHardcastle is surrounded by the Goblins. The Golbins are stamping on Urist McHardcastle. Urist McHardcaste is laughing at the Goblins. The carving related to the prolonged and bloody death of Urist McHardcastle in the Fall of 1659, the Winter of 1659, and the Spring of 1660. On the engraving is an image of Cheese.

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: [C++] help with variables
« Reply #4 on: September 22, 2009, 05:45:12 pm »

You can create variables inside of class functions all you want, they just won't be kept once the function is done running.  But again, which variable exactly are you talking about?  We may not be on the same page at the moment, and "it" isn't particularly specific...

What I can't say I understand is how it's getting the key pressed, I thought to get a variable into a function you had to pass it or declare it external, and g_pressedKey is neither... it works, cause when I build and test it pressing 'a' does trigger an attack (regardless of the distance between ASCII-man and the duck, I might add...) I just can't figure out how you did it... do global variables work like that by default?

Sorry if any of that came across as aggressive, it was definitely not intended as such.  Just trying to give constructive criticism, and doing a pretty mediocre job of it :P

Global variables are, well, global. They're always in scope unless you replace them within the scope. So yes, if you've got a global variable you can acces it from any function you'd like.
Logged

timmeh

  • Bay Watcher
    • View Profile
    • My Portfolio
Re: [C++] help with variables
« Reply #5 on: September 22, 2009, 05:55:39 pm »

Good to know, thanks!  I constantly see people avoiding global variables like the plague, so I haven't really used them much yet (at least, intentionally)... I'll have to do some googling later.
Logged
On the Wall is a Masterfully engraved carving of Urist McHardcastle and Goblins. Urist McHardcastle is surrounded by the Goblins. The Golbins are stamping on Urist McHardcastle. Urist McHardcaste is laughing at the Goblins. The carving related to the prolonged and bloody death of Urist McHardcastle in the Fall of 1659, the Winter of 1659, and the Spring of 1660. On the engraving is an image of Cheese.

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: [C++] help with variables
« Reply #6 on: September 22, 2009, 08:02:26 pm »

A header file is literally stuck in the place of the #include line just before the file is compiled.

However, if you want to share a variable between files, you declare it extern in the header file or the files it is shared by and then in ONE of them declare it normally. If, for example, by a header file, an extern definition is in the same file as the non-extern definition, it won't care. In fact, I don't know for sure, but it might be *required*.
Logged
Eh?
Eh!

Derakon

  • Bay Watcher
    • View Profile
Re: [C++] help with variables
« Reply #7 on: September 22, 2009, 11:01:12 pm »

One reason to avoid global variables is that it makes it very hard to recognize when things have changed in your program. Let's say you have a program that spans 10 files (not very big; my current project is at 77 so far). You have a bug which you eventually track down to a global variable being changed when it shouldn't be. Now you have to look through those 10 files to make certain that every time you change the global variable, you're doing it properly.

This is by no means the only reason to avoid global variables, but it's a pretty good one. Global variables make things very easy for you in the short term, but they can drastically worsen your life later on. It's best to just get into the habit of not using them.
Logged
Jetblade - an open-source Metroid/Castlevania game with procedurally-generated levels

alfie275

  • Bay Watcher
    • View Profile
Re: [C++] help with variables
« Reply #8 on: September 23, 2009, 11:00:20 am »

The reason they are in like that is because they are part of the class, for example i could reference 'g_myRoom.GetDuck().HP' and it would return the HP variable because it is an aspect of the class and each instance is unique to the instance of the class.
Logged
I do LP of videogames!
See here:
http://www.youtube.com/user/MrAlfie275

eerr

  • Bay Watcher
    • View Profile
Re: [C++] help with variables
« Reply #9 on: September 26, 2009, 06:06:12 pm »

ah, the confusion of object oriented programming : P.
But I can't really help you with global variables, because java is diffrent.
Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: [C++] help with variables
« Reply #10 on: September 26, 2009, 06:21:37 pm »

By Derakon's response, I see a clear use for a global variable: a commonly read state value, changed rarely, and possibly through a function to aid in debugging.

From my own experience, I know that a semi-global varialbe is very useful (global to a file but not used elsewhere at all) because you have a group of functions sharing data and using global variables would be bothe the most efficient and the most clear way.

Finally, there is set once variables such as a pointer to a class or struct.


Also, static variables in languages without MUST be global to work.

{Theory}
I believe that the older games(NES and similar) don't crash often because they don't use pointers or use the stack for functions. They would use stuff *like* a pointer, but not quite(array, basically), and this is my personal belief of how the pokemon red/blue missingno thing works(one after the last pokemon in memory, an array index bounds error, maybe turned feature? Original source of removed content?) and NES metroid secret worlds(Theoretically, could you find a level cell, normally unreachable, that varies with your health?)
{/theory}

But overall, globals aren't bad at all, it's just misuse in large projects where the overhead of function-access is minimal. Write-rarely, read-often, global variables are quite useful, especially in small projects.
Logged
Eh?
Eh!