The Most Efficient Way to Create a Color Map

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MadMelon999

This Q&A thread is more of a solution than a question, but basically, I was trying to find out the most efficient ways to setup and render a Color Map (a bunch of Color_Rects in a GridContainer). Here are some important links to my findings.

Setup, which references “setup()” in Grid Renderer.gd: There are two main ways to set up the color grid

Create a bunch of ColorRect Objs and set them up in a double for loop

var obj
for y in GRID_DIMS.y:
	for x in GRID_DIMS.x:
		obj = ColorRect.new()
		obj.rect_min_size = COLOR_RECT_SIZE*Vector2(1,1)
		obj.color = # Input color Here
		grid.add_child(obj)

or duplicate a ColorRect that already has some basic data set up for it (rect_min_size and other features you may want to add)

var obj_scene = ColorRect.new()
obj_scene.rect_min_size = COLOR_RECT_SIZE*Vector2(1,1)
	
var obj
for y in GRID_DIMS.y:
	for x in GRID_DIMS.x:
		obj = obj_scene.duplicate()
		obj.color = # Input Color Here
		grid.add_child(obj)

According to the Spreadsheet, you should NOT use the .duplicate method (the second one), as it is 75% less efficient than the first method.

.

Render, which references “render_test()” in Grid Renderer.gd: There are 2 things to take into account when rendering a color_map

Firstly, if you have multiple modes for rendering, will you use a method like

if mode == COLOR_SCHEMES.X:
	for child in grid.get_children():
		## Code
elif mode == COLOR_SCHEMES.Y:
	for child in grid.get_children():
		## Code
elif mode == COLOR_SCHEMES.XY:
	for child in grid.get_children():
		## Code
elif mode == COLOR_SCHEMES.Simplex:
	for child in grid.get_children():
		## Code

or

for child in grid.get_children():
	if mode == COLOR_SCHEMES.X:
		## Code
	elif mode == COLOR_SCHEMES.Y:
		## Code
	elif mode == COLOR_SCHEMES.XY:
		## Code
	elif mode == COLOR_SCHEMES.Simplex:
		## Code

According to the data in the spreadsheet, it is more efficient (15% more efficient) to do an “if” and then a “for” order of operations (so the first option). An added benefit to this is that you can setup custom variables for these modes and you can specify which ColorRect cells need to be changed.

.

Secondly, when should you change the color of the ColorRect? Is it better to do it for every for loop recursion, or save each new color in an array and do it afterwords?

According to the data in the spreadsheet, it is more efficient (13% more efficient) to render the ColorRect in the “for loop” instead of rendering the ColorRect later by referencing an array.

.

Threads, which references “_on_Action_pressed()” in Grid Renderer.gd:
Threads should NOT BE USED for SETUP, as, for some reason, they are 98% less efficient than non-threaded versions. They also sometimes cause errors and some Color_Rects do not get rendered

Threads can be used for RENDER, although they are less efficient (between 34% and 50% less efficient). These should be avoided if possible, but because the RENDER time is so low, a loss of 50% efficiency only adds about a fraction of a second instead of multiple seconds.

.

Tell me what you think in the comments, as well as any other important methods I glanced over or problems using the project. Thank you!