Cant Change Variables In A Function...

Hello!

I’m trying to make a program that get data from a database very often. Now I got this strange problem. I have a networklistener that is used in a network request. First I managed the data I got with the networklistener in another function that was called in the networkrequest but after severel hours (days!) of trial and error I put everything in the networklistener just to see if it worked. 

I alse added a local variable outside of the networkrequest-function that I changed last in the networkrequest function to see if atleast that one got changed to true but it wasn’t! All variables I use are only active in the function and not out side it.

I’m going to explain as good as I can what I’m trying to achive.

  • Get data from database

  • Store it in a lua table (it’s like 20 values)

-Use the table in my application (One of them is just a speedometre like in a car)

I use a three-tier architecture so the connection to the database and the querries to it is done on another server.

All my app is doing is pulling that answer from the server via a JSON object so the variable “myNewData” contains the JSON object and the variable “decodedData” contains the… decoded JSON object.

My real problem is that the data is not saved outside the networklistener. Its gone.

Can anyone see what I’m doing wrong. I’m sorry for my bad looking code, but I’ve tried to solve the same problem for days and it starts to get messy…

This is the code.

print("Begin teting!") done1 = false local navTable = {     Eng\_Spd = 0,     Spd\_Set = 0 }      local changeTab = function()     navTable.Eng\_Spd = 2 end printNavTable = function()     print("navTable innehåller: ")     print(navTable.Eng\_Spd)     print(navTable.Spd\_Set) end     require "sqlite3" local myNewData  local json = require ("json") local decodedData local SaveData2 = function(tab)     local i = 1     local counter = 1     local index = "livedata"..counter     local navValue = decodedData[index]     print(navValue)          while (navValue ~=nil) do         --tablefill ="INSERT INTO navaltable VALUES (NULL,'" .. navValue[1] .. "','" .. navValue[3] .."','" .. navValue[4] .."','" .. navValue[5] .."','" .. navValue[6] .."');"         --print(tablefill)         --db:exec(tablefill)         if     navValue[3] == "Eng Spd" then navTable.Eng\_Spd = navValue[4]         elseif navValue[3] == "Spd Set" then navTable.Spd\_Set = navValue[4]              else print("blah")         end         print(navTable.Eng\_Spd)         print(navTable.Spd\_Set)         counter=counter+1             index = "livedata"..counter                 navValue = decodedData[index]                          end     end    function networkListener( event )     if (event.isError) then             print("Network error!")     else             myNewData = event.response             print("From server: "..myNewData)             decodedData = (json.decode(myNewData))          local counter = 1     local index = "livedata"..counter     local navValue = decodedData[index]     print(navValue)          while (navValue ~=nil) do         --tablefill ="INSERT INTO navaltable VALUES (NULL,'" .. navValue[1] .. "','" .. navValue[3] .."','" .. navValue[4] .."','" .. navValue[5] .."','" .. navValue[6] .."');"         --print(tablefill)         --db:exec(tablefill)         if     navValue[3] == "Eng Spd" then navTable.Eng\_Spd = navValue[4] print("FÖR FAAAAAAAAAAAAAN!")         elseif navValue[3] == "Spd Set" then navTable.Spd\_Set = navValue[4]              else print("blah")         end         print(navTable.Eng\_Spd)         print(navTable.Spd\_Set)         counter=counter+1             index = "livedata"..counter                 navValue = decodedData[index]                          end         --db:exec("DROP TABLE IN EXISTS navaltable")     end     print("Set done 1 to true")     done1 = true end         printNavTable() network.request( "http://127.0.0.1/firstMidle.php", "GET", networkListener ) if done1 == true then --changeTab() print("in done!") --uppdateNavalTable() printNavTable() end print("Done!")  

And here is the output (console):

Copyright (C) 2009-2012  C o r o n a   L a b s   I n c .         Version: 2.0.0         Build: 2012.971 Begin teting! navTable innehÃ¥ller: 0 0 Done! From server: {"livedata1":["1","0","Eng Spd","30","0","2013-03-15 11:35:48"],"li vedata2":["1","1","Spd Set","13","0","2013-03-15 11:35:37"]} table: 02EF1330 FÖR FAAAAAAAAAAAAAN! 30 0 30 13 Set done 1 to true  

And finaly, sorry for this wall of text but I realt need help here and I want you to understand my problem.

Kind regards!

** UPDATED **

Sorry for the short response, but where are you trying to access this data from and which variable specifically is giving the problem,

myNewData and/or decodedData

Is SaveData2() giving you the problem?

Please note, I don’t see anything in your code that would prevent the data from being retained in either of those file local variables, but consider this:

  1. Attempting to access these variables from another file will not work.
  2. Attempting to access these variables before the network listener is complete will also fail.

Is it possible that you are attempting one of these two things?

Thanks for the reply!

I’m not accessing them yet. I do that in the “real” app. This is just a test file. If you mean the data that is stored in my new data I get it from a mariaDB and the url in the network request leads to a php script that returns a JSON object which in turn contains the answer of the querry to the db…

myNewData  or  decodedData  are not giving me problem I think. Both contains what it should. And if I print what is in navTable.Eng_Spd after I put navValue[4] in it, in the networklistener, It prints the value I want. If I print it after navTable.Eng_Spd = 0.

SaveData2() is not giving me problems. I moved what the functions do in to the networklistener just for testing. When I used SaveData2() I got the same problem.

What scares me is that every print in networklistener comes last even though I call it first. Eg. notice print(“done!”) and print(“Set done1 to true”). 

So maybe “2. Attempting to access these variables before the network listener is complete will also fail.” is the problem. I will try to do something to make it finish and see if it work.

Hi,

The program doesn’t pause while the network listener function is running, it carries on down.

So your “if done1 == true” will never be true when that statement is hit.

That is also why you are seeing “Done!” at the top of your output.

You need to move that stuff into functions and then call them from your network listener.

Dave

Thank you both! The problem was nr 2. The networklistener was running while the program continued.

** UPDATED **

Sorry for the short response, but where are you trying to access this data from and which variable specifically is giving the problem,

myNewData and/or decodedData

Is SaveData2() giving you the problem?

Please note, I don’t see anything in your code that would prevent the data from being retained in either of those file local variables, but consider this:

  1. Attempting to access these variables from another file will not work.
  2. Attempting to access these variables before the network listener is complete will also fail.

Is it possible that you are attempting one of these two things?

Thanks for the reply!

I’m not accessing them yet. I do that in the “real” app. This is just a test file. If you mean the data that is stored in my new data I get it from a mariaDB and the url in the network request leads to a php script that returns a JSON object which in turn contains the answer of the querry to the db…

myNewData  or  decodedData  are not giving me problem I think. Both contains what it should. And if I print what is in navTable.Eng_Spd after I put navValue[4] in it, in the networklistener, It prints the value I want. If I print it after navTable.Eng_Spd = 0.

SaveData2() is not giving me problems. I moved what the functions do in to the networklistener just for testing. When I used SaveData2() I got the same problem.

What scares me is that every print in networklistener comes last even though I call it first. Eg. notice print(“done!”) and print(“Set done1 to true”). 

So maybe “2. Attempting to access these variables before the network listener is complete will also fail.” is the problem. I will try to do something to make it finish and see if it work.

Hi,

The program doesn’t pause while the network listener function is running, it carries on down.

So your “if done1 == true” will never be true when that statement is hit.

That is also why you are seeing “Done!” at the top of your output.

You need to move that stuff into functions and then call them from your network listener.

Dave

Thank you both! The problem was nr 2. The networklistener was running while the program continued.