Posted 16 August 2014 - 02:46 PM
I wrote a script to interact with Enhanced Portals 3 through touch screen monitors to simulate elevators. It all works beautifully.
However I predict that I might want to use the code multiple places and I have been sharing the code as well. To that end I would greatly prefer that I could add the details(text for the touchscreen buttons) through a separate piece of code and then refer to the same main bit of coding(so that I only have to manage/update one main program). What I came up with was this:
Unique startercode that can be customized for each individual use:
Core program:
Now in theory this works, but whenever there is a space in the button text, it is treated as separate arguments (http://computercraft.../wiki/Shell.run)
This means that "Floor 1" becomes "Floor" and "1".
Is there anything I can do to prevent/circumvent this?
However I predict that I might want to use the code multiple places and I have been sharing the code as well. To that end I would greatly prefer that I could add the details(text for the touchscreen buttons) through a separate piece of code and then refer to the same main bit of coding(so that I only have to manage/update one main program). What I came up with was this:
Unique startercode that can be customized for each individual use:
local buttontext = {}
buttontext[1]="Floor 1 "
buttontext[2]="Floor 2 "
buttontext[3]="Floor 3 "
buttontext[4]="Floor 4 "
buttontext[5]="Floor 5 "
buttontext[6]="Floor 6 "
buttontext[7]="Floor 7 "
buttontext[8]="Floor 8 "
buttontext[9]="Floor 9 "
shell.run("rm", "elevator")
shell.run("pastebin", "get", "1M5sgXpT", "elevator")
shell.run("elevator", buttontext[1], buttontext[2], buttontext[3], buttontext[4], buttontext[5], buttontext[6], buttontext[7], buttontext[8], buttontext[9])
Core program:
local buttontext = {...}
--Load floor location from disk
local FloorFile = fs.open("disk/floor", "r")
if FloorFile then
ThisFloor = tonumber(FloorFile.readLine())
print("This floor: "..ThisFloor)
FloorFile.close()
else
print("No Floor File/disk found")
return
end
os.setComputerLabel("Elevator"..ThisFloor)
local buttoncolours={}
for k=1, #buttontext do
buttoncolours[k]=8192 --8192=green
end
buttoncolours[ThisFloor]=32 --32=lime
mouseWidth = 0
mouseHeight = 0
monitor = peripheral.wrap("bottom")
monitor.clear()
monitor.setCursorPos(1,1)
w,h=monitor.getSize()
print(w)
print(h)
--Draw buttons
for k=1, #buttontext do
monitor.setBackgroundColour((buttoncolours[k]))
monitor.setCursorPos(2,k*2)
monitor.write(buttontext[k])
monitor.setBackgroundColour((colours.black))
end --for
function checkClickPosition()
for k=1, #buttontext do
if mouseWidth > 1 and mouseWidth < 18 and mouseHeight == k*2 and k~=ThisFloor then
redstone.setOutput("top", true)
sleep(1)
redstone.setAnalogOutput("back", k)
sleep(2)
redstone.setOutput("top", false)
redstone.setAnalogOutput("back", 0)
end
end
end
repeat
event,p1,p2,p3 = os.pullEvent()
if event=="monitor_touch" then
mouseWidth = p2 -- sets mouseWidth
mouseHeight = p3 -- and mouseHeight
checkClickPosition() -- this runs our function
end
until event=="char" and p1==("x")
Now in theory this works, but whenever there is a space in the button text, it is treated as separate arguments (http://computercraft.../wiki/Shell.run)
This means that "Floor 1" becomes "Floor" and "1".
Is there anything I can do to prevent/circumvent this?