x


(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!

more ▼

asked Apr 06 '10 at 08:55 PM

Om3n gravatar image

Om3n
28 4 5 10

instead of renaming your question (solved), please click the accept button on the answer that sovled your question. Thanks! Lucas

May 10 '10 at 08:31 PM Lucas Meijer 1 ♦♦

Ditto. Let's try to keep it tidy.

Jul 22 '10 at 05:31 AM grey
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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.

more ▼

answered Apr 06 '10 at 09:19 PM

StephanK gravatar image

StephanK
6k 39 53 93

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?

Apr 06 '10 at 09:49 PM Om3n

Do you mean loading the gameobjects from a distant server, or just spawning a prefab? For loadin look up asset streaming in the manual. For spawning prefabs look at GameObject.Instantiate() and Resources.Load

Apr 07 '10 at 07:24 AM StephanK

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 streaming to see if it leads me anywhere, Thank you again.

Apr 07 '10 at 02:45 PM Om3n
(comments are locked)
10|3000 characters needed characters left

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);
        }
    }
}
more ▼

answered May 07 '10 at 05:52 PM

Om3n gravatar image

Om3n
28 4 5 10

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered May 10 '10 at 06:46 PM

sandeep gravatar image

sandeep
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

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";

?>
more ▼

answered Jul 13 '10 at 07:58 AM

Jens T gravatar image

Jens T
138 6 7 17

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2090
x525
x233
x110
x42

asked: Apr 06 '10 at 08:55 PM

Seen: 2552 times

Last Updated: May 07 '10 at 05:53 PM