Posted 05 February 2018 - 12:51 AM
So, what the program is supposed to do is make a 2d "ripple" around wherever the user clicks. It was supposed to work this way:
- First for loop to track how many layers have been drawn
- Nested for loop to track which column is being drawn to
- Another nested for loop to track which row is being drawn to
- an if statement to check if the current position has been added to the 'ignore' list, which is supposed to track which points have been drawn to in order to not draw to them again. (I want a ripple, not a square that keeps getting bigger)
When I try to run it, though, I click an area on the screen (besides the "close" button in the bottom right), and suddenly lua crashes. (Edit: I tried again and lua didn't crash. But it still doesn't do what I expect ¯\_(ツ)_/¯) =(
So, my questions are
- WHY did itcrash not do what I expect?
- How can I prevent it if I can?
- And if I can't (or if there's a simpler way to do this), how else can I do what I'm trying to do?
Here's the code (I put it in a spoiler tag 'cus it's kinda long):
Here's the relevant code for mrtutils (a custom API used in the code above):
- First for loop to track how many layers have been drawn
- Nested for loop to track which column is being drawn to
- Another nested for loop to track which row is being drawn to
- an if statement to check if the current position has been added to the 'ignore' list, which is supposed to track which points have been drawn to in order to not draw to them again. (I want a ripple, not a square that keeps getting bigger)
When I try to run it, though, I click an area on the screen (besides the "close" button in the bottom right), and suddenly lua crashes. (Edit: I tried again and lua didn't crash. But it still doesn't do what I expect ¯\_(ツ)_/¯) =(
So, my questions are
- WHY did it
- How can I prevent it if I can?
- And if I can't (or if there's a simpler way to do this), how else can I do what I'm trying to do?
Here's the code (I put it in a spoiler tag 'cus it's kinda long):
Spoiler
tArgs = {...}
local clr = tArgs[1]
-- If the user didn't specify a color, set it to lightBlue!
if clr == nil then
clr = colors.lightBlue
end
-- Sets up an ignore list so the ripple is a bunch of rings, not squares
local ignore = {...}
term.clear()
local w,h = term.getSize()
term.setCursorPos(w-1,h-1)
term.setBackgroundColor(colors.red)
term.setTextColor(colors.black)
print("x")
-- Pulls a mouse click event.
local _, _, x, y = os.pullEvent("mouse_click")
local done = false
-- If the click was on the "x", done is set to true
if x == w-1 and y == h-1 then
done = true
end
-- if done wasn't true, continue.
if not done then
term.setBackgroundColor(clr)
mrtutils.writeAt(" ",x,y)
table.insert(ignore,{ x,y })
local x1,y1
-- Counts layers
for i=1,w/2 do
-- Counts columns in layers
for j=0,(i*2) do
-- Counts rows in columns
for k=0,(j*2) do
x1,y1 = x+(j-i),x+(k-j)
-- Checks for if the current x,y is in the ignore list
for l=1,#ignore do
if not x1 == ignore[l][1] and not y1 == ignore[l][2] then
mrtutils.writeAt(" ",x1,y1)
sleep(0.01)
-- Adds current x,y to the ignore list so it isn't drawn to again
table.insert(ignore,{ x1,y1 })
end
end
end
end
end
end
term.clear()
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
-- Writes at a specific location
function writeAt(str, x, y)
term.setCursorPos(x,y)
term.write(str)
end
Note: I'm using CraftOS 1.7Edited on 04 February 2018 - 11:55 PM