   Usage of struct
  Heyas,I am putting together an RPG battle type game.  I was told a while ago on this forum that global variables are bad and doing a struct to hold the player''s information(HP, Power, Gold, etc.) would be a better way to go.  However, I''m not quite sure how I''d go about manipulating the data within the struct.  I believe I would set the struct up as follows:struct PlayerInfo {    string PlayerName;    int PlayerHP;    int PlayerGold;    int PlayerPower;);Then, when the player goes to a shop and buys something, how would I go about removing say 10 Gold?Also, could some one possibly recommend a book they think would be a good refernce?  LIke one where I could go to answer questoins such as these?Thank you,Chryzmo 
struct PlayerInfo {string PlayerName;int PlayerHP;int PlayerGold;int PlayerPower;);PlayerInfo Player;Player.PlayerGold -=10;This is a good site it's a good place to get started it goes over your question and some of the basics.   [edited by - FtMonkey on July 31, 2003 2:37:50 AM];  Also check this site out. Scroll to the bottom and you got plenty of tutorials  --{You fight like a dairy farmer!} ;  Let me tell you something.global variables are not bad at all,if you read the book:"Tricks of the WindowsGame Programming Gurus"You will see that global variables are much more faster then variables you define in the struct or class.When you need fast proccesing then use global variables.;struct PlayerInfo {string PlayerName;int PlayerHP;int PlayerGold;int PlayerPower;};PlayerInfo *Player;Players = new Playerinfo;Player->PlayerGold -=10; ;quote:Original post by cleves   Let me tell you something.global variables are not bad at all,if you read the book:"Tricks of the WindowsGame Programming Gurus"You will see that global variables are much more faster then variables you define in the struct or class.When you need fast proccesing then use global variables.Hahahaha... no. Using global variables for speed is even more stupid than simulating multiplication and division with bitshifts. Wreaks havok on your design (just look at LaMothe's source! It's a crime against nature!) Pay no mind to the "optimizations" LaMothe advises, for your own sake.   [edited by - twix on July 31, 2003 10:23:02 AM];  Nope.how can you argue with a man that has dozens of programming years behaind him?And second i find many book where the same is written!;  Even if he did make them global, it is still a good idea to put them in a structure of some kind. ;quote:Original post by cleves Nope.how can you argue with a man that has dozens of programming years behaind him?And second i find many book where the same is written!Let me put this another way. When working on an actual project, the FIRST thing that you should consider is proper design, NOT using as many globals as possible to (maybe) increase speed. Doing otherwise will end you up with a jumbled mess like LaMothe's code, because optimization should only be done after a) everything works the way it should, and b) you've profiled everything to figure out exactly what's slow.Furthermore, bit shifts are no faster than multiplication on modern processors. The overhead of accessing values from a struct is so negligible that you'd have to be programming on a 5 year old computer for it to have any impact even in code that requires high performance. Try benchmarking these things, and you'll agree.Hell, even those sine/cosine lookup tables don't make much of a difference (about a 20% speed increase for 10 thousand sines on my machine). It might be worth it, but only after everything else is worked out and your algorithms are streamlined.Premature optimization leaking into your design phase is the root of all evil (and horrible, horrible code).I don't take something written in a book for its face value just because the author has years of programming experience. You shouldn't either.   [edited by - twix on July 31, 2003 11:32:51 AM];quote:Premature optimization leaking into your design phase is the root of all evil (and horrible, horrible code).I believe that is a quote from Dykstra, and it is a quote I''ve always hated, though I owe my job to it.  I work as a performance expert with a company that makes billing systems (billing systems that have a far bigger codebase than any game out there).  Once a project gets to a certain level of complexity, lack of optimization in design will bring you to your knees, and I''ve seen it over and over.Performance/optimization needs to be considered at all stages of development, from hardware sizing to performance testing.  If you don''t consider it as part of design, you may end up with no alternative but to throw away 25% of your code and start over when you encounter a problem.Certain things, like unrolling loops or lookup tables, are best left to the performance test phase.  But making a sweeping statement about leaving all optimization to that stage is dangerous and short-sighted.This is, of course, all imho. :-)