How Do I Validate a Game Name?

This is the place to find out how to accomplish something specific with EZ Game Saver.

How Do I Validate a Game Name?

Postby giza » Fri May 11, 2012 3:23 pm

Hi Brady,

I'm finalising a game and I've set up a TextField to allow the user to choose a unique game name. I know the MenuController script provides for the validation of a Profile name using the bool ValidateProfileName() function. How would I go about validating for an existing game name under the current profile?

Thanks!

*EDIT*
Ok, I've changed the SaveButton script to the following and it seems to be working OK. Am I on the right track here or am I setting myself up for some problems somewhere? I'd really appreciate some guidance here as I'm a novice with c#!
Code: Select all
using UnityEngine;
using System.Collections;

//---------------------------------------------------------------
//   This script demonstrates how to save a game.
//   See the "SaveGame()" method.
//---------------------------------------------------------------


public class SaveButton : MonoBehaviour
{
   protected string saveName = "";
   Rect buttonArea = new Rect(450,5,45,40);   
   public GUIStyle mySkin21  = new GUIStyle();
   protected ArrayList saves = new ArrayList();
   protected bool windowRectEmpty  = false;
   protected const int MAX_SAVE_SLOT = 20;
   string currentProfile;
   protected bool replace = false;
   
   // Will tell us if a SaveGameMaster object
   // exists so we can run the game before
   // we've added save functionality:
   bool saveMasterExists = false;
   int i2;
   int i3;
   
   void Start()
   {
      // See if our SaveGameMaster object exists so
      // we don't display our save button until we've
      // added it to our game:
      saveMasterExists = (null != FindObjectOfType(typeof(SaveGameMaster)));
      currentProfile = SaveGameMaster.GetActiveProfile();
      saves.AddRange(SaveGameMaster.GetSaveGames(currentProfile, MAX_SAVE_SLOT));   
      }   

   void OnGUI()
   {
      // Don't draw our button if no SaveGameMaster exists:
      if (!saveMasterExists)
         return;
      //Set up the Save button
      if (GUI.Button (buttonArea, "Save", mySkin21)){
         windowRectEmpty = true;
         }         
      //Call our input text field function
      if (windowRectEmpty == true) {
         InputSaveGameName();
         }
      }
   
   void InputSaveGameName (){   
      // Text field for save Name Input
      saveName = GUI.TextField(new Rect(420,50,150,40), saveName);
         ValidateSaveName();
         if (GUI.Button(new Rect(580,48,40,40), "OK")){
            if ( replace == true){
            DeleteSaveGame((SaveGameInfo)saves[i3]);//Call the delete function if required before saving
            }
            SaveGame();//Call the Save Game function
         }
      }      
   
   void SaveGame()
   {

      // Get the currently-active profile:
      string currentProfile = SaveGameMaster.GetActiveProfile();

      // Get the slot number to which we want to save.
      int saveSlot = SaveGameMaster.GetNextSlotNum(currentProfile);

      // Cap the slot number at 20 so its easier to
      // manage the existing savegames.
      // Also reserve slot 0 for the autosave:
      saveSlot = Mathf.Clamp(saveSlot, 1, 20);

      // Save the game:   
      SaveGameMaster.instance.Save(currentProfile, saveSlot, saveName);
      
      //Re-import the saves array
      saves.AddRange(SaveGameMaster.GetSaveGames(currentProfile, MAX_SAVE_SLOT));
      
      //Close the Save As Window & reset saveName to null
      saveName = "";
      windowRectEmpty = false;
      replace = false;
   }
   
   bool ValidateSaveName()
   {
      //Check to see whether this is a duplicate save name:
      for (i2 = 0; i2 < saves.Count; ++i2)
         {
         if (saveName == ((SaveGameInfo)saves[i2]).name){
          Debug.Log ("You are about to Replace an Existing File - Click OK to Proceed");   
            i3 = i2;//keep track of the relevant duplicate
            replace = true;
         }
      }
      return true;
   }
   
   //Delete the existing saved game if user proceeds
   void DeleteSaveGame(SaveGameInfo sg)
   {
      SaveGameMaster.ClearSaveGame(SaveGameMaster.GetActiveProfile(), sg.saveNum);
      saves.Remove(sg);

   }   
   
}

giza
 
Posts: 10
Joined: Sun Apr 29, 2012 1:17 am

Re: How Do I Validate a Game Name?

Postby Brady » Fri May 18, 2012 8:44 am

You would probably use SaveGameMaster.GetSaveGames() to retrieve an array of saves under the specified profile, then iterate through that list, comparing the names.
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Re: How Do I Validate a Game Name?

Postby johnniewalk » Mon Mar 16, 2015 6:50 am

Yes You are Right...
johnniewalk
 
Posts: 11
Joined: Fri Mar 13, 2015 8:44 am
Location: India

Re: How Do I Validate a Game Name?

Postby valamun » Mon Apr 16, 2018 8:54 am

ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212
ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212
ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212
ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212
ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212
ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212
ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212
ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212
ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212
ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212
ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212ac23212
ac23212
valamun
 
Posts: 23463
Joined: Sat Mar 17, 2018 5:46 pm


Return to EZ Game Saver How to...

Who is online

Users browsing this forum: No registered users and 1 guest

cron