- Home /
How can i Connect a database for my Log in form? (For windows Standalone)
I have read some answers here in Unity forums, but i still don't know where to begin adding a database. I have an experience in MySql. Can i use it in my Log in form in a standalone game? most of the answers I've read are like for Online or Web games, like using WWW. can i also use it for an offline standAlone game?
I hope someone can enlighten me about these things. Thanks a lot!
I'm not sure I follow; what would be the point of a database-driven login in an offline game? By offline do you mean single player, as in, you want the player to have to log in to play single player? Either way, you still have to use the WWW class, and the $$anonymous$$ySQL database would have to accessible over the Internet.
I think he means offline as in the files are stored on the users computer
Well, that seems a little silly, as I don't think anyone would ever be able to play his game except him, because they'd have to have $$anonymous$$ySQL installed locally and the database and tables built and set up, and I don't think thats something he could make Unity do automatically. And even if he did do it locally, he'd still have to interface with it with WWW and his private ip/loopback address, as a local server, and still have to run some web code (php/python or whatever) to get the data. Derek Traver 0 secs ago
(I suspect the OP means a windows standalone client -- native executable, not browser-hosted -- which connects to an online database.) Note that the System.Data namespace and friends should work, so you can open a SqlConnection and retrieve data into a DataSet. Let me know if it works, 'cuz I want to do the same thing! :-) http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection(v=VS.90).aspx
i actually don't mean a log in system, but something that can store someone's name, score, and level. Like when i type my name, i can load the level where i stopped.I'm really sorry if i can't express it so clear..should i try the codes posted which uses the WWW function?i'm quite confused on where to begin :S and i'm really confused o
Answer by PrimeDerektive · Jan 08, 2011 at 02:37 PM
Ohh Jeez! No, you do not have to go that crazy; if you just want to store simple things like the player's name, his score, and what level he was on, use PlayerPrefs; you don't need to use MySQL at all, that would be overkill.
See the PlayerPrefs docs for details: http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html
It is a very simple solution for saving simple data to the local computer in Unity, like ints, strings, and floats.
So in your game, when you want to save, you would do something like:
PlayerPrefs.SetString("PlayerName", "Derek");
PlayerPrefs.SetInt("PlayerScore", 1337);
PlayerPrefs.SetInt("CurrentLevel", 2);
Then when you start your game, you can retrieve all that stuff, and do whatever you want with it:
var playerName = PlayerPrefs.GetString("Player Score") var score = PlayerPrefs.GetInt("PlayerScore"); var currentLevel= PlayerPrefs.GetInt("CurrentLevel");
Application.LoadLevel(currentLevel);
Ohh!Thanks a lot for this!i think this is the answer that i'm looking for. Sorry for being a noob..i think imma go and learn PlayerPrefs..the code posted was a great help,Thanks so much again, i hope i can get back here to ask help if i encounter one..you guys are great!THAN$$anonymous$$S!
can i use playerPref for multiple player?i'm confused how to save the current game and still retrieve the data of the other players.i hope you can help me with this :)
$$anonymous$$ultiple players on the same computer? Like different "saves"?
Yup..just in the same computer..would it be possible to use playerPrefs for that?or does it store only data for a single player?Thanks a lot Derek!
Answer by Statement · Jan 08, 2011 at 02:49 PM
Derek hit the spot.
I just want to contribute with some nice thoughts about savegame data. It can be beneficial to have a struct or class describing the data in a type safe manner, so it is easy to find the different settings with intellisense. Then you would have a load and save function to save or load to player prefs. You could even use reflection to automate the save/load process.
With abstracting the storage layer away, you can later change from PlayerPrefs to ini files or xml files or sql tables. Your code would work none the less.
Example:
public class Savegame { public string playerName; public int playerScore; public int currentLevel;
public static Savegame Load()
{
Savegame loaded = new Savegame();
loaded.Reload();
return loaded;
}
public void Reload()
{
playerName = PlayerPrefs.GetString("playerName");
playerScore = PlayerPrefs.GetInt("playerScore");
currentLevel = PlayerPrefs.GetInt("currentLevel");
}
public void Save()
{
PlayerPrefs.SetString("playerName", playerName);
PlayerPrefs.SetInt("playerScore", playerScore);
PlayerPrefs.SetInt("currentLevel", currentLevel);
}
}
Then you can use it such as
void Start()
{
Savegame game = Savegame.Load();
Application.LoadLevel(game.currentLevel);
}
or
void Start()
{
game.currentLevel = Application.currentLevel;
game.Save();
}
I add yoyos comment here for formatting purposes.
public class Savegame
{
public string playerName
{
get { return PlayerPrefs.GetString("playerName"); }
set { PlayerPrefs.SetString("playerName", value); }
}
// and so on...
}
Oh thanks for this statement!!i'm just quite confused because i use javascript, but i think this would be very helpful. imma go and learn PlayerPrefs first to eli$$anonymous$$ate my confusions..thanks again to the both of you!!THAN$$anonymous$$S! >_< i'll get back if i didn't get something..take care!
Just add a comment here if you need to get hold of me. I usually read all comment replies.
can i use playerPref for multiple player?i'm confused how to save the current game and still retrieve the data of the other players.i hope you can help me with this :)
You could also use C# property getters, for example
public string playerName
{
get { return PlayerPrefs.GetString("playerName"); }
}
(Sorry about the formatting, wish I could put code in comments ...)
@yoyo I'll put your comment into my post so it get formatted. @futureseeker This will only work on a local machine. So as long your multiplayer is like several profiles on a single computer or split screen etc it's fine. You can't access data on another computer with playerprefs. Simply prefix with player name. plr1_health, plr2_health etc...
Your answer
