This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
billysback's profile picture

Memory for drawing tasks is messing up

Started by billysback, 08 June 2013 - 09:35 AM
billysback #1
Posted 08 June 2013 - 11:35 AM

local memory = {}
local nmem = {}
--[[
where:
  objs is a table containing a list of the objects, (objects consist of {x,y,[color]})
  i is the current iteration of the memory it is drawing,
  n is the maximum iteration of memory,
  cols is a table containing the colour objects will be drawn in at different iterations of the memory
]]
local function draw(objs, i, n, cols)
	if i == 0 then
		term.setBackgroundColor(colors.black)
		term.setTextColor(colors.white)
		term.clear()
	end
	for j=1,#objs do
		local obj = objs[j]
		local color = cols[i+1]
		if type(color) ~= "number" then color = colors.white end
		if obj[3] ~= nil then color = obj[3] end
		term.setCursorPos(obj[1], obj[2])
		term.setTextColor(colors.black)
		term.setBackgroundColor(color)
		term.write(" ")
	end
	term.setCursorPos(i+1,1)
	print(i)
	for i=1,#info do
		--print(info[i])
	end
	info = {}
	if i < n then
		local oclone = {}
		for j=1,#objs do oclone[j] = objs[j] end
		nmem[i+1] = oclone
		if memory[i+1] ~= nil then
			draw(memory[i+1], i+1, n, cols)
		else
			memory = nmem
		end
	else
		memory = nmem
	end
end

I'm inputting

draw(tab, 0, 2, {colors.white, colors.lightGray, colors.gray})
where tab is a table of objects to draw, this has no errors in it…

basically, it's just drawing the objects in the same positions regardless of what stage it is in the memory, so it results in having the standard objects without memory but in gray instead of white.

I'm completely stumped as to what I should do, so I'm asking the forums.
if anyone can help at all, it would seriously be appreciated.
Engineer #2
Posted 08 June 2013 - 12:09 PM
As I do not fully understand what you are doing here, you might want to consider some OOP:

local mainObject = {
   __index = mainObject;
   create = function()
	   local self = {
		  x = 1;
		  y = 3;
		  colour = colours.black;
		  setPos = function( self, newx, newy )
			  self.x = newx
			  self.y = newy
			  return self
		  end;
		  setColour = function( self, col )
			   self.colour = col
			   return self
		  end;
		  draw = function( self )
		           term.setBackgroundColour( self.colour )
			   term.setCursorPos( self.x, self.y )
			   --# drawing stuff
		  end;
	   }
	   return setmetatable( self, mainObject )
   end;
}

After that, you create an object like so:

local first = mainObject.create()
--# Call a function that you defined in the self table
first:setColour( 1 ):draw() --# You escape the self with a colon, and you can chain :D/>
local second = mainObject.create():setColour( 4 ):draw() -- Independant object

:)/>
billysback #3
Posted 08 June 2013 - 12:59 PM
OOP isn't really necessary for what I'm doing as the tables are very simple, also your code completely removes my Memory thing which I'm asking help with…

I'm not having a go, just making it clear what I'm asking help with, which is the idea that it remembers the last positions of the objects and draws them with different colours.
BigSHinyToys #4
Posted 08 June 2013 - 01:20 PM
If you could explain in more detail what this program is doing and what is should be doing that might be helpful.

looking through the code I can see the info table is declared after the for loop you want to print it with.
Engineer #5
Posted 08 June 2013 - 02:56 PM
OOP isn't really necessary for what I'm doing as the tables are very simple, also your code completely removes my Memory thing which I'm asking help with…

I'm not having a go, just making it clear what I'm asking help with, which is the idea that it remembers the last positions of the objects and draws them with different colours.
As I do not fully understand what you are doing here.
billysback #6
Posted 08 June 2013 - 03:39 PM
basically, this is iterating between a list of tables which contain the information {x-coordinate, y-coordinate, color} the color is optional and will instead just draw a fading colour instead

The memory table contains information on all of the previous draws, it stores the tables which the draw function iterates through to get the tables which contain the information, the objects.

It then iterates through the memory and calls the draw function for every table inside the memory table.

whats not working:
the objects in the memory are all going being drawn on the same x,y coordinates… basically.
Engineer #7
Posted 08 June 2013 - 04:00 PM
Hmm.. I do not know why you are making it so complicated. Modify this to your wishes, since I dont know what you are doing with the parameters.

--# table should look like so:
local objects = {
	{ x = 1; y = 1; colour = colours.blue };
	{ x = 3; y = 1; }
}

local draw = function( self, backupColour )
	for k, v in pairs( self ) do
		if self.x and self.y then
			term.setBackgroundColour( v.colour or (backupColour or colours.grey))
			term.setCursorPos( v.x, v.y )
			--# draw that object
		end
	end
end
Edited on 08 June 2013 - 02:01 PM
billysback #8
Posted 08 June 2013 - 04:08 PM
that's not the point…
the point is it remembers the last draw tasks and thus their colours…
Sorroko #9
Posted 08 June 2013 - 04:15 PM
Why not just maintain a list of objects to draw; in order to draw from the last frames, don't reset the table and just add more objects to it?
Use @Engineer's code and simply keep adding objects to the "objects" table and not removing them.
I might not be understanding properly, correct me if I'm wrong, a little more information on the use and purpose of the code might help.
Engineer #10
Posted 08 June 2013 - 04:16 PM
OH! Then I really recommend rewriting, sort of, 3 functions:

term.setBackgroundColour
term.setTextColour
term.write

After that you can create a table of the screen, and you are going to draw that. So to create that table:

local w, h = term.getSize()
for y = 1, h do
   screen[y] = {}
   for x = 1, w do
     screen[y][x] = {}
     screen[y][x].bg = colours.black
     screen[y][x].textcol = colours.white
     screen[y][x].char = " "
   end
end
Then you make override the earlier mentioned functions and when you write something, it overwrites the table in the correct spots with the correct colours and shizzle. After that, you can copy that specific table at some point and make a function table that draws that.
You can escape the overwriting thing with the term.native.<API CALL>. If you do this, I think you are pretty much good to go.
billysback #11
Posted 08 June 2013 - 04:35 PM
OH! Then I really recommend rewriting, sort of, 3 functions:

term.setBackgroundColour
term.setTextColour
term.write

After that you can create a table of the screen, and you are going to draw that. So to create that table:

local w, h = term.getSize()
for y = 1, h do
   screen[y] = {}
   for x = 1, w do
	 screen[y][x] = {}
	 screen[y][x].bg = colours.black
	 screen[y][x].textcol = colours.white
	 screen[y][x].char = " "
   end
end
Then you make override the earlier mentioned functions and when you write something, it overwrites the table in the correct spots with the correct colours and shizzle. After that, you can copy that specific table at some point and make a function table that draws that.
You can escape the overwriting thing with the term.native.<API CALL>. If you do this, I think you are pretty much good to go.
I still don't understand how that solves my problem at all.
I also really don't want to convert the format in which the x,y,color are in as this is not where it is broken…

I just want focus on the

        if i < n then
                local oclone = {}
                for j=1,#objs do oclone[j] = objs[j] end
                nmem[i+1] = oclone
                if memory[i+1] ~= nil then
                        draw(memory[i+1], i+1, n, cols)
                else
                        memory = nmem
                end
        else
                memory = nmem
        end


and possibly the

                local color = cols[i+1]
                if type(color) ~= "number" then color = colors.white end
                if obj[3] ~= nil then color = obj[3] end

bit,

without sounding ungrateful you keep on rewriting the bit that I'm fine with and works and missing out the bit which is broken.
I understand that it's kind of unclear but.. I don't really know how to be more clear.
Engineer #12
Posted 08 June 2013 - 04:38 PM
Then Im going to ask, where do the parameters i and n stand for? And how is the table with objects constructed?
billysback #13
Posted 08 June 2013 - 04:53 PM

--[[
where:
  objs is a table containing a list of the objects, (objects consist of {x,y,[color]})
  i is the current iteration of the memory it is drawing,
  n is the maximum iteration of memory,
  cols is a table containing the colour objects will be drawn in at different iterations of the memory
]]

this is in the original code…

the table objects is constructed as above, a table containing tables in the format {x, y, color} where the third (color) is optional
Orwell #14
Posted 08 June 2013 - 04:56 PM
Then Im going to ask, where do the parameters i and n stand for? And how is the table with objects constructed?
'i' is the current iteration of the objects in memory that's being drawn. 'n' is the total number of iterations. It's all in the OP. How the objects are being constructed doesn't matter. We now it are tables in the format of {x, y, } where color is optional.

I'm a bit confused though, each object is in the memory table, right? It's just that you call

draw(memory[i+1], i+1, n, cols)
But I have no clue whether the memory actually contains the objects? For now you just pass one list of objects ('objs') and then, if memory is indeed empty, you set 'memory = nmem' where 'nmem' only contains the current list of objects. Thus repeating the same objects over and over again. So if the memory table is indeed empty, that's what's happening. Maybe it's not, I can't tell from the code.

Edit: also, if memory indeed contains a list of object tables, why do you explicitely pass the obj into the function? Isn't the current interation enough?
billysback #15
Posted 08 June 2013 - 05:01 PM
the logic (I realized I need to reset nmem so edited the code) is the when draw() is called it then looks to see if there is any memory of previous calls of draw()
for each call of draw() in a loop it adds the current object table in to the nmem table (new memory), when the loop is forced to end the the current memory is replaced with the new memory table, which contains a updated memory. I'm really tired so I might not be making much sense, sorry.
Engineer #16
Posted 08 June 2013 - 05:13 PM
Then Im going to ask, where do the parameters i and n stand for? And how is the table with objects constructed?
'i' is the current iteration of the objects in memory that's being drawn. 'n' is the total number of iterations. It's all in the OP. How the objects are being constructed doesn't matter. We now it are tables in the format of {x, y, } where color is optional.
Im sorry, but it doesnt say it clearly in the OP. I know for a matter of fact that i is the standard variable that is used in a for-loop, but I didnt know it about n. However, people can intend to represent it something else, like "integer" or something like that.

Oops, I read over it… Wow, that is really embarrassing! My apologies!

And to be very honest, I think you should use another method to do this.
Multiple ways lead to Rome (its a dutch expression, not sure if it makes sense in English.)
Im currently doing some screen tracking and stuff, and the description I posted about it before will roughly describe it.(post #10)

Im giving up on this one, sorry.
Orwell #17
Posted 08 June 2013 - 05:23 PM
the logic (I realized I need to reset nmem so edited the code) is the when draw() is called it then looks to see if there is any memory of previous calls of draw()
for each call of draw() in a loop it adds the current object table in to the nmem table (new memory), when the loop is forced to end the the current memory is replaced with the new memory table, which contains a updated memory. I'm really tired so I might not be making much sense, sorry.
I see, so is the problem that all objects within the same 'objs' table are drawn on the same place? I understood the objects where drawn at their respective coordinates, but the same at each iteration.
Orwell #18
Posted 08 June 2013 - 07:24 PM
Isn't this simply working?

Code I ran:
Spoiler

local memory = {}
local nmem = {}
local info = {}
--[[
where:
  objs is a table containing a list of the objects, (objects consist of {x,y,[color]})
  i is the current iteration of the memory it is drawing,
  n is the maximum iteration of memory,
  cols is a table containing the colour objects will be drawn in at different iterations of the memory
]]
local function draw(objs, i, n, cols)
	    if i == 0 then
			    term.setBackgroundColor(colors.black)
			    term.setTextColor(colors.white)
			    term.clear()
	    end
	    for j=1,#objs do
			    local obj = objs[j]
			    local color = cols[i+1]
			    if type(color) ~= "number" then color = colors.white end
			    if obj[3] ~= nil then color = obj[3] end
			    term.setCursorPos(obj[1], obj[2])
			    term.setTextColor(colors.black)
			    term.setBackgroundColor(color)
			    term.write(" ")
	    end
	    term.setCursorPos(i+1,1)
	    print(i)
	    for i=1,#info do
			    --print(info[i])
	    end
	    info = {}
	    if i < n then
			    local oclone = {}
			    for j=1,#objs do oclone[j] = objs[j] end
			    nmem[i+1] = oclone
			    if memory[i+1] ~= nil then
					    draw(memory[i+1], i+1, n, cols)
			    else
					    memory = nmem
			    end
	    else
			    memory = nmem
	    end
end
local tab = {{5,5,colors.red}, {10,10,colors.blue}, {35,5,colors.green}}
local tab2 = {{10,5,colors.red}, {15,10,colors.blue}, {40,5,colors.green}}
draw(tab, 0, 2, {colors.white, colors.lightGray, colors.gray})
draw(tab2, 1, 2, {colors.white, colors.lightGray, colors.gray})

Output I got:
Spoiler
billysback #19
Posted 08 June 2013 - 07:31 PM
Leave the colours section blank, so

local tab = {{5,5}, {10,10}, {35,5}}
local tab2 = {{10,5}, {15,10}, {40,5}}

it's not supposed to do anything if you add your own colours in… :P/>

EDIT:
wait, it worked in this example.
ffs. Nevermind everyone who helped, sorry for wasting your time… thanks for pointing that out Orwell.
This code is fully functional. Dammit.
Orwell #20
Posted 08 June 2013 - 07:48 PM
Leave the colours section blank, so

local tab = {{5,5}, {10,10}, {35,5}}
local tab2 = {{10,5}, {15,10}, {40,5}}

it's not supposed to do anything if you add your own colours in… :P/>

EDIT:
wait, it worked in this example.
ffs. Nevermind everyone who helped, sorry for wasting your time… thanks for pointing that out Orwell.
This code is fully functional. Dammit.
Indeed, it's beautiful with the grayscale. How hilarious. :P/>