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

Variables

Started by Deimz, 02 November 2012 - 08:53 AM
Deimz #1
Posted 02 November 2012 - 09:53 AM
Hello Computercraft,
I'm currently making a calculator which uses variables from another file to define the Items per hour being made.


for T=0, 30, 1 do
local TimeTimer
TimeTimer = (T)
   print( "TimeTimer is "..tostring(TimeTimer) )
sleep(1)
end

I need the " TimeTimer" variable to be used in my calculator.


local x = 0
while true do
os.pullEvent("redstone") -- wait for a "redstone" event
if rs.getInput("right") then -- check the input
x = (x)+1
textutils.slowPrint( "item went past "..tostring(x).." times" )
shell.run("monitor","left","itemsdisplay")

-- X/TimeTimer = y

-- y*3600 = answer
end
end

Could somebody please help me get "TimeTimer" variable working in the second piece of code? Both of the files are located in same location, local variables could be used, but I'm not sure how to use them.
Deimz #2
Posted 02 November 2012 - 10:05 AM
#bump
zeekkay #3
Posted 02 November 2012 - 10:10 AM
http://computercraft...le=Fs_%28API%29

This is the API you're looking for. Use something like h = fs.open("filename","r") to open a file so that it may be read. foo is a handle for the file
Then use something like stringname = h.readLine() to get the first line of the file.
Just be sure to use tonumber(stringname) to convert the type from a string so you can work with it.

Make sure you close it afterwards! Good practice! Especially in multiplayer! h.close()

Not a fan of using files to store data, unless it is absolutely necessary. (As in "in case of server crash")
Deimz #4
Posted 02 November 2012 - 10:32 AM
http://computercraft...le=Fs_%28API%29

This is the API you're looking for. Use something like h = fs.open("filename","r") to open a file so that it may be read. foo is a handle for the file
Then use something like stringname = h.readLine() to get the first line of the file.
Just be sure to use tonumber(stringname) to convert the type from a string so you can work with it.

Make sure you close it afterwards! Good practice! Especially in multiplayer! h.close()

Not a fan of using files to store data, unless it is absolutely necessary. (As in "in case of server crash")

Thanks for the reply, will try it out momentarily.. What would be my alternative to not use different files? I want the redstone input to be checked 100% of the time as the signal is a very important factor of this calculation, if I add in the the timer into my code I would have to use sleep(1) for a seconds pause, that would delay my redstone checks, and my calculations would be inaccurate, if possible could you expand on this?
Thank you :D/>/>
ChunLing #5
Posted 02 November 2012 - 12:10 PM
Put all the variables you want to save in a table, then you can write the textutils.serialize of it and reload by unserializing the readAll of the file.
Deimz #6
Posted 02 November 2012 - 12:21 PM
I have no idea how to use tables :/
darkrising #7
Posted 02 November 2012 - 12:45 PM
http://lua-users.org.../TablesTutorial

Edit: wait a second, couldn't you just make the first piece of code a function in the second? :D/>/>
Deimz #8
Posted 02 November 2012 - 01:00 PM
http://lua-users.org.../TablesTutorial

Edit: wait a second, couldn't you just make the first piece of code a function in the second? :)/>/>
Thanks for the link, would it work? :D/>/> *sorry just started CC yesterday* I need both codes to be running at all times, ignore the 30 cap on the counter, that'll be removed. I need the counter giving me variubles and I need them processed in the second bit * By processed I mean


-- X/TimeTimer = y
-- y*3600 = answer

That needs to be updated also, every second the 'answer' is going to be printed.

edit:
If you could re-write it with a bit of explanation of you did it would be very pleased <:)/>/>
darkrising #9
Posted 02 November 2012 - 02:14 PM
You could do something like this:


function TimeTimer()
  while true do
    <code>
    TimeTimer = <code> -- dont make it local
  end
end


function theFunction()
  local x = 0
  while true do
    os.pullEvent("redstone") -- wait for a "redstone" event
    if rs.getInput("right") then -- check the input
    x = x+1
    textutils.slowPrint( "item went past "..tostring(x).." times" )
    shell.run("monitor","left","itemsdisplay")

    -- X/TimeTimer = y

    -- y*3600 = answer
    end
  end
end

parallel.waitForAll(TimeTimer, theFunction) -- runs both functions at same time, as they are both while true loops it will run forever
Deimz #10
Posted 03 November 2012 - 04:24 AM
You could do something like this:


function TimeTimer()
  while true do
	<code>
	TimeTimer = <code> -- dont make it local
  end
end


function theFunction()
  local x = 0
  while true do
	os.pullEvent("redstone") -- wait for a "redstone" event
	if rs.getInput("right") then -- check the input
	x = x+1
	textutils.slowPrint( "item went past "..tostring(x).." times" )
	shell.run("monitor","left","itemsdisplay")

	-- X/TimeTimer = y

	-- y*3600 = answer
	end
  end
end

parallel.waitForAll(TimeTimer, theFunction) -- runs both functions at same time, as they are both while true loops it will run forever
Thank youuu, trying it out now :D/>/> will tell you if it works :)/>/>