colorOptions = {colors.red, colors.white, colors.blue, colors.green, colors.orange}
function dPixed(x,y)
local lastX, lastY = term.getCursorPos()
term.setBackgroundColor(colorOptions[math.random(1,#colorOptions])
term.setCursorPos(x,y)
write(" ")
term.setCursorPos(lastX,lastY)
end
while true do
type, btn, x, y = os.pullEvent("mouse_click")
dPixel(x,y)
end
If you didn't get that, then here's an explanation.
Spoiler
As you may know, you can use os.startTimer(paramTime) to queue a timed event. What you may not know is: it returns a unique timer ID.
With this id and a little ingenuity, you can make it only call a function when the last timer is called. EX:
--CurrentTimer
cT = 0 --Init the var (so we don't crash)
--Let us draw text at mouse's last pos
lastX = 0
lastY = 0
while true do
type, v1, v2, v3 = os.pullEvent()
if (type=="timer" and v1==cT) then
term.setCursorPos(lastX,lastY)
print("X")
term.setCursorPos(0,0)
end
if (type=="mouse_click") then
cT = os.startTimer(2)
lastX = v2
lastY = v3
end
end
If you need an explanation:
Spoiler
I hope you're following me, this is my first tutorial in awhile
Now, to make it a drag function.
Initialize the currentTimer as 0 so we don't get errors.
Initialize drag so we don't get errors.
cTimer = 0 drag = false
Let's make a function that runs when we perform a "proper drag"
function myDragFunc(x,y)
term.setCursorPos(x,y)
write("O")
end
Next, I'll start our loop
while true do --In the loop endEverything from here on out is inside that loop.
Get input
-- Event type, argument 1, argument 2, argument 3 = os.pullEvent() type,v1,v2,v3 = os.pullEvent()
Let's see if the timer went off.
If it did, let's tell the program that we can no longer drag, and tell the same to the user.
if (type=="timer" and v1==cTimer) then
term.setCursorPos(1,1)
write("dragEnd")
drag=false
end
Now let's see if the input is a mouse_drag.
if (type=="mouse_drag") then end
Everything from here on out will be inside the if.
See if it's where a drag can start. (In this example, a box starting at 5,5 and ending at 15,10)
if (v2>4 and v2<16 and v3>4 and v3<11) then
drag = true
cTimer = os.startTimer(1)
myDragFunc(v2,v3)
--Let's clear the "dragEnd" text
term.setCursorPos(1,1)
write(" ")
end
And finially let's put in code for when drag==true.
if (drag) then cTimer = os.startTimer(1) myDragFunc(v2,v3) end
All assembled, that would be:
Spoiler
Hopefully, you learned something.
If you use this knowledge in any way, I ask that you (not require) post what you made in my topic. I love seeing creativity in action!











