- Home /
why my android game lag when getting data from online mysql
hi all, i need your help.
i want to ask, why my game is lag when i get data from online mysql database, how can my game not lag if i want to have database online in it ???
any solution? if yes please help me here. i developed game for android device, using mysql online.
Thanks
Why not post the offending code that makes the call. Can you supply some more information, are you using the unity's WWW class and post to some php/asp/form page?
ok, this is my code for connect into database online :
DatabaseCS.cs :
using UnityEngine;
using System.Collections;
using System.Data;
using $$anonymous$$ySql.Data.$$anonymous$$ySqlClient;
public class DatabaseCS : $$anonymous$$onoBehaviour {
private static IDbConnection dbConnection;
// Use this for initialization
void Start () {
//openSqlConnection();
}
public void OnApplicationQuit() {
closeSqlConnection();
}
public static void openSqlConnection() {
print ("Open");
string connectionString =
"Server = ip;" +
"Database = database name;" +
"User ID = user id;" +
"Password= my password;" +
"Pooling=false";
dbConnection = new $$anonymous$$ySqlConnection(connectionString);
dbConnection.Open();
Debug.Log("Connected to database.");
}
public static void closeSqlConnection() {
dbConnection.Close();
dbConnection = null;
Debug.Log("Disconnected from database.");
}
public static IDataReader doQuery(string sqlQuery) {
IDbCommand dbCommand = dbConnection.CreateCommand();
dbCommand.CommandText = sqlQuery;
IDataReader reader;
reader = dbCommand.ExecuteReader();
dbCommand.Dispose();
dbCommand = null;
return reader;
}
}
and then i use this code to get my data from database, just simple code for example how i can get data from my database into my android..
this code to set radius :
DatabaseCS.openSqlConnection();
IDataReader reader = DatabaseCS.doQuery("SELECT Radius FRO$$anonymous$$ mytower");
reader.Read();
sphereCol.radius = (float)reader["Radius"]+(curLvl)*(0.2f*(float)reader["Radius"]);
just like that how i get data from my database, it's for all the data that i want to get from database like attack, speed, health, radius, and many more, anything wrong that make my game lag ?
or maybe i used the wrong code to connect into database?
Thanks :D
sorry, can you please give me example put the connection code and query code into coroutines? because i don't really understand about it, you can use my code above for the example
Thanks :D
Answer by $$anonymous$$ · Jan 07, 2013 at 04:09 PM
I'm more than certain that it lags because it waits for the database reply in the frame you're in when you call the query. You should not block the game while you wait for your query result. Try putting the connection code and query code into coroutines so you don't block your game while waiting for them.
sorry, can you please give me example put the connection code and query code into coroutines? because i don't really understand about it, you can use my code above for the example
Thanks :D
Answer by Bunny83 · Jan 08, 2013 at 02:04 PM
Never direct connect to a database from a game. Everyone that uses your game can read your username / password. Use a webserver + pup to encapsulate the db functions.
In the case that this isn't a game but an application that should connect to the users local database, you should put database queries into a desperate thread. Using coroutines won't help here since the database query is like an atomic instruction unless you use asynchronous queries which also uses a seperate thread.
Can you give a bit more details about what you actually do? Is it a game or is it just an application?
i want to make mobile game.
hmm, in my game i have some data like attack, speed, radius, etc. i used web server so i can change the data easily just like ad$$anonymous$$ area, but the problem i have is the game must online because it connect into web server and another problem is lag because the game must get data from my online web server and i used $$anonymous$$ySQL.
something like that, do you understand right? sorry for my bad english :D
hope you understand and can help me here.
thanks
Then it's absolutely a bad idea to use a DB$$anonymous$$ exposed to public as your. You should write a little proxy (in PHP?) that connects to database and sends client the requested data.
another problem is lag just use coroutines as explained in this link:
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine
or maybe i must use sqlite to solved this? but i never use sqlite and don't understand about it at all..
hmm, ya i look into it but i don't still get how can start coroutines solve this, because i must always get the data every second right? but when i use coroutine there's time before i get my data, or i'm wrong? sorry i don't get it :D
can you please give me some example here with my code above?
thanks
// WARNING: UNTESTED CODE!
using UnityEngine;
using System.Data;
using $$anonymous$$ySql.Data.$$anonymous$$ySqlClient;
public class example : $$anonymous$$onoBehaviour {
void Start() {
StartCoroutine(openSqlConnection());
}
public static void openSqlConnection() {
print ("Open");
string connectionString =
"Server = ip;" +
"Database = database name;" +
"User ID = user id;" +
"Password= my password;" +
"Pooling=false";
dbConnection = new $$anonymous$$ySqlConnection(connectionString);
yield return dbConnection.Open();
Debug.Log("Connected to database.");
}
}
my game still lag after i use coroutine haha..
any other way so my game not lag again?
Answer by pippo19 · Sep 28, 2014 at 03:09 PM
Hi quantum_rez, I work at android app that should connect to online database. On PC and on unity editor it work; but if i build apk itsnt work. How u do to work it? Have u add uses permission in AndroidManifest?
Thanks