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

Menus, buttons and lua code + Bundled cable

Question

I don't think I under stand the lua rules. I have done programming in C#  and lua is nothing like c#.

http://pastebin.com/6D6wpqSB Save in your library folder as "screen.lua"

The above code is a library that I use for my menus and buttons. It does work. The problem is that I can't seem to get the code to work. ?. Look at my code below. and it should make some sense to you. The idea is to start and stop trains (railcarft) using signals from bundled cable. with menus and buttons. The idea is to update the menu as well as sending signals out and being able to to turn the signals off. I just don't think I under stand the rules for coding in lua. For a sample. Look at isRefueling. When this is turned on, It sends a signal to a switch motor then switches a train track. Sending the train to refuel. When you stop trains and they have been sitting for a while. and not knowing how much fuel is left. I thought this was a good idea.

Can some one help me code this.
 


    local scr = require("screen")
    local component = require("component")
    local colors = require("colors")
    local rs = component.redstone
    local event = require("event")
    local term = require("term")
    local sides = require("sides")

    local rs = component.redstone
    isRefueling = false
    isCityActive = false
    isTownActive = false

    
    local win = scr:newPane(win, 50, 25)
    term.clear()
    win:center()
    win:box(1, 1, 50, 25, 0x999999)
    win:centerText(1, 10, 50, 0x999999, 0x000000, "Trains Main Menu.")
    win:button("Start / Stop Town  "..tostring(IsTownActive), 19, 12, 25, 3, 0xFF0000, "town")
    win:button("Start / Stop City  "..tostring(isCityActive), 19, 14, 25, 3, 0xFF0000, "city")
    if isRefueling == false then
    win:button("Refuel On   "..tostring(isRefueling), 19, 16, 20, 3, 0xFF0000, "refueloff")
    elseif isRefueling then
    win:button("Refuel Off  "..tostring(isRefueling), 19, 16, 20, 3, 0xFF0000, "refuelon")
    end
    
    win:button("Exit me",19 , 22, 20, 3, 0xFF0000, "exit")
    win:render()


function startIt(mode, place)

    if place == "city" then

      if mode == "on" then
      rs.setBundledOutput(sides.north, colors.lime, 255) --Starts train for city
      rs.setBundledOutput(sides.north, colors.pink, 0) -- Turns Parks off
      isCityActive = true
      elseif mode == "off" then
      rs.setBundledOutput(sides.north, colors.pink, 255) -- Stops Train
      rs.setBundledOutput(sides.north, colors.lime, 0) -- Turn Start off
      isCityActive = false
      end

  elseif place == "town" then

      if mode == "on" then
	  rs.setBundledOutput(sides.north, colors.green, 255) -- Starts train for town
      rs.setBundledOutput(sides.north, colors.red, 0) -- turns Stop off 
      isTownActive = true      
      elseif mode == "off" then
	  rs.setBundledOutput(sides.north, colors.green, 0) -- Shuts train city off
      rs.setBundledOutput(sides.north, colors.red, 255) --  Stops Train
      isTownActive = true
      end
  end
end

function Refueling(mode)

  if mode == "on" then
  rs.setBundledOutput(sides.north, colors.blue, 255)
  isReFueling = true 
  elseif mode == "off" then
  rs.setBundledOutput(sides.north, colors.blue, 0)
  isRefueling = false
  end
end


  
local function run(ev, p1, p2, p3, p4, p5)
  if ev == "touch" then
        if win:clicked(p2, p3) then
            ret = win:buttonClicked(p2, p3)
            if ret == "town" then     
 
             
          
                      
                term.clear()
                win:render()
            elseif ret == "city" then
            
            if isCityActive == true then
            startIt("on","city")
            elseif isCityActive == false then
            startIt("off","city")
            end
            
            elseif ret == "refuelon" then
            Refueling("on")
            
            elseif ret == "refueloff" then
            Refueling("off")
                          
            elseif ret == "exit" then
                term.clear()
                running = false      
            end
        end
  
  elseif ev == "key_down" then
    term.clear()
    running = false

  end

end

running = true
repeat

    local ev, p1, p2, p3, p4, p5 = event.pull(.5, _, ev, p1, p2, p3, p4, p5)
    run(ev, p1, p2, p3, p4, p5)
  
until running == false
term.clear()

  
Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0

What I see is that you create one of two buttons at the beginning of the program depending on the value of isRefueling.

But it isn't updated when isRefueling is changed.

(Another error, Lua is case sensitive: IsTownActive and isTownActive are two different variables.)

But all in all that are just 'normal' bugs.

So, which problems do you exactly have? (Expected vs. observed behaviour, error messages etc.)

If that is specified more clearly it is easier to help you.

Link to post
Share on other sites
  • 0

Well it doesn't work, It's just that plan, If your saying that 2 values are not the same, Then I need to make some fixes in the code. Maybe that's why it doesn't work, I don't know.

 

UPDATE:

I fount the value on the button line is the only one that didn't match the others. How ever that's only for display. The idea is that this code should work and doesn't. When I click on a button,. It needs to update the display all so. So I can turn the value from true to false and back.The problem is to update the menu. or screen with the new value. So when I click in the button. The value in the display changes.

Link to post
Share on other sites
  • 0

Is there a reason why I'm not getting any help at all.

Yes.  Or, at least, why it's not coming quickly.  You're asking a question that takes a lot of time and effort to answer: to go through your custom API and figure out how it works, then apply that knowledge to your code, and debug it for you.

 

Make it easier for people to help you:  Narrow down the issue by debugging it yourself as much as you can. 

 

Try writing a very simple program, verify that it works, then slowly add functionality until something breaks.

 

Or, try walking through your program step-by-step in the lua console.  Open the lua console (type lua), and load your library with scr = require("screen").  You can do the same with your main script's functions, by moving them to another library that you can then require.  From there, play around with the functions by calling them with various parameters and checking the results, to see what doesn't work as expected.

 

Or, and this is my favorite method, create a quick debug function that simply writes whatever parameter you pass to it to a debug text file:

function db(text)
  local debugFile = io.open("debug.txt","a")
  debugFile:write(tostring(text))
  debugFile:close()
end

Call "db(text)" throughout your program.  Use it to check the values of variables and function results at specific points ...

db("Starting Button Click, ret = " .. tostring(ret) .. ", isCityOn = " .. tostring(isCityOn))

... or, pepper it throughout your program to create a series of "checkpoints" that you can then use to figure out if, when, and in what order things are happening (e.g. db("Starting Draw") at the beginning of the draw function that isn't working, to see if the function is actually being called when it's supposed to, or if the function is misbehaving).

 

If you can ask for help in a way that doesn't require learning how your whole program works, you'll get that help faster.

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.