Loading html content to variable

Hello, I have a background and programming and scripting language but I am new to Lua, I have a feeling it’s probably something really minor and amateur.

Imagine i have file called “dircheck” with no extension on a sever and the contents are “TextLoaded123”

I am trying to load those contents into a variable in my app. The response prints correctly the contents of the file but I can’t seem to get it into the variable. The print results for the variable at the end are still the initial value I gave it which is “test”

[lua]local loadedHTMLtext = “test”

local function networkListener( event )
if ( event.isError ) then
print( “Network error!”)
else
loadedHTMLtext = event.response
print ( "RESPONSE: " … event.response )
end
end

network.request( “http://sample.com/sampledir/dircheck”, “GET”, networkListener )

print ( loadedHTMLtext )[/lua]
Anyone know what I’m doing wrong?

Thank you in advance for your help! I feel embarrassed even having to ask :stuck_out_tongue: [import]uid: 93934 topic_id: 15832 reply_id: 315832[/import]

so what exactly is the question?

Line 14 prints “test”

but then after a small instance of time, Line 8 is fired assuming that the file is present and loaded.

That is because the first thing that app does is execute the commands from line 1 to 14, where by 3 to 10 are a function which is called only later. So you get the default value of “test”

then the page is loaded, and you get the actual value.

there are a couple of ways for dealing with this,

  1. Have a callback that returns to your code when you call the network.request so your app knows when the HTML returned

  2. Have a notification that will notify a function to trigger when the value of a variable changes (Custom implementation, Lua does not offer this by default)

  3. Use coroutines where your app thread can stop execution and continue on either an error or receiving the data.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15832 reply_id: 58548[/import]


so what exactly is the question?

The question was what am I doing wrong! :stuck_out_tongue:

Thanks for your help, I didn’t know this is how the code is loaded. I assumed the Function would be run after line 12 since that’s when it was triggered and I loaded it up before then. Makes sense now though.

Thank you! [import]uid: 93934 topic_id: 15832 reply_id: 58636[/import]