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

[question][lua]

Started by Ghozer, 06 June 2012 - 06:45 AM
Ghozer #1
Posted 06 June 2012 - 08:45 AM
Hey, i'm new to ComputerCraft and know a bit of LUA already so i'm not totally 'unknowing', however, i'm just wondering a couple of things..

I know it's possible to send an RS signal in and trigger an event, some text, or something similar, and I know it's possible to send an RS signal out too, to control doors or machines etc..

however, i'm wondering if I could use a pressure plate setup with RS signal input to control the cursor on screen (or even for example the snake game that comes with CC as standard)

I have an idea for a large project and want to find out if it's possible, and how to do it before I start work (as I don't want to get so far in and then realise it's not possible)

so, could I take an RS signal, and trigger a LEFTCURSOR event for example..
if so, what internal function would I use for it?

Thanks!
Cloudy #2
Posted 06 June 2012 - 09:35 AM
Well what I would do is use the parallel API to run the program and your own code which checks for redstone events, and when it finds the correct one, it does a os.queueEvent with the relevant params.
kazagistar #3
Posted 06 June 2012 - 06:06 PM
Here is a link to to some events.
Ghozer #4
Posted 07 June 2012 - 09:13 AM
That looks like what i'm after…

Is there any info on os.queueEvent anywhere? there's nothing on the wiki that I can see! :S
OmegaVest #5
Posted 07 June 2012 - 03:46 PM
queueEvent is to create your own events.

Queue, then pull, and it should find your event. Good for stopping parallels, I guess. Not worked with that to any great extent.
Cloudy #6
Posted 07 June 2012 - 04:06 PM
The syntax is pretty simple - you'd do it like this:

os.queueEvent("key", 123)
os.queueEvent("char", "A")

Obviously replace that with the real params for left or right arrows.
Ghozer #7
Posted 05 July 2012 - 06:17 AM
i'm struggling a little with this…

here's what I have up to now…

local function game()
term.redirect(peripheral.wrap("right"))
os.run({}, "rom/programs/computer/worm")
end
local function pads()
if rs.testBundledInput("front", 16) then
  os.queueEvent("key", 200)
elseif rs.testBundledInput("front", 1) then
  os.queueEvent("key", 203)
elseif rs.testBundledInput("front", 2048) then
  os.queueEvent("key", 205)
elseif rs.testBundledInput("front", 16384) then
  os.queueEvent("key", 208)
end
os.pullEvent("key")
end
while true do
parallel.waitForAny(game, pads)
end

It starts the 'worm' game, but no input works from the bunbled inputs…

it's probably something totally silly that I have missed, but any help is appreciated…
Lyqyd #8
Posted 05 July 2012 - 06:35 AM
You're pulling the event right away after queuing it, so the game never sees the event. Remove the os.pullEvent("key") from the input function.
Ghozer #9
Posted 05 July 2012 - 07:31 AM
Ok, that worked… however, it accepts the input now, but it keeps reverting back..

(I have it set up with 4 pressure plates, when I step on YELLOW (number 16, for UP) it moves the option UP on screen, but as-soon as I step off the pressure plate, it moves back down, so it's not saving the events (or it's re-running the function all the time, and thus resetting it)

how can I get it to run worm only once, then run the 'pads' function over and over to check the inputs
Deathknight0897 #10
Posted 05 July 2012 - 07:36 AM
I presume you are making a game console fundamentally that's kinda clever i had same idea a while back but there isnt many games what you overall project if you don't mind me asking
try a repeat just a guess as a little confused about your physical set up

repeat
	-- put code for controller here
until a -- 1
Deathknight0897 #11
Posted 05 July 2012 - 07:41 AM
Oh btw please pm me if you get the keys functioning i may have a crack at my old code and see how it goes
Ghozer #12
Posted 05 July 2012 - 07:47 AM
The 'keys' are functioning, but the problem seems to be parallels is re-executing the game function each time,

so each tine I step on a key, it moves the cursor, but then on stepping off, it resets, as it's reloading the game…

I tried setting a variable to check if the game is running, and just skipping re-loading the game, however that stops the input from working…

**scratches head**
Ghozer #13
Posted 05 July 2012 - 07:52 AM
hehe, actually… done it, all sorted and working :P/>/>

I just added a repeat until like you suggested, but the 'until' condition will never be met….

i'll fix it later so a user has to press a button to end and exit the game which will then close it, but for now it works :)/>/>
Deathknight0897 #14
Posted 05 July 2012 - 08:02 AM
may i see ur code :P/>/> as my keys i never got working loops are fantastic aren't they abuse at will the loop (not to be read into) pm me if u don't want everyone seeing the code
MysticT #15
Posted 05 July 2012 - 03:58 PM
The problem was that the pad function needed to be in a loop. Also, it would be better to wait for a change in redstone instead of checking all the time.
This should work:

local function game()
  term.redirect(peripheral.wrap("right"))
  os.run({}, "rom/programs/computer/worm")
end

local function pads()
  while true do
    os.pullEvent("redstone") -- wait for redstone input
    if rs.testBundledInput("front", 16) then
	  os.queueEvent("key", 200)
    elseif rs.testBundledInput("front", 1) then
	  os.queueEvent("key", 203)
    elseif rs.testBundledInput("front", 2048) then
	  os.queueEvent("key", 205)
    elseif rs.testBundledInput("front", 16384) then
	  os.queueEvent("key", 208)
  end
end

parallel.waitForAny(game, pads)
Deathknight0897 #16
Posted 05 July 2012 - 05:39 PM
we already solved by using
repeat
until a – 1

and why ur version is better it wont be as responsive surely as checking as often as the program does
The problem was that the pad function needed to be in a loop. Also, it would be better to wait for a change in redstone instead of checking all the time.
This should work:

local function game()
  term.redirect(peripheral.wrap("right"))
  os.run({}, "rom/programs/computer/worm")
end

local function pads()
  while true do
	os.pullEvent("redstone") -- wait for redstone input
	if rs.testBundledInput("front", 16) then
	  os.queueEvent("key", 200)
	elseif rs.testBundledInput("front", 1) then
	  os.queueEvent("key", 203)
	elseif rs.testBundledInput("front", 2048) then
	  os.queueEvent("key", 205)
	elseif rs.testBundledInput("front", 16384) then
	  os.queueEvent("key", 208)
  end
end

parallel.waitForAny(game, pads)
Ghozer #17
Posted 05 July 2012 - 05:42 PM
The problem was that the pad function needed to be in a loop. Also, it would be better to wait for a change in redstone instead of checking all the time.
This should work:

local function game()
  term.redirect(peripheral.wrap("right"))
  os.run({}, "rom/programs/computer/worm")
end

local function pads()
  while true do
	os.pullEvent("redstone") -- wait for redstone input
	if rs.testBundledInput("front", 16) then
	  os.queueEvent("key", 200)
	elseif rs.testBundledInput("front", 1) then
	  os.queueEvent("key", 203)
	elseif rs.testBundledInput("front", 2048) then
	  os.queueEvent("key", 205)
	elseif rs.testBundledInput("front", 16384) then
	  os.queueEvent("key", 208)
  end
end

parallel.waitForAny(game, pads)

Yeah, after getting it fixed with repeat until last night, I had another look over it earlier and realised that a while true would have been better so actually made this change myself before reading your reply :P/>/>
MysticT #18
Posted 05 July 2012 - 05:47 PM
It doesn't really matter wich loop you use (altough while is normally used for infinite loops). The important thing is to have os.pullEvent("redstone"), since it will make the program just send the input when there's a change in redstone, so it doesn't loop all the time consuming you cpu time. If you want it to be able to stand on a pressure plate and have it send repeated key presses, then you should use a timer/sleep, like this:

local function pad()
  while true do
    -- check all the inputs
    sleep(0.5) -- you can change the time, so it checks more often
  end
end
Deathknight0897 #19
Posted 05 July 2012 - 06:03 PM
no u have misunderstood im not asking you that my point was which one is more responsive as a loop
just checking through a sleep like he did or this os.pull pullEvent()
MysticT #20
Posted 05 July 2012 - 06:14 PM
If you use os.pullEvent("redstone"), it will wait for a change in redstone input to send the "key" events. If you want to press a button, pressure plate, etc. and count it as a single key press, it's better to use it. If you want to keep sending key presses while the redstone input is on, use sleep, cause if you don't add some function that yields (like os.pullEvent and sleep), it will throw an error.
Ghozer #21
Posted 07 July 2012 - 01:36 PM
It doesn't really matter wich loop you use (altough while is normally used for infinite loops). The important thing is to have os.pullEvent("redstone"), since it will make the program just send the input when there's a change in redstone, so it doesn't loop all the time consuming you cpu time. If you want it to be able to stand on a pressure plate and have it send repeated key presses, then you should use a timer/sleep, like this:

local function pad()
  while true do
	-- check all the inputs
	sleep(0.5) -- you can change the time, so it checks more often
  end
end

I did actually have it like this once, had a 0.2 sec timer, because without it, or any lower it flooded the parallel api and caused it to error out…

I have it working nice now though! :P/>/>