I have been working on making a custom server (probably won't work but I want to give it a try and work on it as a hobby) but I've already run into a problem! What I have the php scripts outputing the commands as text then getting unity to translate that into the c# commands.
Here's the ServerCalls.cs:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using System.Linq;
public class ServerCallsScript : MonoBehaviour {
private bool isPlayerInRoom = false;
public bool autoConnectToLobby;
public bool showLobbyGUI;
public float width;
public float height;
public float x;
public float y;
public enum logTypeEnum {
Developer,
Debug,
Warning,
Error
};
public logTypeEnum logType = logTypeEnum.Warning;
public string MainServerUrl = "https://network-hlapi-myusername.c9.io/serverMain.php";
private string serverText = "";
void Start()
{
StartCoroutine(ServerCommands());
}
void Update()
{
}
void OnGUI()
{
if (showLobbyGUI)
{
if(GUI.Button(new Rect(5,80,115,21), "Create Game"))
{
}
GUI.TextField(new Rect(130, 80, 115, 21), "Game1");
if(GUI.Button(new Rect(5, 110, 115, 21), "Join Game"))
{
}
GUI.TextField(new Rect(130, 110, 115, 21),"Game Name");
}
}
/// <summary>
/// This is called as soon as the player opens the scene
/// </summary>
public void OnConnectedToServer()
{
if (logType == logTypeEnum.Debug || logType == logTypeEnum.Developer)
{
Debug.Log("Successfully Connected To Server at " + DateTime.Now.ToString("hh:mm:ss"));
}
}
/// <summary>
/// Called When Player Connects To Lobby. Automatically called if auto-join lobby is enabled
/// </summary>
public void OnConnectedToLobby() {
if (logType == logTypeEnum.Debug || logType == logTypeEnum.Developer)
{
Debug.Log("Connected To Lobby at " + DateTime.Now.ToString("hh:mm:ss"));
}
}
public void ShowLobbyGui() {
if (logType == logTypeEnum.Debug || logType == logTypeEnum.Developer)
{
Debug.Log("Showing Lobby Gui");
}
}
/// <summary>
/// Called when the player joins a game
/// </summary>
public void OnPlayerJoinedRoom()
{
if (logType == logTypeEnum.Debug || logType == logTypeEnum.Developer)
{
Debug.Log("Player Successfully Joined Room at " + DateTime.Now.ToString("hh:mm:ss"));
}
}
IEnumerator ServerCommands() {
Debug.Log("Collecting Info from Server...");
WWWForm Mainform = new WWWForm();
Mainform.AddField("isAutoJoinLobby", autoConnectToLobby.ToString());
Mainform.AddField("isShowingLobbyGui", showLobbyGUI.ToString());
Mainform.AddField("hasPlayerJoinedRoom", isPlayerInRoom.ToString());
WWW mainServer = new WWW(MainServerUrl, Mainform);
yield return mainServer;
serverText = mainServer.text;
string[] serverCommands = serverText.Split('
');
if(mainServer.error != null)
{
Debug.LogError("We encountered an error! Error:" + mainServer.error);
}
if (serverCommands.Contains("OnConnectedToServer"))
{
OnConnectedToServer();
}
if (serverCommands.Contains("OnConnectedToLobby"))
{
Debug.Log("OnConnectedToLobby");
OnConnectedToLobby();
}
if (serverCommands.Contains("showLobbyGui"))
{
ShowLobbyGui();
}
if (serverCommands.Contains("OnPlayerJoinedRoom"))
{
OnPlayerJoinedRoom();
}
}
}
For ServerMain.php:
<?php
$servername = getenv('IP');
$username = getenv('C9_USER');
$password = "I Wouldn't Tell You That Now";
$database = "server";
$dbport = 3306;
// Create connection
$db = mysql_connect($servername, $username, $password, $dbport)or die("Cant Connect to server");
mysql_select_db($database) or die("Cant connect to database");
$isAutoConnectToLobby = $_POST['isAutoJoinLobby'];
$isShowingLobbyGui = $_POST['isShowingLobbyGui'];
$hasPlayerJoinedRoom = $_POST['hasPlayerJoinedRoom'];
$ClientClickConnectButton = $_POST['ClientClickConnectButton'];
echo "OnConnectedToServer
";
if ($isAutoConnectToLobby == true){
echo "OnConnectedToLobby
";
}
if($isShowingLobbyGui == true){
echo "showLobbyGui
";
}
if($ClientClickConnectButton == true){
sleep(5); //Just to give some time to complete any in-complete operations
echo "onClientClickConnectButton
";
}
if($hasPlayerJoinedRoom == true){
echo "OnPlayerJoinedRoom
";
}
?>
The problem is that the ServerMain.php script doesn't seem to listen to any of the WWWForm fields. If you add quotes to the if statements in the php script it just goes through all the if statements but if you don't add quotes it doesn't do any of the if statements, even if the public bools are enabled