Problem with wwwform

Hi,

I’m trying to get Unity to send a wwwform to a PHP file every time every time the player makes a new move - left, right, forward etc. However I am having trouble getting the info out via wwwform: I keep getting the following error:

MissingMethodException: Method not found: ‘UnityEngine.WWWForm.addField’.

Here’s the full code the WWWForm references are right near the bottom:

var moveSpeed = 1.0;
var turnSpeed = 1.0;
var direction = "still";
var tale="I woke up in the middle of nowhere";
var newStatement;
var oldStatement ="nothing";

	function Update()
	{
	
	if(Input.GetButtonDown("Jump"))
	{
		transform.position.z += 1.0;	
		direction = "Jump";
		}

	
	if(Input.GetButton("Forward"))
	{
		transform.position += transform.forward * moveSpeed * Time.deltaTime;
		direction = "forward";
	}
	if(Input.GetButton("Back"))
	{
		transform.position += -transform.forward * moveSpeed * Time.deltaTime;
		direction = "back";
	}
	if(Input.GetButton("Left"))
	{
		transform.eulerAngles.y += -turnSpeed * Time.deltaTime;
		direction = "Left";
	}
	if(Input.GetButton("Right"))
	{
		transform.eulerAngles.y += turnSpeed * Time.deltaTime;
		direction = "Right";
	}
	    		if(direction==="Jump"){
		newStatement = "jump";

				}

	if(direction==="forward"){
		newStatement = "forward";
			}
		
	else if(direction==="back"){
		newStatement = " back";
			}
			
			else if(direction==="Left"){
		newStatement = "left";		
		}
		
			else if(direction==="Right"){
		newStatement = "right";		
		}
			
	else{
		newStatement = "stopped";
		}
		
	if(newStatement!=oldStatement){
		addtoDB();
		}
	
}

	
	
function addtoDB()
	{
	


			var form = new WWWForm();
			var phpurl = "http://localhost/writer.php";
			var sendR = WWW(phpurl,form);

			if(direction==="Jump"){			
				form.addField("value", "1");
				yield sendR;
				print("addtoDB is working");
			}
			
			if(direction==="Forward"){
				form.addField("value", "1");
				print("addtoDB is working");
				yield sendR;
			}	
			

	}

Could anyone help me on what I am doing wrong?

Thanks,

David

I guess you mean AddField, right?

Also keep in mind that TCP connections are quite slow. Each packet will be acknowledged by the server. Most webservers only allow two simultaneous connections from the same client. If your user presses the buttons faster than the packets are beeing sent you get in trouble.

I would collect the data locally and pack them together into one packet and send that only every 10 sec.

BTW: You only send the “value” but not what action has been performed (forward / jump / …). So your php script always gets a value of 1

edit
Ok It seems you’re using UnityScript so i have to switch my mind first :wink:

As said above, you have to determine what data you need. You didn’t say much about the actual use of this data. Just the directions won’t help you much.

A way to do some kind of dead reckoning is to store the current action and the delay between the last action.

The temp data structure would look like this:

class MyAction
{
    var action : String;
    var delay : float;
}

In your script you can use a queue to store the actions and a coroutine / InvokeRepeating call to send them.
So it could look like this:

edit
this is my exact testing script and it works pretty well:

import System.Collections.Generic;

class MyAction
{
    var action : String;
    var delay : float;
}

var queuedData = new Queue.<MyAction>();
var lastTime : float = 0.0;
var direction = "";

function Start()
{
    SendData();
}

function Update()
{
    if (Input.GetKeyDown(KeyCode.W))
	{
	    direction = "forward";
		addtoDB();
	}
    if (Input.GetKeyDown(KeyCode.S))
	{
	    direction = "back";
		addtoDB();
	}
    if (Input.GetKeyDown(KeyCode.D))
	{
	    direction = "right";
		addtoDB();
	}
    if (Input.GetKeyDown(KeyCode.A))
	{
	    direction = "left";
		addtoDB();
	}
}

function addtoDB()
{
    var A = new MyAction();
    A.action = direction;
    A.delay = Time.time - lastTime;
    lastTime = Time.time;
    queuedData.Enqueue(A);
}

function SendData()
{
	while(true)
	{
        if (queuedData.Count>0)
        {
            var form = new WWWForm();
			Debug.Log("Send" + queuedData.Count);
            while (queuedData.Count>0)
            {
                var A = queuedData.Dequeue();
                form.AddField("action[]", A.action);
                form.AddField("delay[]", A.delay.ToString());
            }
            var phpurl = "http://localhost/writer.php";
            var sendR = WWW(phpurl, form);
            yield sendR;
		    Debug.Log(sendR.text);
        }
	    yield WaitForSeconds(5);
	}
}

MyPHP script looks like this (it just returns the data):

<?PHP
    $actions = $_POST['action'];
    $delays = $_POST['delay'];
    if (isset($actions))
    {
        $count = sizeof($actions);
        for($counter = 0; $counter < $count; $counter++)
        {
            echo $actions[$counter] . " = " . $delays[$counter] . "

";
}
}
?>

So the player pressed forward 2.6 seconds after the game started, he run forward for 4.6 sec. and then pressed left for 2.1 sec. then he stopped for 10.2 sec. …

You should also rethink your if-elas chains when reading the key input. While you press forward you can’t press any other key. You might want to use GetButtonDown, but you can’t detect the release of all keys that way. But that wasn’t part of the question here ;).

*edit
Some possible reasons for a freeze:

You might have a while loop that never ends that doesn’t have a yield in it. Generally infinite loops crash / freeze the application because it is caught in this loop and can’t do anything else. When using yield in a function the function exits at the yield and Unity can continue it’s other tasks. Unity’S coroutine scheduler will resume your coroutine when it’s time to do so.

Another thing that might confuse is this loop:

while (queuedData.Count>0)

This does work because the Dequeue function removes an object from the queue so that decreases the count. This way the loop runs until the queue is empty. Other containers like List would also work, but you have to remove the element manually.

SendData is a coroutine that runs in the background. It’s started once in start and checks every 5 sec. if there’s data to be send.

You need to use AddField with a capital “A”

This will be extremely unperformant; please consider using sockets. If you want simpleness use NodeJS.