Unity to PHP

got scripts? Need scripts?
Post Reply
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Unity to PHP

Post by hallsofvallhalla »

I promised yall these scripts sometime back . I owe them to you so here they are

Want to access a PHP file on a webserver while running Unity? Here is how

in the example below I am getting the player name for Quests of Crocania..This is named getname.js and is a script in unity. I placed it in a folder called webscripts and attach it to the player

Code: Select all

function Awake () {

getname();   

}

function getname()
{
var formText = new Array(); //this field is where the messages sent by PHP script will be in
var inctext;


var URL = "http://localhost/crocania/getmethods/webgetname.php"; //change for your URL

      var w = WWW(URL); //here we create a var called 'w' and we sync with our URL and the form
   yield w; //we wait for the form to check the PHP file, so our game dont just hang
   if (w.error != null) {
      print(w.error); //if there is an error, tell us
   } else {
     
	  inctext = w.text;
       formText = inctext.Split(":"[0]); 
////////////create variables from DB
	  GameObject.Find ("Playername").guiText.text=""+formText[0];
	  
	  
	  w.Dispose(); //clear our form in game
   }
}

I then have a file on my localhost in the directory of /crocania/getmethods/ called webgetname.php

Code: Select all

<?php
include_once '../connect.php';
session_start();
?>


<?php 
    $player=$_SESSION['player'];
echo $player;

?>
as you can see unity runs the script which does a http request to run the file. The PHP file runs and echos out the player name. Unity is listening for anything returned and when the name is returned we turn it into a unity variable.

Now you are asking.."Well what about multiple variables from one call...Well ole Halls has got you covered!

this is how I grab all the players skills
this is a file called webgetskills.js and is also a unity .js file

Code: Select all

function Awake () {

getskills();   

}

function getskills()
{
var formText = new Array(); //this field is where the messages sent by PHP script will be in
var inctext;


var URL = "http://localhost/crocania/webgetskills.php"; //change for your URL

      var w = WWW(URL); //here we create a var called 'w' and we sync with our URL and the form
   yield w; //we wait for the form to check the PHP file, so our game dont just hang
   if (w.error != null) {
      print(w.error); //if there is an error, tell us
   } else {
     
	  inctext = w.text;
       formText = inctext.Split(":"[0]); 
////////////create variables from DB
var miningskill = formText[2];
var forestryskill = formText[1];
var harvestskill = formText[3];
var searchskill = formText[4];


//////put variables on GUI
guiText.text=""+forestryskill;
	  GameObject.Find ("MiningText").guiText.text=""+miningskill;
	  GameObject.Find ("Playername").guiText.text=""+formText[0];
	  GameObject.Find ("Harvesttext").guiText.text=""+harvestskill;
	  GameObject.Find ("Searchtext").guiText.text=""+searchskill;
	  
	  
	  w.Dispose(); //clear our form in game
   }
}
i have a php file located ocalhost/crocania/webgetskills.php that it calls

Code: Select all

<?php
include_once 'connect.php';
session_start();
?>


<?php if (isset($_SESSION['player'])) 
  {
    $player=$_SESSION['player'];
    $playerinfo="SELECT * from players where name='$player'";
    $playerinfo2=mysql_query($playerinfo) or die("Could not get user stats");
    $playerinfo3=mysql_fetch_array($playerinfo2);
    
	
	 }
	else
	{
	print "Sorry, not logged in  please <A href='login.php'>Login</a><br>";
 exit;}
echo $playerinfo3['name'];
echo ":";
echo $playerinfo3['Forestry'];
echo ":";
echo $playerinfo3['Mining'];
echo ":";
echo $playerinfo3['Harvest'];
echo ":";
echo $playerinfo3['Search'];


?>
As you can see it collects everything echoed and I just add a : between each variable. Once it gets to unity I split the variables by : into an array.

Simple as that!!!

result

Image
User avatar
Ravinos
Posts: 281
Joined: Tue Sep 15, 2009 4:14 pm

Re: Unity to PHP

Post by Ravinos »

that looks relatively painless!
User avatar
Callan S.
Posts: 2043
Joined: Sun Jan 24, 2010 5:43 am

Re: Unity to PHP

Post by Callan S. »

That's great! Really excited about the potential of this! Thanks Halls! I'll keep coming back to this and studying it.

When you made that gathering demo, by what method did you do the first person camera? I'm still floundering in unity, mostly.
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Unity to PHP

Post by SpiritWebb »

This is awesome...thanks for this! I do got a question. How would one go about adding the functionality to allow players to see each other move around in real time? And could a chat option be implemented in the same manner? If this is possible. And with this, how much stress can a database handle if several hundreds of players are connected?

Looking at other options other then SmartFoxServer as I did manage to get a connection to a db, its confusing on where to go from there, cause it skips the PHP entirely and I cannot find anything to help in that direction. Unity -> SmartFoxServer -> PHP -> db and back.
Image

Image
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Unity to PHP

Post by hallsofvallhalla »

I would not use this method to track dynamic positions. Especially with hundreds of players. You could use it to place objects on a map and such but since it is a Http request I would not use it for a refresh rate under a second or two.



I used the First person camera script.
User avatar
davidc088
Posts: 61
Joined: Fri Jun 11, 2010 1:01 am

Re: Unity to PHP

Post by davidc088 »

That's looking great!
Go to Gamers Feed for all of your Gaming news and rumors. http://www.gamers-feed.com/

Image
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Unity to PHP

Post by SpiritWebb »

Database has:

database name: unity
table users, 2 columns. id and player. Only one row, which is my name, with an id of 1.

webtest.js (unityscript)

Code: Select all

function Awake () {

getname();   

}

function getname()
{
var formText = new Array(); //this field is where the messages sent by PHP script will be in
var inctext;


var URL = "http://odcmm/UnitySiteTest/webtest.php"; //change for your URL

      var w = WWW(URL); //here we create a var called 'w' and we sync with our URL and the form
   yield w; //we wait for the form to check the PHP file, so our game dont just hang
   if (w.error != null) {
      print(w.error); //if there is an error, tell us
   } else {
     
     inctext = w.text;
       formText = inctext.Split(":"[0]); 
////////////create variables from DB
     GameObject.Find ("player").guiText.text=""+formText[0];
     
     
     w.Dispose(); //clear our form in game
   }
}
webtest.php

Code: Select all

<?php 
   $db = mysql_connect("localhost","root","") or die("Could not connect to db");
if(!$db)
die("no db");
if(!mysql_select_db("unity",$db))
die("No Database Selected");
session_start();
?>

<?php

    $player=$_SESSION['player'];
echo $player;

?>
When I pull it up on the browser, I am getting this error:
Notice: Undefined index: player in C:\wamp\www\UnitySiteTest\webtest.php on line 11
Image

Image
User avatar
Callan S.
Posts: 2043
Joined: Sun Jan 24, 2010 5:43 am

Re: Unity to PHP

Post by Callan S. »

$_SESSION['player'] doesn't seem to have been set yet? If you just gave $_SESSION['player'] a dummy value it'd probably work. I really must try this with unity myself soon!!!
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
User avatar
Callan S.
Posts: 2043
Joined: Sun Jan 24, 2010 5:43 am

Re: Unity to PHP

Post by Callan S. »

I'm trying the first code, that just grabs a name. And getting this error

Code: Select all

NullReferenceException
getname+$getname$9+$.MoveNext() (at Assets/Standard Assets/Character Controllers/Sources/Scripts/getname.js:24)
What does that mean?

Edit: I'm guessing it perhaps refers to some component I don't have.

I tried out something I found with google

Code: Select all

print("The variable is :"+formText);
And it shows the value down in the editor window.

Is there an easy way to show the info on screen? I've tried this

Code: Select all

function OnGUI()
{
  GUI.Label(Rect(0,0,100,100),formText)
}
And it just gives an error?
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Unity to PHP

Post by hallsofvallhalla »

@spirit I built this with sessions in mind. Make sure you are logging in and creating a session in the browser first. Remember QOC had Unity built into the game. SO you may want to remove session and just have unity pass the players name or somekind of ID to the database.


@callan your issue is here

Code: Select all

 GameObject.Find ("Playername").guiText.text=""+formText[0];
do you have a GUI object named playername?
Post Reply

Return to “Scripting/Coding”