Search Unity

Not updating Text

Discussion in 'Scripting' started by mholmes, Oct 18, 2018.

  1. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    I'm trying to update text while my code performs a http request:

    code:
    Code (CSharp):
    1. public void SubmitTicket()
    2.     {
    3.         _txtTitle.text = "Please Wait...";
    4.         _Ticket.Product = _cbProducts.options[_cbProducts.value].text.ToString();
    5.         _Ticket.User_Email = _tbEmail.text;
    6.         _Ticket.Issue_Description = _tbIssue.text;
    7.  
    8.         Validate_Submission();
    9.  
    10.         if (_Valid)
    11.         {
    12.             //End Point
    13.             string uri = _EndPoint + "api/WhiteBoXGaming/Post_SubmitS";
    14.  
    15.             StartCoroutine(booleanwebrequest(uri, _Ticket));
    16.  
    17.             if (!_Response)
    18.             {
    19.                 _Core.Message_Box("Error: HTTP Request Failed", "Failed to submit your request. Please try again. If the problem persist, please contact support.", "OK");
    20.             }
    21.             else
    22.             {
    23.                 _Core.Message_Box("Information: Success", "Your request has been sent, please check your email to track your tickets progress. Thank you.", "OK");
    24.             }
    25.         }
    26.  
    27.         //Reset Values
    28.         _txtTitle.text = "Contact Support";
    29.         _Valid = true;
    30.     }
    the text never changes. Do i need to do something different to get it to refresh or repaint or update the text? Not sure why its not working.

    _txtTitle is the text I want to change while it performs the task.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    That isn't a coroutine or an async, so the entire thing is going to run in a single frame. If you change the text at the top of the function and then change it back to the original value at the bottom, then the text will always have the original value at the time any frame is rendered.
     
  3. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    What do you suggest? What is your suggested solution?
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Wait to change the text back to the original value until the request finishes, or until a certain amount of time has passed (or both).
     
  5. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    Code snippet would be helpful. BTW the co routine causes it to wait for a response otherwise I could not do line: 17.
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Calling StartCoroutine() does NOT pause your function until the coroutine finishes. (IIRC the coroutine will run until its first yield, and then the calling function continues.) I'm not sure how your current code works, or whether it works, but I am 100% sure that StartCoroutine() does not pause for the coroutine to finish if the coroutine contains waits.


    For changing text when the request is done:

    If you have a working http request that correctly interprets the response you get, then by definition you must already have some code somewhere that waits for the request to finish (not shown in your OP). You can change your text there.

    If you don't already have working code for http requests, then updating text is not the problem you need to solve right now.


    For changing text after a fixed amount of time:

    If you need help to write a coroutine that says "wait X seconds, then do Y", then you do not understand coroutines at all. Go read an introductory tutorial.
     
  7. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    Hmm I tried that

    code:
    Code (CSharp):
    1. IEnumerator booleanwebrequest(string uri, object obj)
    2.     {
    3.         try
    4.         {
    5.             string json = _Core.GenericObjectJson(obj);
    6.             var client = new HttpClient();
    7.             var webRequest = WebRequest.Create(uri);
    8.  
    9.             _txtTitle.text = "Please Wait...";
    10.             webRequest.Method = "POST";
    11.             webRequest.ContentType = "application/json";
    12.  
    13.             using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
    14.             {
    15.                 streamWriter.Write(json);
    16.             }
    17.  
    18.             var httpResponse = (HttpWebResponse)webRequest.GetResponse();
    19.             using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    20.             {
    21.                 var result = streamReader.ReadToEnd();
    22.                 _Response = Convert.ToBoolean(result);
    23.             }
    24.         }
    25.         catch (Exception ex)
    26.         {
    27.             _Response = false;
    28.         }
    29.  
    30.         _txtTitle.text = "Contact Support";
    31.         yield return _Response;
    32.     }
    Still not working
     
  8. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    As I suspected, you have no idea how coroutines work.
     
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Your coroutine doesn't appear to do anything other than execute completely in one go. Not sure why you're doing this in a coroutine this way. You might as well just make this a regular method, because that is essentially how it is behaving anyway.

    Coroutines are almost always used with a loop, where to prevent blocking the main thread you yield to space out execution of the entire loop across multiple frames. If you have no execution you're trying to spread out across multiple frames I don't see the value of this being a coroutine in the first place.

    Some short examples from the docs below. Notice every example includes a loop to be executed over multiple frames.
    https://docs.unity3d.com/Manual/Coroutines.html
     
    Last edited: Oct 18, 2018
  10. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    Thanks for the replies but this does not solve my issue or answer my question. Telling someone their code sucks in a nutshell is counter productive. All I am trying to accomplish is a simple progress update/text change to notify the user processing is occurring. I assume a co routing is equal to a background worker in .Net Pretty sure this is a basic/fundamental question. More than one way to skin this cat. Please post helpful code snippets related to the problem please or post solutions snippets / solution directly related to the problem.
     
    Last edited: Oct 19, 2018
  11. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I am not inclined to write you a working example, because it would take more work than you seem to be willing to put in, so I assume that you have decided this problem isn't important enough to warrant that level of effort.

    Nope! Coroutines do not involve multi-threading.

    The fundamental reason that your notifications are not working is because your code for making the HTTP call is not correct, so you're trying to build a new layer on top of a foundation that was never doing what you wanted in the first place.

    I'm not sure exactly which of several possible flavors of "not correct" it is, because I haven't used that particular HTTP library before, so I'd have to learn how to use it myself in order to tell you how to use it correctly.

    The most likely possibilities are

    A) You are not waiting for the asynchronous operation to complete, or
    B) You are waiting, but are blocking the main Unity thread while you wait, which prevents Unity from doing anything else--including rendering the new text you want it to display
     
    Joe-Censored likes this.