a few things could be done to make it a bit more compact and clean
local side = nil
for r,s in pairs(rs.getSides()) do
if peripheral.getType(s) == 'monitor' then
side = s
break
end
end
m = peripheral.wrap(side)
This can be changed to this
for r,s in pairs(rs.getSides()) do
if peripheral.getType(s) == 'monitor' then
m = peripheral.wrap(s)
break
end
end
No reason to do the wrapping outside the for loop.
This is more of a bad practice thing, but when you use a variable from a function outside a function, you should return it from the function instead of declaring it globally within the function.
function Slotter(xmin, xmax, ymin, ymax)
local number = math.random(1, 3)
local color = 0
Slot = number
if number == 1 then
color = 16384
elseif number == 2 then
color = 2048
elseif number == 3 then
color = 16
end
for i = xmin, xmax do
for o = ymin, ymax do
m.setCursorPos(i,o)
m.setBackgroundColor(color)
m.write(" ")
end
end
end
could instead be
function Slotter(xmin, xmax, ymin, ymax)
local number = math.random(1, 3)
local color = 0
if number == 1 then
color = 16384
elseif number == 2 then
color = 2048
elseif number == 3 then
color = 16
end
for i = xmin, xmax do
for o = ymin, ymax do
m.setCursorPos(i,o)
m.setBackgroundColor(color)
m.write(" ")
end
end
return number
end
Then when you call it, you simply assign the return value to slotter like so
print("SPIN")
Slotter3 = Slotter(2, 2, 2, 2)
print(Slotter3)
Slotter2 = Slotter(4, 4, 2, 2)
print(Slotter2)
Slotter1 = Slotter(6, 6, 2, 2)
print(Slotter1)
While we're at it, declaring anything with a capital first letter is not something you should do in LUA. The name for it right now escapes me, but the usual practice is to name things like so: variableName
functionName()
also, we could do this last bit with a for loop
print("SPIN")
for i=2,6,2 -- this loop will loop once where i = 2, then it will increase i by 2 and loop again, and keep doing this until i == 6
Slotter..(i/2) = Slotter(i, i, 2, 2)
print(Slotter..(i/2)
end
Edited - I've noticed that whenever i unload/load chunks, its a bit buggy? Is that just a server im playing on, or? It works, but i have to go into terminal everytime its to Spin?
I assume you mean you have to start the program over again? Whenever a computer is unloaded and reloaded it will restart. This means any program running will stop. only way around this is to name your program "startup" and place it in the root directory. The computer will run the startup file whenever it's rebooted.