- Home /
(solved)Pasing WWW data post/get to load gameobjects?
I cannot seem any reference on how to go about spawning gameobjects from a sql/php backend using data retrieved using the WWW set. I cant seem to find any kind of reference in regards to this anywhere. Im trying to pull x,y,z from the sql using php and the spawning the prefab for the player at the position. I need to be able ot parse the data out of the returned data and then spawn the gameobject based on the data. Any thoughts/references on this would be greatly appreciated. Thank you!
ins$$anonymous$$d of rena$$anonymous$$g your question (solved), please click the accept button on the answer that sovled your question. Thanks! Lucas
Answer by StephanK · Apr 06, 2010 at 09:19 PM
You can use WWW to open any url you want, including your typical get parameters (e.g. http://myserver.com/?action=getData) www.data will contain the received data as a string. So you can write you own php code, that will return the reqired data in a format that you can easily parse. To parse the returned data you can use the standard .net string features, or you return xml and use .net's xml parsing functions.
Thank you very much for a prompt reply. It has put me in the right direction. Would you have to have any links to spawning dynamic gameobjects based on the data received from the database?
Do you mean loading the gameobjects from a distant server, or just spawning a prefab? For loadin look up asset strea$$anonymous$$g in the manual. For spawning prefabs look at GameObject.Instantiate() and Resources.Load
I meant loading the gameobjects with data received with info from the www data stream. ie: client logs in & queries the db then spawns objects based on that query. I will start with asset strea$$anonymous$$g to see if it leads me anywhere, Thank you again.
Answer by Om3n · May 07, 2010 at 05:52 PM
This is how i ended up doing it, hopefully it can serve as an example for somone else =)
function Start()
{ // show the progress window showProgress=true;
var w = WWW(URL); yield w; planetData = w.data; //here we return the data our PHP told us
var singlecoords = planetData.Split("\n"[0]); //line splitting php outut
curLine=0; maxLines=singlecoords.Length;
for (line in singlecoords) { // advance progress indicator ++curLine; var coords = line.Split(","[0]); //taking xyz & other data from singlecoords if (line.Length >= 8) { // makes sure we only instantiate valid planet records and // not any blanks the PHP/web exchanger added to the end planetID = parseInt(coords[0]); planetName = coords[6]; planetOwner = coords[4]; planetSector = coords[5]; objectType = parseInt(coords[7]);
var pos = Vector3(parseInt(coords[1]),parseInt(coords[2]),parseInt(coords[3]));
if (ObjectFactories.Length>objectType && ObjectFactories[objectType] != null) {
var planetInstance = Instantiate(ObjectFactories[objectType], pos, Quaternion.identity);
planetInstance.name = planetName;
switch (objectType) {
case 0: /* Planet */
pInfo=planetInstance.GetComponent(PlanetClick);
pInfo.planetName=planetName;
pInfo.planetSector=planetSector;
pInfo.planetOwner=planetOwner;
pInfo.planetID=planetID;
pInfo.objectType=objectType;
break;
case 1: /* Asteroid Belt */
print(objectType);
pInfo=planetInstance.GetComponent(PlanetClick);
pInfo.planetName=planetName;
pInfo.planetSector=planetSector;
pInfo.planetOwner=planetOwner;
pInfo.planetID=planetID;
pInfo.objectType=objectType;
break;
default:
print("No initializer for object type "+objectType);
}
} else {
print("No prefab template in objectType slot "+objectType);
}
}
}
Answer by Jens T · Jul 13, 2010 at 07:58 AM
Please note. This might fail strangely and silently. Check if there is difference between receiving hard coded and dynamic data using this php code:
this is hard coded text and will usually appear <?php
print "This is dynamic text, should appear, but might disappear";
?>
Answer by sandeep · May 10, 2010 at 06:46 PM
Question: Can i use this way to read fetched data from php/MYSQL Db.
code in my unity .js file.
var hs_get : WWW = new WWW (url); yield hs_get; var newdata = hs_data.data; . . .
does this mean , "newdata" has all the attributes as in the database table fetched from DB using PHP?
reply to me : sandeepsebol@gmail.com