This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
chrisprij's profile picture

[Lua][Question] Problems accessng global variables after startup

Started by chrisprij, 13 January 2013 - 05:55 PM
chrisprij #1
Posted 13 January 2013 - 06:55 PM
Hello.

There's data that my turtle's hold, and they save it to a file when a program finishes running. One piece of data, for example, are the current coordinates. Also, if I want to log off while they are in the middle of a program, I have a rednet wireless call from my main computer that has them save their position, state, etc. so that they can continue their work when starting up again.

After the startup is where the problem arises. One startup, it loads the information from the file, and such information that gets used a lot, like the position variable X, Y, and Z, get loaded into global variables. Everything runs smoothly, I can use these variables during startup, no problem. Everything works fine, and there is no problem with my code.

After startup, however, whenever I call these global variables, they all are "nil". Is there a particular reason for this?

Here is my code. getCoords() just prints the coordinates to the screen (which works fine if this gets called during startup, but after startup, it fails).


function saveCoords()
local h = fs.open("coords", "w")
h.writeLine(tostring(X))
h.writeLine(tostring(Y))
h.writeLine(tostring(Z))
h.close()
print("Save successful!")
end
function loadCoords(source)
local h = fs.open(source, "r")
X = tonumber(h.readLine())
Y = tonumber(h.readLine())
Z = tonumber(h.readLine())
h.close()
print("...")
print("Loading Complete!")
getCoords()
end

Thanks in advance! :D/>
Luanub #2
Posted 13 January 2013 - 08:05 PM
Can you post the rest of the code?
chrisprij #3
Posted 13 January 2013 - 10:59 PM
Here's my Coordinates code (does the loading, saving, calibrating, and printing to the screen of coordinates):



function getCoords()
print("Coordinates: ("..tostring(X)..", "..tostring(Y)..", "..tostring(Z)..")")
end
function calibrate (x, y, z)
	    --X, Y, Z are global vars (no "local")
X = x
Y = y
Z = z
getCoords()
end
function saveCoords()
local h = fs.open("coords", "w")
h.writeLine(tostring(X))
h.writeLine(tostring(Y))
h.writeLine(tostring(Z))
h.close()
print("Save successful!")
end
function loadCoords(source)
local h = fs.open(source, "r")
X = tonumber(h.readLine())
Y = tonumber(h.readLine())
Z = tonumber(h.readLine())
h.close()
print("...")
print("Loading Complete!")
getCoords()
end

and here's my startup:


--import Coordinates file
os.loadAPI("programs/Coordinates")
--load coordinates from file
Coordinates.loadCoords("coords")
print("")
--prints coordinates to screen
Coordinates.getCoords()

and here's a test-file I run before reboot, to set the coordinates and save them to the file:


os.loadAPI("programs/Coordinates")
--calibrate to specific numbers.
Coordinates.calibrate(10, 15, 20)
--save the coordinates to the file.
Coordinates.saveCoords()
--print the coords, to make sure they r still there.
Coordinates.getCoords()

Everything runs well, even during startup, but when I try to access the global vars (which were used correctly in startup), they all are "nil". On startup, it prints

"Coordinates: (10, 15, 20)"

but after that, anything I do to access or use the X, Y and Z global variables doesn't work, and all 3 come back as nil.
Zeph #4
Posted 06 August 2013 - 05:51 AM
I'm having this trouble too.

This works:

-- "startup" file
globalVar = 1

-- "test" file
print(globalVar) -- prints 1

This does not work:

-- "startup" file
globalVar = 1
os.loadAPI("api/testapi")

-- "api/testapi" file
if globalVar then -- prevents nil error
  print("testapi globalVar: "..globalVar)
end
globalVar = 2

-- "test" file
print(globalVar) -- prints 1

It seems global variables are not visible to APIs. Any advice?

EDIT: Funnily enough, if i simply "dofile("api/testapi")" in startup (instead of os.loadAPI) it works, and the functions of "testapi" are available to the "test" file. Is this an acceptable way of doing things?
EDIT: except it fails for things like testapi.testfunction() calls, like you did for APIs…
immibis #5
Posted 06 August 2013 - 05:23 PM
It would be Coordinates.X/Coordinates.Y/Coordinates.Z but only functions are exposed from APIs. Make a function to return them.