Issue With Shared.SendHTTPRequest

TechnIckSTechnIckS Join Date: 2007-01-14 Member: 59616Members
<div class="IPBDescription">Executes backwards!??</div>So i have this code:

<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->            
        
                ...... (more code above) .......
                local steamId = client:GetUserId()
        local temp = 3
                local test = 1
        Shared.SendHTTPRequest("http://www.blabla.com/testpage.php?id="..tostring(steamId), "GET", function(response)
            if response then
                Shared.Message("Is Authorized.")
                Shared.Message("Response is "..response)
                temp = tonumber(response)
                Shared.Message("Answer in function is "..temp)
            else
                Shared.Message("Is NOT Authorized.")
            end
        end)
        test = temp
        Shared.Message("test out of http request is "..test)
        return test
                ... (do other things with test) ...<!--c2--></div><!--ec2-->

The page it pulls just returns a simple number, in this case 19.

Whenever it executes this code however, this is what i get:

<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->test out of http request is 3
Is Authorized.
Response is 19
temp in function is 19<!--c2--></div><!--ec2-->


It looks like it executes backwards... it seems to ignore SendHTTPRequest and its code until after it returns test variable!

Can someone please give me a hand to manage to assign that http response to the test variable? In my head this makes 0 sense...

Thanks in advance!

Comments

  • xDragonxDragon Join Date: 2012-04-04 Member: 149948Members, NS2 Playtester, Squad Five Gold, NS2 Map Tester, Reinforced - Shadow
    It wont block execution until the response is received, so it continues executing and Prints out the test out of http request is 3 first.

    Once a response is received it runs that function you set.
  • InsaneInsane Anomaly Join Date: 2002-05-13 Member: 605Members, Super Administrators, Forum Admins, NS1 Playtester, Forum Moderators, NS2 Developer, Constellation, NS2 Playtester, Squad Five Blue, NS2 Map Tester, Subnautica Developer, Pistachionauts, Future Perfect Developer
    Like Dragon said, that function that changes the value of temp and prints the result is a callback function that is only called after your GET HTTP request is successful. If you want to assign the value of temp to test or do anything else with it you should do it inside the callback function.
  • TechnIckSTechnIckS Join Date: 2007-01-14 Member: 59616Members
    edited January 2013
    Thanks for the response fellas!

    Well the problem is that i need to use this inside the function.
    The function is like this:

    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->function setValue(ns2id):

    local temp
    if (condition a) -> check web site response (with ns2id argument) and assign it to temp.
    else if (condition b) -> assign X value to temp
    else -> assign Y value to temp
    return temp<!--c2--></div><!--ec2-->

    This function setValue MUST return the web site response when "condition a" is met.

    Some might suggest to run the web site response outside the function and store it in a variable ahead of time. I would love to, but the script parses certain variables to setValue that are used in the html query.

    Anyone have any idea how to accomplish this?
  • TechnIckSTechnIckS Join Date: 2007-01-14 Member: 59616Members
    edited January 2013
    Is there a way to make the main function wait for Shared.SendHTTPRequest to receive response before continuing execution? I've even tried putting a loop that just waits around for the callback function (added a bool X of true, did Shared.SendhTTPRequest and made the last line in the callback function change X to false, and after Shared.SendHTTPRequest i added a while x do end) - but it just sits in that endless loop, so that didn't work, and i'm getting really frustrated here....

    I even tried this:

    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->*temp and test variables defined somewhere up here*

    local function setResponse(response)
        temp = response
    end

    local function callPage()
        Shared.SendHTTPRequest("http://www.blah.org/blah.php?id=blah", "GET", setResponse)
    end

    local function importantFunction(blah)
            callPage()
            test = temp
            return test
    end<!--c2--></div><!--ec2-->

    and off course this didn't work either... **rips last 3 hairs off the head**
  • InsaneInsane Anomaly Join Date: 2002-05-13 Member: 605Members, Super Administrators, Forum Admins, NS1 Playtester, Forum Moderators, NS2 Developer, Constellation, NS2 Playtester, Squad Five Blue, NS2 Map Tester, Subnautica Developer, Pistachionauts, Future Perfect Developer
    It all rather depends on what you are doing with test after returning it from importantFunction(), but my suggestion would be to refactor a bit and send it to a handler function, a little like this:

    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->if condition == a then
        Shared.SendHTTPRequest("http://www.blah.org/blah.php?id=blah", "GET", function(response)
            DoSomething(response)
        end)
    else
        DoSomething(defaultValue)
    end<!--c2--></div><!--ec2-->
Sign In or Register to comment.