Posted 28 May 2014 - 04:47 AM
Hello There!
I'm writing my first real, actually-does-stuff lua script (with perhaps too ambitious a goal), after almost 5 straight days of hardcore self-teaching; but I'm having an issue with what (I think) is the very last hurdle before Periscope is basically ready.
I'm attempting to make a script that will automatically detect all connected peripherals (through modem or no) and (based on task chosen) either display all detected peripherals(this part works) OR display one, and prompt the user for a variable name for the script to wrap that peripheral to; lather, rinse, repeat until all are wrapped…(this part doesn't quite work.)
Current Features:
“skip peripheral” function
very simple(and currently somewhat ineffective, for some reason) error detection
Two Modes:
display all detected
wrap detected one by one
Planned Features:(if I can get it working as is, lol)
already wrapped peripheral detection
cleaner instructional output (the print() statements everywhere aren't cutting it)
Symptoms:
everything works without even a single detected error, but when I go to the lua prompt to double-check if the entered variable names were wrapped properly they return nil values…
My Theory:
after some mindboogling, I think that the “name” arg to the “packagecandy” function (named as such because it “wraps”[c what i did thar? :P/> ]) isn't expanding to become the variable value, which should then be defined to [peripheral.wrap()]'s output; instead, it's staying as the literal string ”name”, which the peripherals are all getting wrapped to in turn…
Question:
how fix? That variable (should) contain the user's inputted wrapper name, but if it won't expand then how else can i get that input to become the peripheral.wrap() variable name? i've tried a few methods(commented in the code below), but lua complains about unexpected symbols…
All help offered is greatly appreciated; i'm quite proud of my progress so far, but now i'd just like for it to actually work! :D/>
So, without further ado, I present the mostly completed, yet thoroughly broken… Periscope!
Code So Far:
(p.s all my attempts to fix this issue are currently commented; as such, from lua's standpoint, the packagecandy function is currently empty… just a heads up for you, if you decide to test the code yourself.)
(p.p.s. please forgive the funky comment/code formatting; the code tags themselves are what's funkifying it tbh, as it's all perfectly lined up in both zerobrane, notepad++, and it even funkified when i manually lined it up in the posting WYSIWYG mods, feel free to edit that to fix it, as I've been trying for the past half hour using many combos of whitespace characters, with only what you see now to show for it.)
Thanks all!
I'm writing my first real, actually-does-stuff lua script (with perhaps too ambitious a goal), after almost 5 straight days of hardcore self-teaching; but I'm having an issue with what (I think) is the very last hurdle before Periscope is basically ready.
I'm attempting to make a script that will automatically detect all connected peripherals (through modem or no) and (based on task chosen) either display all detected peripherals(this part works) OR display one, and prompt the user for a variable name for the script to wrap that peripheral to; lather, rinse, repeat until all are wrapped…(this part doesn't quite work.)
Current Features:
“skip peripheral” function
very simple(and currently somewhat ineffective, for some reason) error detection
Two Modes:
display all detected
wrap detected one by one
Planned Features:(if I can get it working as is, lol)
already wrapped peripheral detection
cleaner instructional output (the print() statements everywhere aren't cutting it)
Symptoms:
everything works without even a single detected error, but when I go to the lua prompt to double-check if the entered variable names were wrapped properly they return nil values…
My Theory:
after some mindboogling, I think that the “name” arg to the “packagecandy” function (named as such because it “wraps”[c what i did thar? :P/> ]) isn't expanding to become the variable value, which should then be defined to [peripheral.wrap()]'s output; instead, it's staying as the literal string ”name”, which the peripherals are all getting wrapped to in turn…
Question:
how fix? That variable (should) contain the user's inputted wrapper name, but if it won't expand then how else can i get that input to become the peripheral.wrap() variable name? i've tried a few methods(commented in the code below), but lua complains about unexpected symbols…
All help offered is greatly appreciated; i'm quite proud of my progress so far, but now i'd just like for it to actually work! :D/>
So, without further ado, I present the mostly completed, yet thoroughly broken… Periscope!
Code So Far:
(p.s all my attempts to fix this issue are currently commented; as such, from lua's standpoint, the packagecandy function is currently empty… just a heads up for you, if you decide to test the code yourself.)
(p.p.s. please forgive the funky comment/code formatting; the code tags themselves are what's funkifying it tbh, as it's all perfectly lined up in both zerobrane, notepad++, and it even funkified when i manually lined it up in the posting WYSIWYG mods, feel free to edit that to fix it, as I've been trying for the past half hour using many combos of whitespace characters, with only what you see now to show for it.)
local tbl=peripheral.getNames() --detects peripherals
--local tbl ={"left", "logistics", "blokbreak", "right"} : zerobrane test code, ignore
function packagecandy(name, periph) -- function to wrap peripherals, called later on in script
--name=peripheral.wrap('"'..periph..'"') -- attempt 1
--tostring(name)=peripheral.wrap('"'..periph..'"') -- attempt 2
--string.format(name)=peripheral.wrap('"'..periph..'"') -- attempt 3
--print(name .." : ".. periph) -- more zerobrane remnants, ignore
end
print("enter task to run")
print("valid tasks are: display, wrap")
print("input now")
local task=io.read() --allows user to select function to complete
if task == "display" then --this section reads what user wants, and does it ...
print("printing all detected peripherals to screen, in order of table key")
local display="true"
elseif task=="wrap" then
print("beginning wrapping process")
print("enter name for displayed peripheral, or \"skip\" to skip that peripheral")
else
error("invalid task, please try again") -- or errors out if user uncooperative
end
for k,v in ipairs(tbl) do --parses peripheral table(not recursive since the tbl retreival never returns a nested table anyway[to my knowledge])
if display=="true" then
print(k ..": ".. v) --displays table if that option was selected
else -- or begins wrapping process otherwise
print("now wrapping peripheral : ".. v)
print("designate wrapper, or skip")
local wrapper = io.read() --user inputs wanted wrapper name, or "skip" to skip the listed peripheral
if wrapper == "skip" then --"skip" detection
print("skipping peripheral : ".. v)
else --calls wrapper function which wraps the listed peripheral name to the entered wrapper name
print("wrapping peripheral : " ..v .. " : TO : ".. wrapper)
local success=pcall(packagecandy, wrapper, v)
if not success then --simple error detection
error("wrapping failed! unknown reasons... try again?")
else --if no error detected, tell user that it went well!
print("peripheral : " ..v .." : successfully wrapped TO : ".. wrapper)
end
end
end
end
print("complete!") -- all done!
Thanks all!