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

Function ending right away

Started by NocturnalDanger, 14 June 2016 - 06:09 PM
NocturnalDanger #1
Posted 14 June 2016 - 08:09 PM
So I have this code:
http://pastebin.com/9jMNw83s

and when I run it, it seems to work fine. However when I select an output (basically every button on screen) it goes to the function continue(), but after the os.sleep it goes right back to main screen.

Before I had:

local function continue()
	--Set up monitor for function
	mon.clear()
	mon.setTextColor(colors.red)
	mon.setCursorPos(18,13)
	mon.write("Touch here to continue.")
mon.setCursorPos(14,27)
mon.write("Touch here to add another fluid")
	os.sleep(2) --Pause to avoid double-tap
	if xPos >= 18 and xPos <= 40 and yPos == 13 then --Test for touch here
	   redstone.setBundledOutput("top", 0) --Stop Redstone
	end
end

And the problem started after I changed it to:

local function continue()
	--Set up monitor for function
	mon.clear()
	mon.setTextColor(colors.red)
	mon.setCursorPos(18,13)
	mon.write("Touch here to continue.")
mon.setCursorPos(14,27)
mon.write("Touch here to add another fluid")
	os.sleep(2) --Pause to avoid double-tap
	if xPos >= 18 and xPos <= 40 and yPos == 13 then --Test for touch here
	   redstone.setBundledOutput("top", 0) --Stop Redstone
	redstone.setBundledOutput("right", 0) --Stop Redstone
	redstone.setBundledOutput("left", 0) --Stop Redstone
	redstone.setBundledOutput("back", 0) --Stop Redstone
elseif xPos >= 14 and xPos <= 44 and yPos == 27 then --Test for touch here
	-- Straight to end of continue(), to select new fluid.
	end
end

I tried getting rid of the os.sleep (By making it a comment (–os.sleep)) and the monitor didnt do anything. (or it went straight to the main screen again, without the pause.)

Any Ideas? Should I declare the event before I declare the function? (Actually, Ill try that now)

EDIT: No improvement on declaring the event sooner. It doesnt leave the function right away, but it doesnt actually leave continue() at all.
Edited on 14 June 2016 - 06:18 PM
The_Cat #2
Posted 14 June 2016 - 11:02 PM
So, when you click on the monitor on the main screen you go to the continue() screen then it immediately goes off it / doesn't wait for the second input? If so the reason it doesn't wait for the input is because you are not asking for it, you are checking the xPos and yPos from the previous touch. (Check how the first main screen works for touching and implement it for the continue function. Hint: os.pullEvent("monitor_touch"))
Edited on 14 June 2016 - 09:02 PM
NocturnalDanger #3
Posted 14 June 2016 - 11:53 PM
So, when you click on the monitor on the main screen you go to the continue() screen then it immediately goes off it / doesn't wait for the second input? If so the reason it doesn't wait for the input is because you are not asking for it, you are checking the xPos and yPos from the previous touch. (Check how the first main screen works for touching and implement it for the continue function. Hint: os.pullEvent("monitor_touch"))

I have an os.pullevent on line 56. And thats before most of the code. (It shouldnt need to be before I decalre a function, but I tried it and it still didnt work.)

I gave the link to the pastebin on the very top of the post, if you missed it, take a look. (everything is done by me, if you have a way to clean it up, let me know. I fixed allot of the indentations on my computer but I didnt reupload it.)
Edited on 14 June 2016 - 09:54 PM
Bomb Bloke #4
Posted 15 June 2016 - 01:06 AM
I have an os.pullevent on line 56.

But you don't have one in your "continue" function, after having told the user to "Touch here to continue". That function pauses a couple of seconds (due to the sleep) and then near-instantly returns; at which point your main loop wipes the screen and redraws it again.

At every point where you want your script to halt and wait for an event, you have to tell it to halt and wait for an event (eg, by calling os.pullEvent() again).
NocturnalDanger #5
Posted 15 June 2016 - 05:45 AM
I have an os.pullevent on line 56.

But you don't have one in your "continue" function, after having told the user to "Touch here to continue". That function pauses a couple of seconds (due to the sleep) and then near-instantly returns; at which point your main loop wipes the screen and redraws it again.

At every point where you want your script to halt and wait for an event, you have to tell it to halt and wait for an event (eg, by calling os.pullEvent() again).

Well lines 59 through 209 work fine, even though I dont have it 48 times. I assumed that since Im calling the function within those lines too, that it shouldnt need it.

EDIT: So I put on in my continue function and it works (Thank you) But I envisioned when you click "Add another fluid" that it would keep the redstone signal active (On both outputs), however when you select a new output it shuts the old one off.

WHICH isnt an issue, but its something that works differently than expected. And I was wondering why. (Honestly, if its super simple, Ill change it, because thats adds an ability to "cross out" or "cover" active signals which would make alloying easier. But if its allot of work I just wont mess with it.)

EDIT2: My updated code is pastebin.com/QcghX0xa
Edited on 15 June 2016 - 05:15 AM
Bomb Bloke #6
Posted 15 June 2016 - 07:36 AM
The idea is that setBundledOutput doesn't toggle colours; rather it sets a specific combination of colours to be active or inactive. If you specify the colour red, then you get only red; if you later specify black, then red disables and black activates. If you want both at once you have to request both at once.

Really what's going on is that every colour is represented by a number; a power of two. Getting a "combination of colours" is hence as easy as adding those numbers together (or more accurately, ORing them together).

This is where the functions in the colours API come in handy. Eg, if you want to enable the colour red on a given side, without affecting the other colours already enabled there and regardless as to whether red is already enabled, you might do:

rs.setBundledOutput("left", colours.combine(rs.getBundledOutput("left"), colours.red))
Edited on 15 June 2016 - 05:36 AM
NocturnalDanger #7
Posted 15 June 2016 - 08:13 AM
The idea is that setBundledOutput doesn't toggle colours; rather it sets a specific combination of colours to be active or inactive. If you specify the colour red, then you get only red; if you later specify black, then red disables and black activates. If you want both at once you have to request both at once.

Really what's going on is that every colour is represented by a number; a power of two. Getting a "combination of colours" is hence as easy as adding those numbers together (or more accurately, ORing them together).

This is where the functions in the colours API come in handy. Eg, if you want to enable the colour red on a given side, without affecting the other colours already enabled there and regardless as to whether red is already enabled, you might do:

rs.setBundledOutput("left", colours.combine(rs.getBundledOutput("left"), colours.red))

So if I have black active, and I then run

rs.setBundledOutput("left", colours.combine(rs.getBundledOutput("left"), colours.red))
Itll ADD red, so then both black and red are active?

getBundledOutput lists the active colors and then adds red to them, it seems.

(Sorry, my programming knowledge is limitted to CSS, HTML, some Java, some C++ and LabView(Sadly)), used to know Python. So this is a whole new world to me.)

EDIT: Would this mess work?


	    redstone.setBundledOutput("left", colors.combine(colors.combine(redstone.getBundledOutput("left"), redstone.getBundledOutput"right"), colors.combine(getBundledOutput("back"),colors.red)))
Edited on 15 June 2016 - 06:48 PM
Bomb Bloke #8
Posted 16 June 2016 - 04:37 AM
So if I have black active, and I then run

rs.setBundledOutput("left", colours.combine(rs.getBundledOutput("left"), colours.red))
Itll ADD red, so then both black and red are active?

That's what I said, yes.

EDIT: Would this mess work?

redstone.setBundledOutput("left", colors.combine(colors.combine(redstone.getBundledOutput("left"), redstone.getBundledOutput"right"), colors.combine(getBundledOutput("back"),colors.red)))

Well, no, you're missing too many brackets, along with a few other characters.

It looks like you meant:

rs.setBundledOutput("left", colours.combine(rs.getBundledOutput("left"), rs.getBundledOutput("right"), rs.getBundledOutput("back"), colours.red))

… though even then I'm suspecting you meant to use some "rs.getBundledInput"s in there instead.
NocturnalDanger #9
Posted 16 June 2016 - 05:38 AM
rs.setBundledOutput("left", colours.combine(rs.getBundledOutput("left"), rs.getBundledOutput("right"), rs.getBundledOutput("back"), colours.red))

… though even then I'm suspecting you meant to use some "rs.getBundledInput"s in there instead.


Yes, thank you.