Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Loops forever, plays song on rednet requests
- -- When finished, sends signal back to indicate
- -- Can't terminate a song...
- local function getVars(song)
- -- This expects us to be given a function name, which we tack .lua on the end of
- -- We then return an array containing each of the global var names
- local varNames = {}
- local int index = 1
- print("Opening " .. song .. ".lua")
- local reader = fs.open(song .. ".lua","r")
- while true do
- local line = reader.readLine()
- print("Reading line: " .. line)
- if string.find(line,"_G.",1,true) ~= 1 then -- No more vars, exit loop
- break
- end
- -- Otherwise we need to read the var...
- -- _G.AcousticGrandPiano0 = "Guitar"
- -- So skip to position 4
- local varName = string.sub(line,4,string.find(line," ")-1)
- -- We might need the value, but we'll see
- -- We can't guarantee it will be set before we get yielded to, can we?
- varNames[index] = varName
- index = index + 1
- end
- return varNames
- end
- local currentsong = nil
- local function startSong(name,id)
- shell.run(name)
- rednet.send(id,"done")
- end
- local routine = coroutine.create(startSong)
- local lastEvent = nil
- local playing = false
- rednet.open("back")
- while true do
- event ={os.pullEvent()}
- if playing and event[1] == lastEvent then
- code, lastEvent = coroutine.resume(routine,unpack(event))
- elseif event[1] == "rednet_message" then
- local msg = event[3]
- if msg ~= nil then
- print(msg)
- if msg == "stop" then
- playing = false
- lastEvent = nil
- routine = coroutine.create(startSong)
- elseif string.find(msg,"changeinstrument") == 1 then
- -- format: changeinstrument:variableName:newValue
- local msg2 = string.sub(msg, string.find(msg,":")+1) -- variableName:newValue
- local varName = string.sub(msg2, 1, string.find(msg2,":")-1)
- local newValue = string.sub(msg2, string.find(msg2,":")+1)
- _G[varName] = newValue
- else
- -- Before we play the song, let's parse out all the instruments...
- -- We don't need to store their value, we can send that with _G[]
- -- Once we have these, we send them in a rednet message back so it can store them for UI purposes
- varNames = getVars(msg)
- print(textutils.serialize(varNames))
- currentsong = msg
- playing = true
- code, lastEvent = coroutine.resume(routine,currentsong, event[2])
- rednet.send(event[2],textutils.serialize(varNames))
- values = {}
- for i,name in pairs(varNames) do
- values[i] = _G[name]
- end
- rednet.send(event[2],textutils.serialize(values))
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment