Dimencia

WaitForSong

Nov 2nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. -- Loops forever, plays song on rednet requests
  2. -- When finished, sends signal back to indicate
  3. -- Can't terminate a song...
  4. local function getVars(song)
  5. -- This expects us to be given a function name, which we tack .lua on the end of
  6. -- We then return an array containing each of the global var names
  7. local varNames = {}
  8. local int index = 1
  9. print("Opening " .. song .. ".lua")
  10. local reader = fs.open(song .. ".lua","r")
  11. while true do
  12. local line = reader.readLine()
  13. print("Reading line: " .. line)
  14. if string.find(line,"_G.",1,true) ~= 1 then -- No more vars, exit loop
  15. break
  16. end
  17. -- Otherwise we need to read the var...
  18. -- _G.AcousticGrandPiano0 = "Guitar"
  19. -- So skip to position 4
  20. local varName = string.sub(line,4,string.find(line," ")-1)
  21. -- We might need the value, but we'll see
  22. -- We can't guarantee it will be set before we get yielded to, can we?
  23. varNames[index] = varName
  24. index = index + 1
  25. end
  26. return varNames
  27. end
  28.  
  29. local currentsong = nil
  30.  
  31. local function startSong(name,id)
  32. shell.run(name)
  33. rednet.send(id,"done")
  34. end
  35.  
  36. local routine = coroutine.create(startSong)
  37. local lastEvent = nil
  38.  
  39. local playing = false
  40.  
  41. rednet.open("back")
  42. while true do
  43. event ={os.pullEvent()}
  44. if playing and event[1] == lastEvent then
  45. code, lastEvent = coroutine.resume(routine,unpack(event))
  46. elseif event[1] == "rednet_message" then
  47. local msg = event[3]
  48. if msg ~= nil then
  49. print(msg)
  50. if msg == "stop" then
  51. playing = false
  52. lastEvent = nil
  53. routine = coroutine.create(startSong)
  54. elseif string.find(msg,"changeinstrument") == 1 then
  55. -- format: changeinstrument:variableName:newValue
  56. local msg2 = string.sub(msg, string.find(msg,":")+1) -- variableName:newValue
  57. local varName = string.sub(msg2, 1, string.find(msg2,":")-1)
  58. local newValue = string.sub(msg2, string.find(msg2,":")+1)
  59. _G[varName] = newValue
  60. else
  61. -- Before we play the song, let's parse out all the instruments...
  62. -- We don't need to store their value, we can send that with _G[]
  63. -- Once we have these, we send them in a rednet message back so it can store them for UI purposes
  64. varNames = getVars(msg)
  65. print(textutils.serialize(varNames))
  66. currentsong = msg
  67. playing = true
  68. code, lastEvent = coroutine.resume(routine,currentsong, event[2])
  69. rednet.send(event[2],textutils.serialize(varNames))
  70. values = {}
  71. for i,name in pairs(varNames) do
  72. values[i] = _G[name]
  73. end
  74. rednet.send(event[2],textutils.serialize(values))
  75. end
  76. end
  77. end
  78. end
Add Comment
Please, Sign In to add comment