- Home /
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
D'oh! Thanks $$anonymous$$ike and Bunny83 - I'm not all that familiar with Unity/JS functions. Bunny83, how would you suggest collecting locally? I don't know of a method other than via a DB so any suggestions or pointers greatly appreciated.
Well, it depends on how often is the user allowed to perform an action? Well i would just use a generic queue, but you have to make yourself clear what exact data you need. Next problem is how to parse the data in php. One way is to pass each value as an array. I haven't tested this yet since i don't work with php frequently, but like mentioned in this post it should be possible to post multiple values as one array variable.
I can add some more details in my answer...
Answer by Bunny83 · May 31, 2012 at 11:14 AM
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 ;)
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] . "\n";
}
}
?>
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.
I just tried it and it works, how ever with a little change. It seems InvokeRepeating doesn't work with coroutines... So just do every thing in the coroutine. I will change the sample...
Bunny83 Thanks for this - really helpful! I don't need to do any dead reckoning for now but it's an interesting approach to know of for future work. (If I do some calculation on time and speed of transform I could plot a map of player activity with data, possibly?)I've not tested yet but I'll let you know how it goes. Thanks again.
When I try, it crashes the UnityPlayer (as does my corrected original code). $$anonymous$$ay be my localhost if it is working for you...
Are you sure you have a yield in the while(true)? A infinite loop will crash / freeze your application. A yield will interrupt the execution at this point
No, I copied and pasted the code above. It doesn't crash unless it's playing on my local PHP-enabled server, so there's maybe something in the sending of a WWWForm that seems to be crashing it...
Answer by Olly · Jun 26, 2016 at 08:17 PM
This will be extremely unperformant; please consider using sockets. If you want simpleness use NodeJS.
Your answer
![](https://koobas.hobune.stream/wayback/20220613072202im_/https://answers.unity.com/themes/thub/images/avi.jpg)