Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Jasems

MFFS Direction Keys

Question

Ok so I made a simple program to basically detect if a lever is pushed down to send a rednet color to move an MFFS platform.  I am brand new at lua and OpenComputers and would like to do make it so that something like event("char") == "w"  the miner will go up, if it detects an "s" then it will go down, or if you hit "x" it will stop the miner. 

 

 

It might be helpful to just start w/how to get it to detect a keypress to just stop the program. I am slowly but surely understanding some of the terms on the wiki, but some simple examples would be a much better way to learn.  Thanks!

 

 

Link to post
Share on other sites

6 answers to this question

Recommended Posts

  • 0

I dont know all the function names for the mffs miner but this pseudo code should be close enough for you to get the gist of  it.

local m = component["mffs_miner??"] -- mffs miner thingy here..

local keys = {
  -- w is the key you want bound to this function
  w = function( ... )
    return m.forward() -- miner relevant direction per key press here
  end,
  s = function( ... )
    return m.backwards() -- same as above but for key 's'
  end,
  -- etc...
}

while true do
  local event = {event.pull()}
    if event[1] == "key_down" and keys[event[2]] then
       -- if key press and key is a registered key bind then
       keys[event[2]]()
    end
end
Link to post
Share on other sites
  • 0

I tend to use a slightly different method but the above example is perfectly fine, I'll post mine in case you find it easier and partly to show my favourite thing about coding... Mainly that there is never just one right answer.

 

I do most of my processing in the loop so...

local event = require("event")
local keyboard = require("keyboard")

local running = true
while running do
    ev, p1, p2, p3, p4, p5 = event.pull(1, _, ev, p1, p2, p3, p4, p5)
    if ev == "key_down" then
        local char = keyboard.keys[p3]
        if char == "w" then
            --your move forward code block here
        elseif char == "s" then
            --your move backward code block here
        elseif char == "a" then
            --your move left code block here
        elseif char == "d" then
            --your move right code block here
        elseif char == "q" then
            running = false
        end
    elseif ev == "touch"
        --do other cool stuff
    end
end

While writing that I thought... What if it went forward like 10 if it received a W instead of a w that would be cool.  Then I thought why such an arbitrary number? why not have numerical inputs set the distance? Like this...

local event = require("event")
local keyboard = require("keyboard")
local distance = 1

local running = true
while running do
    ev, p1, p2, p3, p4, p5 = event.pull(1, _, ev, p1, p2, p3, p4, p5)
    if ev == "key_down" then
        local char = keyboard.keys[p3]
        if tonumber(char) then --Edited to include Sangars change... for ease of copy pasting.
            if char == 0 then
                distance = 10
            else
                distance = char
            end
        end
        if char == "w" then
            for i = 1, distance do
                --your move forward code block here
            end
        elseif char == "s" then
            --your move backward code block here
        elseif char == "a" then
            --your move left code block here
        elseif char == "d" then
            --your move right code block here
        elseif char == "q" then
            running = false
        end
    elseif ev == "touch"
        --do other cool stuff
    end
end

Haven't used tonumberx in OC yet so not 100% sure that it works but it's like tonumber except it returns boolean according to the Lua reference manual.

That modification should let you use the numbers 0-9 as 1-10, 0 being 10.

Hope this helps.

Link to post
Share on other sites
  • 0

Haven't used tonumberx in OC yet so not 100% sure that it works but it's like tonumber except it returns boolean according to the Lua reference manual.

Uhm, can you provide a link? I've never heard of 'tonumberx' (on the Lua side) before. There's a C API method like that, but...

Simply 'tonumber' will work there, too, though, since that returns 'nil' if it fails.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.