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

A few questions

Started by coolmodi, 21 May 2012 - 01:31 AM
coolmodi #1
Posted 21 May 2012 - 03:31 AM
Hello,

i have a few questions about LUA:

1. Is it possible to jump to different positions in the code?
SpoilerE.g. from where i call the JUMPTOTOPBUTHOW function to where it is defined

function JUMPTOTOPBUTHOW()
end
print("test")

function otherspam()
print("dsfsdfg")
sleep(1)
print("dsfsdfg")
sleep(1)
print("dsfsdfg")
sleep(1)
print("dsfsdfg")
sleep(1)
end

while true do
os.startTimer(0.5)
event, param1 = os.pullEvent()
if event=="char" and param1=="b" then
  otherspam()
elseif event=="char" and param1=="v" then
  JUMPTOTOPBUTHOW()
elseif event=="timer" then
  print("b for different spam")
  print(" and v for test")
  print("asdf")
end
end

2. Is there a way in CC to run two or more programms at once?
For Example:

Programm "a" that has a menu that i access directly at the terminal.
Programm "b" that runs in the background of "a" and refreshes the monitoroutput all the time.

So is there a way to let programm "a" run "b" in the background?

3. If i have a table through something like that:

eustorage = sensors.getSensorReadingAsDict(side,sensor,[b][b]targets[1],"EUStorage")
i can access thing inside with

enr=eustorage[r.storage.val]
t=eustorage.tier
rm=eustorage.redstoneMode
write("1 EU="..enr.." Tier="..t.." RMode="..rm) print("")
but how do i output the whole table as it is?
I only manage to get things like asd5as5d as output, whereas eustorage.tier gives 3 for example.
coolmodi #2
Posted 21 May 2012 - 04:33 PM
Ok for 2. i found

parallel.waitForAll( function1, function2, so on)
parallel.waitForAny( function1, function2, so on)
but how does it actually work?
Can i make something like that:

function 1()
	while true do
	something
	end
end
function 2()
	while true do
	another thing
	end
end
parallel.waitForAny(1(),2())
and it will only go on if one of the loops breaks?

Can't test it atm :P/>/>
MysticT #3
Posted 21 May 2012 - 08:02 PM
1. Yes and no. You can make functions and call them, but when they end it continues where it was before the call.
Example:

local function hello()
  print("Hello World!")
end

print("This prints before")
hello()
print("This prints after")

2. It's almost like you said. You have to use parallel like this:

local function f1() -- 1 can't be a function name, it's a number, so use f1 or something
  while true do
    -- do something
  end
end

local function f2()
  while true do
    -- do something else
  end
end

parallel.waitForAny(f1, f2) -- you can also use parallel.waitForAll(f1, f2)
It will run one function, and when it yields (using os.pullEvent, os.pullEventRaw, coroutine.yield, etc.) it runs the next one, when that one yields the next one, and so on with every function, then it runs again the first one (from where it yielded), and starts again.
Example:

local function first()
  print("1")
  os.pullEvent()
  print("3")
end

local function second()
  print("2")
  os.pullEvent()
  print("4")
end

parallel.waitForAll(first, second)
If you run that code, it will print "1", "2", "3" and "4" (in that order).

3. I don't really understand what you want. If you need to print all the values in a table, you can use:

for key, value in pairs(t) do -- being t a table
  print(key, ": ", value)
end
It would print every key-value pair in the table.
Tables can use any value as an index (numbers, strings, booleans, functions, etc.). To index a table (get the value of the table for some key/index) you can use:

-- being t a table
t[1] = 10
t.two = "Hello" -- this is the same as t["two"] = "Hello"
t[someFunction] = false -- if someFunction isn's the string "someFunction", you can't do t.someFunction, since this would be like t["someFunction"]
coolmodi #4
Posted 22 May 2012 - 12:47 AM
Thanks MysticT, that heped a lot!
I finally understand tables now :P/>/>

But i have another problem:

event, param1, param2 = os.pullEvent()
while event ~= "char" and param1 ~= "s" do
	  print("t")
	  sleep(2)
end
Shouldn't this do print(" t") every 2 seconds until i press "s"?
Because this starts spamming "t" IF i press s or any other button?!?

I got it to actually stop by pressing a button, but still ANY button:

function isspressed()
   os.startTimer(0.1)
   event, param1, param2 = os.pullEvent()
   if event=="char" and param1=="s" then
	   return true
   elseif event=="timer" then
	   return false
   end
end

while isspressed()==false do
   print("t")
   sleep(1)
end

The problem seems to be, that issspressed() returns nothing while a button is pressed, even s :/
How can i make it so, that it will stop if i press a defined button?
MysticT #5
Posted 22 May 2012 - 01:18 AM
os.pullEvent gets the next event on the queue, if there's no events it stops your program until there's one. All that sleep does is start a timer and wait for a "timer" event.
What your first program does:
1 get an event from the queue
2 while that event is not a "char" event and the parameter isn't "s"
3 print "t"
4 wait 2 seconds
5 go back to 2
It always checks for the first event you received, never updating the event and param1 variables, so the while condition will always be the same.
Using sleep causes any event that happens in that time to be ignored. You shouldn't use it if you want to get events.

The correct way to do that would be:

local timer = os.startTimer(2) -- start a timer with 2 seconds
while true do
  local evt, arg = os.pullEvent() -- get an event
  if evt == "char" then -- if it's a "char" event
	if arg == "s" then -- and the argument is "s"
	  break -- break the loop
	end
  elseif evt == "timer" then -- if it's a "timer" event
	if arg == timer then -- and the argument is our timer
	  print("t") -- print "t"
	  timer = os.startTimer(2) -- restart the timer
	end
  end
end
print("Loop stoped, Goodbye...")