Keeping Score Richard Gesick Objectives Create stats for
Keeping Score Richard Gesick
Objectives Create stats for the player Implement those stats in our scripts Create a stat tracker for the stats Create an achievement system Use Player. Prefs to save our stats Use GUI methods to show the stats and achievements • Create/assign the stats • • • 1 -2
Possible Stats • • Kills Deaths Total gold Current gold Gold spent Level Rounds won Rounds lost Kill-death ratio Win-lose ratio Time played Naming convention: p. Kills, p. Deaths, etc Script necessary? 1 -3
Player. Prefs • Allow storage of small amounts of info (strings, ints, floats) (about 1 MB total) • Save data using: Player. Prefs. Set. Float(“keyname”, variable name); • Load data into game using: int p. Kills=Player. Prefs. Get. Int(“player. Kills”); 1 -4
Stat. Tracker Script • Methods to initialize, reset, update the stats during the game. • Methods to set/reset Player. Prefs for storage between games • Display methods • and later to read/write files 1 -5
Setting Specific Stats void Set. Stat(string stat, int. Value = 0) //see opt code { switch(stat) { case "Kills": p. Kills+= int. Value; float f. Kills = p. Kills; float f. Deaths = p. Deaths; if(f. Kills != 0) p. KDR = f. Deaths / f. Kills; break; case "Deaths": p. Deaths+= int. Value; 1 -6
Resetting Stats void Reset. Stats() { p. Kills = 0; p. Deaths = 0; … } 1 -7
Resetting Player. Pref void Reset. All. Prefs() { Player. Prefs. Set. Int("Player. Kills", 0); Player. Prefs. Set. Int("Player. Deaths", 0); Player. Prefs. Set. Int("Player. Total. Gold", 0); … Player. Prefs. Save(); } 1 -8
Player. Prefs. Save() • From unity documentation: • Writes all modified preferences to disk. • By default Unity writes preferences to disk during On. Application. Quit(). In cases when the game crashes or otherwise prematurely exits, you might want to write the Player. Prefs at sensible 'checkpoints' in your game. This function will write to disk potentially causing a small hiccup, therefore it is not recommended to call during actual gameplay. Note: There is no need to call this function manually inside On. Application. Quit(). 1 -9
Saving the player prefs void Save. All. Prefs() { Player. Prefs. Set. Int("Player. Kills", p. Kills); Player. Prefs. Set. Int("Player. Deaths", p. Deaths); Player. Prefs. Set. Int("Player. Total. Gold", p. Total. Gold); … Player. Prefs. Save(); } 1 -10
Displaying stats void On. GUI() { if(show. Stats) { stats. Rect = GUI. Window(0, stats. Rect, Stats. GUI, "Stats"); } } void Stats. GUI(int ID) { GUILayout. Begin. Area(new Rect(15, 25, 400)); GUILayout. Begin. Vertical(); GUILayout. Label("Level - " + p. Level); GUILayout. Label("Gold - " + p. Current. Gold); … } 1 -11
- Slides: 11