Jump to content




passing parameters to running program with read()


12 replies to this topic

#1 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 24 July 2019 - 07:29 AM

As the title implies I'm having trouble passing certain parameters to a running program.
The program is a simple button toggled door contriller and imo holding Ctrl-T is not a very elegant solution.
I currently don't have access to my pc so no code :( , but what i do have is the jist of it:

Declare variables
While not ex do
Write button
os.pullEvent("mouse_click")
Function exit()
If read == "exit" then
ex = true
end
If ex then break
End

As you can see by this pseudo code it is very simple, but the issue I'm having is that i can't read and detect mouse clicks at the same time. I have indeed tried coroutines and parallels but the former allways errors and the latter naver runs both.
Either way I'm willing to rewrite the read command so long as it works, and i can not stress this enough, it is extremely necessary for my intentions to be able to read while pulling events.

P.S. does anyone have the sauce for the read command? I have probed Dan's code like an Ayy in his LMAO and this tomf**kerry has me all tomf**kered out, and as anyone who works with code can tell you; getting pushed down the interdimentional soulstairs of code is exhausting!

Great thanks in advance,
-qwerty

#2 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 24 July 2019 - 07:43 AM

Can you post what you tried with the parallel API? There's definitely no situation where it should only run one of them.

View Postqwerty, on 24 July 2019 - 07:29 AM, said:

P.S. does anyone have the sauce for the read command?
The source can be found here. That said, it's a bit of a behemoth of a function, so not sure how useful it'll be here.

Edited by SquidDev, 24 July 2019 - 07:43 AM.


#3 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 24 July 2019 - 08:36 AM

Well not that it only ran one, rather it simply yealded the firs and errored after the srcond if it wasn't "exit".
My apologies!

P.S. how would i go about re writing read to act like write rather than print and let me pull other events in the background?

#4 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 24 July 2019 - 01:11 PM

I think you're over-complicating a relatively simple problem. As SquidDev requested, please post the code you tried with the parallel API - there are reasons why it didn't work for you, but we can't suggest a fix until we see exactly what you tried.

Edited by Dog, 24 July 2019 - 01:12 PM.


#5 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 24 July 2019 - 02:51 PM

As per request here's the pastebin code, well documented and as informing as possable.
https://pastebin.com/Yer4nb4c

Man all i wanted is a text input field while pulling mouse_click events, and now look where we all are.

My greatest apologies and regards,
-qwerty

#6 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 24 July 2019 - 03:59 PM

Dis regard this, thought the previous didn't post.

With regards,
-qwerty

Edited by qwerty, 24 July 2019 - 04:02 PM.


#7 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 24 July 2019 - 04:02 PM

I don't see where you tried to use the parallel API, but it's relatively simple to implement with what you've got. First, you need to encapsulate your input routines in functions then use the parallel API to execute both input routines. Something like this...

local function mouseInput()
  while true do --# start an infinite loop
	local _, button, x, y = os.pullEvent("mouse_click")
	if x == 2 and y == 2 then
	  sOut = not out
	end
	rs.setOutput("bottom", sOut)
  end
end

local function exitInput()
  while true do --# start an infinite loop
	term.setCursorPos(1, h - 2)
	term.clearLine()
	term.setCursorPos(1, h - 1)
	term.write("Type exit() to exit")
	term.setCursorPos(1, h)
	if read() == "exit()" then break end --# break the loop when exit() is entered
  end
end

parallel.waitForAny(mouseInput, exitInput)

Edited by Dog, 24 July 2019 - 04:07 PM.


#8 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 24 July 2019 - 04:08 PM

Thank you kind sir! I am genuinely ashamed i hadn't even atempted to turn them both into functions...
But i digress, without your help i may still be as confused as ever. Thank you for your help and keep up the good work!

With regards,
-qwerty

#9 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 25 July 2019 - 01:46 AM

Well yo jave helped me greatly, but unfortunately in my instance it still is ineffective as it does not Allow me to activate the button while reading.
That said, thank you for your contribution to my cause.
Further help apreciated but not necessary.

With regards,
-qwerty

Edited by qwerty, 25 July 2019 - 02:17 AM.


#10 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 25 July 2019 - 03:30 PM

Please post your code...

#11 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 25 July 2019 - 04:48 PM

well if you insist...

term.clear()
term.setCursorPos(1,1)
local oldBgC = term.getBackgroundColor()
local BgC = colors.red
local sOut = false
local w,h = term.getSize()
--defining button color
local out = rs.getOutput("bottom")
if out then
BgC = colors.green
else
BgC = colors.red
end
--button "write"
term.setCursorPos(1,1)
term.clearLine()
term.setCursorPos(2,2)
term.setBackgroundColor(BgC)
term.write(" ")
term.setBackgroundColor(oldBgC)
--button "handeler"
local function btev()
while true do
  local event, mb, x, y = os.pullEvent("mouse_click")
   if x == 2 and y == 2 then
   sOut = not out
  end
   rs.setOutput("bottom",sOut)
end
end
--exit "handeler"
local function exit()
while true do
  term.setCursorPos(1,h-2)
  term.clearLine()
  term.setCursorPos(1,h-1)
  term.write('type "exit()" to exit')
  term.setCursorPos(1,h)
  if read() == "exit()" then break end
end
end
parallel.waitForAny(btev, exit)

expectations: button yealds untill exit yelds then code stops
vs,
reality: button yealds once then waits for exit to yeald

thats as simple as i can explain it, but if you wish not to reply i'mfine with that.

With regards,
-qwerty.

#12 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 25 July 2019 - 06:32 PM

OK, right now the code will exit if you type exit(), but will continue to run and allow you to click the button as often as you like without exiting. How is that different than what you want?

In regard to the button not changing the output more than once - that's because you only ever set 'out' once in the code and never set it again - and you base 'sOut' on 'out' - so neither value changes. I'd recommend making a simple change to your code, like so...

Change this...
  sOut = not out

to this...
rs.setOutput("bottom", not rs.getOutput("bottom"))

and remove...
rs.setOutput("bottom", not sOut)


#13 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 25 July 2019 - 08:49 PM

My nogin's been jogin all day long and i hadn't even considered that...
I'm sorry for your hassles and forever in your debt.
Thank you for your help.

With regards and utmost gratitude,
-qwerty





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users