8 posts
Location
The Netherlands
Posted 20 April 2016 - 09:12 PM
I have been working on a program that gives me helpful information about my Big Reactors reactor and automatically turns it on and off. I decided to make a function containing all of the variables so that the code would be (possibly) easier to read. Now whenever I run my program it gets to the line where it loads the function and it stops there without giving me an error message. Does anyone know why this happens? Also, I only really need the fix so no need to optimize the rest of my code, as I'd like to do that myself.
Spoiler
-- Version
local version = "0.70"
-- This function can round a given number to a given amount of decimals
function round(val, decimal)
if (decimal) then
return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
else
return math.floor(val+0.5)
end
end
-- This function loads all of the variables
function Variables()
reactor = peripheral.wrap("BigReactors-Reactor_1")
active = reactor.getActive()
connected = reactor.getConnected()
energy = reactor.getEnergyStored()
energyPercentage = energy / 10000000 * 100
energyPercentageRounded = round(energyPercentage, 2)
energyPerTick = reactor.getEnergyProducedLastTick()
fuel = reactor.getFuelAmount()
fuelMax = reactor.getFuelAmountMax()
fuelPerTick = reactor.getFuelConsumedLastTick()
fuelTicksLeft = fuelMax / fuelPerTick
fuelSecondsLeft = fuelTicksLeft / 20
fuelMinutesLeft = fuelSecondsLeft / 60
fuelMinutesLeftRounded = round(fuelMinutesLeft, 0)
fuelPercentage = fuel / fuelMax * 100
fuelPercentageRounded = round(fuelPercentage, 2)
fuelSecondsLeftRounded = round(fuelSecondsLeft, 0)
fuelTicksLeftRounded = round(fuelTicksLeft, 0)
offWhenEnergy = 9750000
onWhenEnergy = 10000
timeLeftPreference = fuelMinutesLeftRounded
end
-- Start of the program
while connected == true do
sleep(0)
Variables()
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.yellow)
term.write("Automated Reactor Management BETA")
term.setCursorPos(1,3)
term.setTextColor(colors.white)
term.write("Activated: ")
if active == true then
term.setTextColor(colors.green)
term.write("Yes")
else
term.setTextColor(colors.red)
term.write("No")
end
term.setCursorPos(1,4)
term.setTextColor(colors.white)
term.write("Energy Stored: ")
if energy > 5000000 then
term.setTextColor(colors.green)
term.write(energy)
elseif energy > 2500000 then
term.setTextColor(colors.orange)
term.write(energy)
else
term.setTextColor(colors.red)
term.write(energy)
end
term.setTextColor(colors.white)
term.write(" (")
if energy > 5000000 then
term.setTextColor(colors.green)
term.write(energyPercentageRounded)
elseif energy > 2500000 then
term.setTextColor(colors.orange)
term.write(energyPercentageRounded)
else
term.setTextColor(colors.red)
term.write(energyPercentageRounded)
end
term.setTextColor(colors.white)
term.write(")")
term.setCursorPos(1,5)
term.write("Fuel: ")
if fuelPercentage > 50 then
term.setTextColor(colors.green)
term.write(fuelPercentageRounded)
elseif energy > 25 then
term.setTextColor(colors.orange)
term.write(fuelPercentageRounded)
else
term.setTextColor(colors.red)
term.write(fuelPercentageRounded)
end
term.setCursorPos(1,6)
term.setTextColor(colors.white)
term.write("Estimated time left in ")
if timeLeftPreference == fuelMinutesLeftRounded then
term.write("minutes: ")
elseif timeLeftPreference == fuelMinutesLeft then
term.write("minutes: ")
elseif timeLeftPreference == fuelSecondsLeftRounded then
term.write("seconds: ")
elseif timeLeftPreference == fuelSecondsLeft then
term.write("seconds: ")
elseif timeLeftPreference == fuelTicksLeftRounded then
term.write("ticks: ")
elseif timeLeftPreference == fuelTicksLeft then
term.write("ticks: ")
end
if timeLeftPreference == fuelMinutesLeftRounded then
if timeLeftPreference > 60 then
term.setTextColor(colors.green)
term.write(timeLeftPreference)
elseif timeLeftPreference > 15 then
term.setTextColor(colors.orange)
term.write(timeLeftPreference)
else
term.setTextColor(colors.red)
term.write(timeLeftPreference)
end
elseif timeLeftPreference == fuelMinutesLeft then
if timeLeftPreference > 60 then
term.setTextColor(colors.green)
term.write(timeLeftPreference)
elseif timeLeftPreference > 15 then
term.setTextColor(colors.orange)
term.write(timeLeftPreference)
else
term.setTextColor(colors.red)
term.write(timeLeftPreference)
end
elseif timeLeftPreference == fuelSecondsLeftRounded then
if timeLeftPreference > 3600 then
term.setTextColor(colors.green)
term.write(timeLeftPreference)
elseif timeLeftPreference > 900 then
term.setTextColor(colors.orange)
term.write(timeLeftPreference)
else
term.setTextColor(colors.red)
term.write(timeLeftPreference)
end
elseif timeLeftPreference == fuelSecondsLeft then
if timeLeftPreference > 3600 then
term.setTextColor(colors.green)
term.write(timeLeftPreference)
elseif timeLeftPreference > 900 then
term.setTextColor(colors.orange)
term.write(timeLeftPreference)
else
term.setTextColor(colors.red)
term.write(timeLeftPreference)
end
elseif timeLeftPreference == fuelTicksLeftRounded then
if timeLeftPreference > 72000 then
term.setTextColor(colors.green)
term.write(timeLeftPreference)
elseif timeLeftPreference > 18000 then
term.setTextColor(colors.orange)
term.write(timeLeftPreference)
else
term.setTextColor(colors.red)
term.write(timeLeftPreference)
end
elseif timeLeftPreference == fuelTicksLeft then
if timeLeftPreference > 72000 then
term.setTextColor(colors.green)
term.write(timeLeftPreference)
elseif timeLeftPreference > 18000 then
term.setTextColor(colors.orange)
term.write(timeLeftPreference)
else
term.setTextColor(colors.red)
term.write(timeLeftPreference)
end
end
if energy > offWhenEnergy then
reactor.setActive(false)
elseif energy < onWhenEnergy then
reactor.setActive(true)
end
end
Edited on 20 April 2016 - 07:15 PM
1080 posts
Location
In the Matrix
Posted 20 April 2016 - 09:31 PM
while connected == true do
Connected is never defined and thus will never be true. You either want to run your variables function right before the loop as well, or you want to define it yourself. By the way, use locals.
Lua Basics
8 posts
Location
The Netherlands
Posted 20 April 2016 - 09:33 PM
while connected == true do
Connected is never defined and thus will never be true. You either want to run your variables function right before the loop as well, or you want to define it yourself. By the way, use locals.
Lua Basics
Thanks for helping me, I forgot to check such things after moving everything to a function. I was using locals before I added everything to a function, but I removed them because I thought that was the case this happened.
8 posts
Location
The Netherlands
Posted 20 April 2016 - 09:43 PM
while connected == true do
Connected is never defined and thus will never be true. You either want to run your variables function right before the loop as well, or you want to define it yourself. By the way, use locals.
Lua Basics
Thanks for helping me, I forgot to check such things after moving everything to a function. I was using locals before I added everything to a function, but I removed them because I thought that was the case this happened.
Whenever the function runs it seems to not do anything and leave the variables empty, even after making everything local and running it once.
1080 posts
Location
In the Matrix
Posted 20 April 2016 - 09:47 PM
Because the function has it's own scope, so the local variables only exist in the function.
If you want local variables that the rest of your code can use, declare them at the top of your script.
local connected,othervariable,othervar,lala,doop
--#rest of code
Edited on 20 April 2016 - 07:48 PM
8 posts
Location
The Netherlands
Posted 20 April 2016 - 09:52 PM
Because the function has it's own scope, so the local variables only exist in the function.
If you want local variables that the rest of your code can use, declare them at the top of your script.
local connected,othervariable,othervar,lala,doop
--#rest of code
So I have to leave my variables outside of my function?
1080 posts
Location
In the Matrix
Posted 20 April 2016 - 09:54 PM
Just the declaration, you can keep the function pretty much the same as it is now just make sure that any variable you have or use, you put at the first line of the script like I did.
Also, I would wrap the peripheral just once, so like a line under the declarations do a peripheral.wrap to the variable for the peripheral, and then remove the line in your function.
8 posts
Location
The Netherlands
Posted 20 April 2016 - 09:57 PM
Just the declaration, you can keep the function pretty much the same as it is now just make sure that any variable you have or use, you put at the first line of the script like I did.
Also, I would wrap the peripheral just once, so like a line under the declarations do a peripheral.wrap to the variable for the peripheral, and then remove the line in your function.
Thanks for helping me.