Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by dysfunc · May 31, 2012 at 11:06 AM · wwwformformmissingmethodexceptionaddfield

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dysfunc · May 31, 2012 at 11:50 AM 0
Share

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.

avatar image Bunny83 · May 31, 2012 at 12:10 PM 0
Share

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...

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · May 31, 2012 at 01:14 PM 0
Share

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...

avatar image dysfunc · May 31, 2012 at 03:11 PM 0
Share

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.

avatar image dysfunc · May 31, 2012 at 03:14 PM 0
Share

When I try, it crashes the UnityPlayer (as does my corrected original code). $$anonymous$$ay be my localhost if it is working for you...

avatar image Bunny83 · May 31, 2012 at 04:31 PM 0
Share

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

avatar image dysfunc · Jun 01, 2012 at 03:15 PM 0
Share

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...

Show more comments
avatar image
1

Answer by whydoidoit · May 31, 2012 at 11:08 AM

You need to use AddField with a capital "A"

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What exactly does the wwwform.addfield do exactly? 0 Answers

REST API Response Error: 406 - not acceptable 1 Answer

Posting raw XML data to web - no parameter name 2 Answers

Get o Post method without open browser 1 Answer

Sending data to a .php service using Unity's WWW? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges