How to store a dictionary into an array?

Godot Version

4.2.1

Question

hello,
Is it possible to store this dictionary inside an array ?
Loop it throw the draw function, and use the array position has layers ?

var mouseGroup = {
	'Fnum': []
}

@export var canvasTex = {};

var layer = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

var layerNum: int = 0;




	
	if ( Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) && mouseJustPress == false ):
		mouseJustPress = true;
		
		if ( drawText == null ):
			return;
		
		mouseGroup.Fnum.clear();
		
		for i in range(0, drawText.get_image().get_size().x,32 ):
			for j in range(0,  drawText.get_image().get_size().y, 32):
				
				mouseGroup.Fnum.append(  { "cords": Vector2(i, j), "img": ImageTexture.create_from_image(drawText.get_image().get_region( Rect2( i, j, 32, 32) ) ) } );
		
 
	if ( Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) && mouseJustPress == true ):
		
		for i in range (0, mouseGroup.Fnum.size() ):
			canvasTex[ mGpos + mouseGroup.Fnum[i].cords ] = mouseGroup.Fnum[i].img;
		
		layer[layerNum] = canvasTex; #########################



func _draw():
	
	
	for lay in range (0, 5 ):
		
		var layx = layer[lay ];
		
		for i in canvasTex:
			draw_texture( canvasTex[i], i );

iam storing “mouseGroup.Fnum” inside “canvasTex”
The problem is when i try to store “canvasTex” inside an array “layer[layerNum]”

doing this doesnt seem to work:

	for lay in range (0, 5 ):
		var layx = layer[lay ];
		for i in canvasTex:
			draw_texture(   layx [     canvasTex[i], i     ]    );

this also doesnt work:
draw_texture( layer[lay].canvasTex[i], i );

is there a way to loop throw the layers array and draw all canvasTex in seperate layers ?

this is the stuff inside print(layer[layerNum]);

Godot Engine v4.2.1.stable.official (c) 2007-present Juan Linietsky, Ariel Manzur & Godot Contributors.
  modules/gltf/register_types.cpp:63 - Blend file import is enabled in the project settings, but no Blender path is configured in the editor settings. Blend files will not be imported.
--- Debug adapter server started ---
--- GDScript language server started on port 6005 ---



{ (-1312, 768): <ImageTexture#-9223370369149112502>, (-1280, 768): <ImageTexture#-9223370369098780852>, (-640, 2784): <ImageTexture#-9223370369048449202>, (224, 192): <ImageTexture#-9223370366179545152>, (224, 224): <ImageTexture#-9223370366129213502>, (224, 256): <ImageTexture#-9223370366078881852>, (224, 288): <ImageTexture#-9223370366028550202>, (224, 320): <ImageTexture#-9223370365978218552>, (224, 352): <ImageTexture#-9223370365927886902>, (224, 384): <ImageTexture#-9223370365877555252>, (256, 192): <ImageTexture#-9223370365827223602>, (256, 224): <ImageTexture#-9223370365776891952>, (256, 256): <ImageTexture#-9223370365726560302>, (256, 288): <ImageTexture#-9223370365676228652>, (256, 320): <ImageTexture#-9223370365625897002>, (256, 352): <ImageTexture#-9223370365575565352>, (256, 384): <ImageTexture#-9223370365525233702>, (288, 192): <ImageTexture#-9223370365474902052>, (288, 224): <ImageTexture#-9223370365424570402>, (288, 256): <ImageTexture#-9223370365374238752>, (288, 288): <ImageTexture#-9223370365323907102>, (288, 320): <ImageTexture#-9223370365273575452>, (288, 352): <ImageTexture#-9223370365223243802>, (288, 384): <ImageTexture#-9223370365172912152>, (320, 192): <ImageTexture#-9223370365122580502>, (320, 224): <ImageTexture#-9223370365072248852>, (320, 256): <ImageTexture#-9223370365021917202>, (320, 288): <ImageTexture#-9223370364971585552>, (320, 320): <ImageTexture#-9223370364921253902>, (320, 352): <ImageTexture#-9223370364870922252>, (320, 384): <ImageTexture#-9223370364820590602>, (352, 192): <ImageTexture#-9223370364770258952>, (352, 224): <ImageTexture#-9223370364719927302>, (352, 256): <ImageTexture#-9223370364669595652>, (352, 288): <ImageTexture#-9223370364619264002>, (352, 320): <ImageTexture#-9223370364568932352>, (352, 352): <ImageTexture#-9223370364518600702>, (352, 384): <ImageTexture#-9223370364468269052>, (384, 192): <ImageTexture#-9223370364417937402>, (384, 224): <ImageTexture#-9223370364367605752>, (384, 256): <ImageTexture#-9223370364317274102>, (384, 288): <ImageTexture#-9223370364266942452>, (384, 320): <ImageTexture#-9223370364216610802>, (384, 352): <ImageTexture#-9223370364166279152>, (384, 384): <ImageTexture#-9223370364115947502>, (576, 352): <ImageTexture#-9223369959751477351>, (576, 384): <ImageTexture#-9223369959701145702>, (576, 416): <ImageTexture#-9223369959650814053>, (576, 448): <ImageTexture#-9223369959600482404>, (576, 480): <ImageTexture#-9223369959550150755>, (576, 512): <ImageTexture#-9223369959499819106>, (576, 544): <ImageTexture#-9223370367991484552>, (608, 352): <ImageTexture#-9223369959751477351>, (608, 384): <ImageTexture#-9223369959701145702>, (608, 416): <ImageTexture#-9223369959650814053>, (608, 448): <ImageTexture#-9223369959600482404>, (608, 480): <ImageTexture#-9223369959550150755>, (608, 512): <ImageTexture#-9223369959499819106>, (608, 544): <ImageTexture#-9223370367639163002>, (640, 352): <ImageTexture#-9223369959751477351>, (640, 384): <ImageTexture#-9223369959701145702>, (640, 416): <ImageTexture#-9223369959650814053>, (640, 448): <ImageTexture#-9223369959600482404>, (640, 480): <ImageTexture#-9223369959550150755>, (640, 512): <ImageTexture#-9223369959499819106>, (640, 544): <ImageTexture#-9223370367286841452>, (672, 352): <ImageTexture#-9223369949215385844>, (672, 384): <ImageTexture#-9223369949215385844>, (672, 416): <ImageTexture#-9223369949165054177>, (672, 448): <ImageTexture#-9223369949114722460>, (672, 480): <ImageTexture#-9223369949064400978>, (672, 512): <ImageTexture#-9223369949014059221>, (672, 544): <ImageTexture#-9223369948963737686>, (704, 352): <ImageTexture#-9223369948863074392>, (704, 384): <ImageTexture#-9223369948863074392>, (704, 416): <ImageTexture#-9223369948812732632>, (704, 448): <ImageTexture#-9223369948762411098>, (704, 480): <ImageTexture#-9223369948712069337>, (704, 512): <ImageTexture#-9223369948661747804>, (704, 544): <ImageTexture#-9223369948611406042>, (736, 352): <ImageTexture#-9223369948510752836>, (736, 384): <ImageTexture#-9223369948510752836>, (736, 416): <ImageTexture#-9223369948460411086>, (736, 448): <ImageTexture#-9223369948410089544>, (736, 480): <ImageTexture#-9223369948359747792>, (736, 512): <ImageTexture#-9223369948309426250>, (736, 544): <ImageTexture#-9223369948259084497>, (1632, -320): <ImageTexture#-9223370368998117552>, (1664, -384): <ImageTexture#-9223370368998117552>, (1696, -416): <ImageTexture#-9223370368998117552>, (1728, -448): <ImageTexture#-9223370368998117552>, (1760, -448): <ImageTexture#-9223370368998117552>, (1792, -448): <ImageTexture#-9223370368998117552>, (1856, -576): <ImageTexture#-9223370368998117552>, (1856, -544): <ImageTexture#-9223370368998117552>, (1856, -512): <ImageTexture#-9223370368998117552>, (1856, -480): <ImageTexture#-9223370368998117552>, (1856, -416): <ImageTexture#-9223370368998117552>, (1856, -352): <ImageTexture#-9223370368998117552>, (2048, -544): <ImageTexture#-9223370368947785902>, (2112, -480): <ImageTexture#-9223370368947785902>, (2144, -1632): <ImageTexture#-9223370368897454252>, (2176, -416): <ImageTexture#-9223370368947785902>, (2752, -1696): <ImageTexture#-9223370368847122602>, (3200, -672): <ImageTexture#-9223370368796790952>, (3232, -640): <ImageTexture#-9223370368796790952>, (3264, -608): <ImageTexture#-9223370368796790952>, (3456, -480): <ImageTexture#-9223370368796790952>, (4800, -1728): <ImageTexture#-9223370368746459302>, (6624, -1536): <ImageTexture#-9223370368947785902>, (6848, -1568): <ImageTexture#-9223370368696127652>, (8512, -1984): <ImageTexture#-9223370368645796002>, (8544, -1984): <ImageTexture#-9223370368595464352>, (15264, 224): <ImageTexture#-9223370368545132702>, (15296, 224): <ImageTexture#-9223370368494801052>, (15328, 224): <ImageTexture#-9223370368444469402>, (15360, 224): <ImageTexture#-9223370368394137752>, (77696, -22464): <ImageTexture#-9223370368343806102>, (992, 320): <ImageTexture#-9223368509596034982>, (1024, 320): <ImageTexture#-9223368509294045088>, (1056, 320): <ImageTexture#-9223369957989869636>, (832, 160): <ImageTexture#-9223370097005881645>, (864, 160): <ImageTexture#-9223370096955550001>, (896, 160): <ImageTexture#-9223370096955550001>, (800, 160): <ImageTexture#-9223370097056213283>, (768, 192): <ImageTexture#-9223370097005881645>, (800, 192): <ImageTexture#-9223370096955550001>, (832, 192): <ImageTexture#-9223370096955550001>, (736, 192): <ImageTexture#-9223370097056213283>, (736, 224): <ImageTexture#-9223370097005881645>, (768, 224): <ImageTexture#-9223370096955550001>, (800, 224): <ImageTexture#-9223370096955550001>, (704, 224): <ImageTexture#-9223370097056213283>, (704, 256): <ImageTexture#-9223370097056213283>, (736, 256): <ImageTexture#-9223370097056213283>, (768, 256): <ImageTexture#-9223370097005881645>, (800, 256): <ImageTexture#-9223370096955550001>, (736, 288): <ImageTexture#-9223370097056213283>, (768, 288): <ImageTexture#-9223370097056213283>, (800, 288): <ImageTexture#-9223370097005881645>, (832, 288): <ImageTexture#-9223370096955550001>, (800, 320): <ImageTexture#-9223369959801809046>, (832, 320): <ImageTexture#-9223369959801809046>, (864, 320): <ImageTexture#-9223368510502004706>, (896, 320): <ImageTexture#-9223368510502004706>, (896, 352): <ImageTexture#-9223368510451673015>, (928, 352): <ImageTexture#-9223368510502004706>, (960, 352): <ImageTexture#-9223368510200014770>, (992, 352): <ImageTexture#-9223368509898024876>, (1024, 352): <ImageTexture#-9223368509596034982>, (1056, 352): <ImageTexture#-9223368509294045088>, (1088, 320): <ImageTexture#-9223369958342191179>, (1088, 352): <ImageTexture#-9223368509294045088>, (1024, 384): <ImageTexture#-9223368509545703333>, (1056, 384): <ImageTexture#-9223368509243713439>, (1088, 384): <ImageTexture#-9223368510200014770>, (992, 416): <ImageTexture#-9223368509797361578>, (1024, 416): <ImageTexture#-9223368509495371684>, (1056, 416): <ImageTexture#-9223368509193381790>, (928, 448): <ImageTexture#-9223368510351009717>, (960, 448): <ImageTexture#-9223368510049019823>, (992, 448): <ImageTexture#-9223368509747029929>, (832, 448): <ImageTexture#-9223369947353114807>, (864, 448): <ImageTexture#-9223368510300678068>, (896, 448): <ImageTexture#-9223368510401341366>, (800, 480): <ImageTexture#-9223369947655104682>, (832, 480): <ImageTexture#-9223369947302783298>, (864, 480): <ImageTexture#-9223368510250346419>, (768, 480): <ImageTexture#-9223369948007426342>, (992, 288): <ImageTexture#-9223369958392522828>, (992, 384): <ImageTexture#-9223368509847693227>, (992, 480): <ImageTexture#-9223368509696698280>, (1024, 288): <ImageTexture#-9223369958040201285>, (1024, 448): <ImageTexture#-9223368509445040035>, (1024, 480): <ImageTexture#-9223368509394708386>, (1056, 288): <ImageTexture#-9223369958040201285>, (1056, 448): <ImageTexture#-9223368509143050141>, (1056, 480): <ImageTexture#-9223368509092718492>, (1088, 288): <ImageTexture#-9223369958392522828>, (1088, 416): <ImageTexture#-9223368510200014770>, (1088, 448): <ImageTexture#-9223368509596034982>, (1088, 480): <ImageTexture#-9223368509294045088>, (1120, 288): <ImageTexture#-9223369958040201285>, (1120, 320): <ImageTexture#-9223369957989869636>, (1120, 352): <ImageTexture#-9223369947453778088>, (1120, 384): <ImageTexture#-9223368509898024876>, (1120, 416): <ImageTexture#-9223368509898024876>, (1120, 448): <ImageTexture#-9223368509294045088>, (1120, 480): <ImageTexture#-9223368509243713439>, (1152, 288): <ImageTexture#-9223369958040201285>, (1152, 320): <ImageTexture#-9223369957989869636>, (1152, 352): <ImageTexture#-9223369947453778088>, (1152, 384): <ImageTexture#-9223368509596034982>, (1152, 416): <ImageTexture#-9223368509596034982>, (1152, 448): <ImageTexture#-9223368509294045088>, (1152, 480): <ImageTexture#-9223368509243713439>, (960, 288): <ImageTexture#-9223369958744844371>, (960, 320): <ImageTexture#-9223368509898024876>, (960, 384): <ImageTexture#-9223368510149683121>, (960, 416): <ImageTexture#-9223368510099351472>, (960, 480): <ImageTexture#-9223368509998688174>, (896, 288): <ImageTexture#-9223369959449487457>, (896, 384): <ImageTexture#-9223368510502004706>, (896, 416): <ImageTexture#-9223368510451673015>, (896, 480): <ImageTexture#-9223368510351009717>, (928, 288): <ImageTexture#-9223369959097165914>, (928, 320): <ImageTexture#-9223368510200014770>, (928, 384): <ImageTexture#-9223368510451673015>, (928, 416): <ImageTexture#-9223368510401341366>, (928, 480): <ImageTexture#-9223368510300678068>, (864, 288): <ImageTexture#-9223369959801809046>, (864, 352): <ImageTexture#-9223368510451673015>, (864, 384): <ImageTexture#-9223368510401341366>, (864, 416): <ImageTexture#-9223368510351009717>, (800, 352): <ImageTexture#-9223369947806099749>, (800, 384): <ImageTexture#-9223369947806099749>, (800, 416): <ImageTexture#-9223369947755767967>, (800, 448): <ImageTexture#-9223369947705436302>, (800, 512): <ImageTexture#-9223369947604773033>, (832, 352): <ImageTexture#-9223369947453778088>, (832, 384): <ImageTexture#-9223369947453778088>, (832, 416): <ImageTexture#-9223369947403446459>, (832, 512): <ImageTexture#-9223369947252451652>, (864, 512): <ImageTexture#-9223369947202119742>, (896, 512): <ImageTexture#-9223368510300678068>, (928, 512): <ImageTexture#-9223368510250346419>, (960, 512): <ImageTexture#-9223368509948356525>, (736, 320): <ImageTexture#-9223369959449487457>, (768, 320): <ImageTexture#-9223369959801809046>, (768, 352): <ImageTexture#-9223369948158421202>, (768, 384): <ImageTexture#-9223369948158421202>, (768, 416): <ImageTexture#-9223369948108099662>, (768, 448): <ImageTexture#-9223369948057757907>, (768, 512): <ImageTexture#-9223369947957094557>, (704, 320): <ImageTexture#-9223369959801809046>, (672, 320): <ImageTexture#-9223369959449487457>, (640, 320): <ImageTexture#-9223369959801809046>, (608, 320): <ImageTexture#-9223369959801809046>, (576, 320): <ImageTexture#-9223369959801809046>, (544, 320): <ImageTexture#-9223369959801809046>, (544, 352): <ImageTexture#-9223369959751477351>, (544, 384): <ImageTexture#-9223369959701145702>, (544, 416): <ImageTexture#-9223369959650814053>, (544, 448): <ImageTexture#-9223369959600482404>, (544, 480): <ImageTexture#-9223369959550150755>, (544, 512): <ImageTexture#-9223369959499819106>, (512, 320): <ImageTexture#-9223369959801809046>, (512, 352): <ImageTexture#-9223369959751477351>, (512, 384): <ImageTexture#-9223369959701145702>, (512, 416): <ImageTexture#-9223369959650814053>, (512, 448): <ImageTexture#-9223369959600482404>, (512, 480): <ImageTexture#-9223369959550150755>, (512, 512): <ImageTexture#-9223369959499819106>, (992, 512): <ImageTexture#-9223368509646366631>, (864, 544): <ImageTexture#-9223369922103404629>, (896, 544): <ImageTexture#-9223368510250346419>, (928, 544): <ImageTexture#-9223368509948356525>, (960, 544): <ImageTexture#-9223368509646366631>, (992, 544): <ImageTexture#-9223368509344376737>, (1024, 512): <ImageTexture#-9223368509344376737>, (1024, 544): <ImageTexture#-9223368509042386843>, (1056, 512): <ImageTexture#-9223368509042386843>, (1056, 544): <ImageTexture#-9223368509143050141>, (1088, 512): <ImageTexture#-9223368509243713439>, (1088, 544): <ImageTexture#-9223368509193381790>, (1120, 512): <ImageTexture#-9223368509193381790>, (1120, 544): <ImageTexture#-9223368509143050141>, (1152, 512): <ImageTexture#-9223368509193381790>, (1152, 544): <ImageTexture#-9223368509143050141>, (1184, 352): <ImageTexture#-9223369947453778088>, (1184, 384): <ImageTexture#-9223368509294045088>, (1184, 416): <ImageTexture#-9223368509294045088>, (1184, 448): <ImageTexture#-9223368509294045088>, (1184, 480): <ImageTexture#-9223368509243713439>, (1184, 512): <ImageTexture#-9223368509193381790>, (1184, 544): <ImageTexture#-9223368509143050141>, (1216, 352): <ImageTexture#-9223369947453778088>, (1216, 384): <ImageTexture#-9223369947403446459>, (1216, 416): <ImageTexture#-9223368509294045088>, (1216, 448): <ImageTexture#-9223368509243713439>, (1216, 480): <ImageTexture#-9223368509193381790>, (1216, 512): <ImageTexture#-9223368509143050141>, (1216, 544): <ImageTexture#-9223368509092718492>, (832, 544): <ImageTexture#-9223369947202119742>, (800, 544): <ImageTexture#-9223369947554441504>, (768, 544): <ImageTexture#-9223369947906763033>, (672, 576): <ImageTexture#-9223369948913395927>, (704, 576): <ImageTexture#-9223369948561074347>, (736, 576): <ImageTexture#-9223369948208762956>, (768, 576): <ImageTexture#-9223369947856431238>, (800, 576): <ImageTexture#-9223369947504119892>, (832, 576): <ImageTexture#-9223369947151788093>, (992, 576): <ImageTexture#-9223368509344376737>, (992, 608): <ImageTexture#-9223368509344376737>, (992, 640): <ImageTexture#-9223368509646366631>, (1024, 576): <ImageTexture#-9223368509042386843>, (1024, 608): <ImageTexture#-9223368509042386843>, (1024, 640): <ImageTexture#-9223368509344376737>, (1056, 576): <ImageTexture#-9223368509092718492>, (1056, 608): <ImageTexture#-9223368509042386843>, (1056, 640): <ImageTexture#-9223368509042386843>, (1088, 576): <ImageTexture#-9223368509143050141>, (1088, 608): <ImageTexture#-9223368509092718492>, (1088, 640): <ImageTexture#-9223368509042386843>, (1120, 576): <ImageTexture#-9223368509092718492>, (1120, 608): <ImageTexture#-9223368509042386843>, (1120, 640): <ImageTexture#-9223368516978010084>, (1152, 576): <ImageTexture#-9223368509092718492>, (1184, 576): <ImageTexture#-9223368509092718492>, (1216, 576): <ImageTexture#-9223368509042386843>, (1152, 608): <ImageTexture#-9223368509042386843>, (1184, 608): <ImageTexture#-9223368509042386843>, (960, 576): <ImageTexture#-9223368509646366631>, (960, 608): <ImageTexture#-9223368509646366631>, (960, 640): <ImageTexture#-9223368509948356525>, (928, 576): <ImageTexture#-9223368509948356525>, (928, 608): <ImageTexture#-9223368509948356525>, (928, 640): <ImageTexture#-9223368510250346419>, (896, 576): <ImageTexture#-9223368510250346419>, (896, 608): <ImageTexture#-9223368510250346419> }

Hello esteemed jonPS.

To answer your question: Yes, this is possible and a part of gdscript’s features.

# Storage
var array_containing_a_dictionary = [
    {
        "dictionary_number" = 1,
    },
    {
        "dictionary_number" = "two",
    },
]

# Access

print(array_containing_a_dictionary[2]["dictionary_number"]) # will print "two"

# Iteration

for dict in array_containing_a_dictionary:
    # For access is by value, not by key.
    # This means that dict is now the dictionary in array_containing_a_dictionary[iteration]
    # where iteration increases with each loop.

    print(dict["dictionary_number"]) # Will print "1" first, then "two" the second loop.

I hope this brief example helps you.

Regards,

can you use this in a draw function ?


var array = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var arrayNUM = 2;

var dictionaryONE = {
	'Fnum': [    ]
}

@export var dictionaryTWO = {};



dictionaryONE.Fnum.append(  { "cords": Vector2(i, j), "img": ImageTexture.create_from_image(drawText.get_image().get_region( Rect2( i, j, 32, 32) ) ) } );

for i in range (0, dictionaryONE.Fnum.size() ):
	dictionaryTWO [ mGpos + mouseGroup.Fnum[i].cords ] = mouseGroup.Fnum[i].img;

array[arrayNUM] = dictionaryTWO ;


func _draw():
#this is working normally:
	for i in dictionaryTWO : 
		draw_texture( dictionaryTWO [i], i );

#this doesnt work but it should draw all the dictionaryTWO  inside var array
for P in range ( 0, array.size() )

	for i in dictionaryTWO : 
		draw_texture( array[P].dictionaryTWO [i], i );

i meant to draw all things inside dictionaryTWO but fetch them in seperate from array[P]

Try this

for p in range(array.size()):
  var dict: Dictionary = array[p]
  for i in dict:
     draw_texture(dict[i], i)

It doesnt work…
i think i had already tried that way, i get the same error… I also dont understand why i can assign an int to a dictionary…?

for p in range(array.size()):
  var dict: Dictionary = array[p] ##### line 222
  for i in dict:
     draw_texture(dict[i], i)

res://test3/tileDrawNode.gd:222 - Trying to assign value of type ‘int’ to a variable of type ‘Dictionary’.
res://test3/tileDrawNode.gd:222 - Trying to assign value of type ‘int’ to a variable of type ‘Dictionary’.
res://test3/tileDrawNode.gd:222 - Trying to assign value of type ‘int’ to a variable of type ‘Dictionary’.

[edit]: it worked by doing this:

for p in range( array.size() ):
  var dict = array[p]
  for i in dict:
     draw_texture(dict[i], i)

but instead of adding the values of “dictionaryTWO” to arrayLayer position 0…
Its doing the same thing has in my original code witch is replacing the keys in “dictionaryTWO” witch are vector2 positions…

Instead of drawing the images in a diferent arrayLayer[layerNumber] —> “dictionaryTWO( vector2(posX, posY), image )”
it replaces the KEYS vector2(posX, posY)… It doesnt matter on what layer iam on: if its arrayLayer[0] or [1], [2], [3], [4]… it always replaces the keys of "dictionaryTWO( vector2(posX, posY), " on all arrayLayer[layerNumber]

It need to check i what layer is on “arrayLayer” if its 0, 1, 2, 3, 4 or 5

thenj use that lçayer number :

draw_texture( arrayLayer[0]. dict[i], i )

number this: pppppp
for p in range( array.size() ):
draw_texture( arrayLayer[pppppp]. dict[i], i )

i cant access the dictionary inside arrayLayer[p]
How do i access the dictionary values inside an array[0] 1, 2, 3, 4, 5, 6 etc…

I would imagine that the variable ‘layer’ needs to be an array of dictionaries, so do like:

var layer = [{},{},{}] or however many dictionaries you want to store in there.

on line 29 on that first screenshot.

1 Like

okay…
I think i manage to store canvasTex in layers
But i cant acess the values inside layers in draw func ? The moment i put canvasTex inside an array everything stops working, i cant acess the array nr, the dictionary keys / values etc… the only thing i can fecth is layers has a whole

extends Sprite2D



var mouseGroup = {
	'Fnum': []
}

var canvasTex = {};

var mouse_tex;

var layers = [{},{},{},{},{},{},{},{},{},{},{},{}] 
var layerPOS = 0;

var root1 = {};


func _process(_delta):
	if ( _image.mouse_tex != null ):
		mouse_tex = _image.mouse_tex;
	
	var mXG = snapped(  get_global_mouse_position().x, 32 );
	var mYG = snapped(  get_global_mouse_position().y, 32 );
	mGpos = Vector2 (mXG, mYG);
	
	if ( Input.is_action_just_pressed("ui_page_down") ):
		layerPOS -= 1;
		layerPOS = clamp(layerPOS, 0, 10 );
	elif ( Input.is_action_just_pressed("ui_page_up") ):
		layerPOS += 1;
		layerPOS = clamp(layerPOS, 0, 10 );


	if ( Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) && mouseJustPress == true ):
		
		for i in range (0, mouseGroup.Fnum.size() ):
			canvasTex[ mGpos + mouseGroup.Fnum[i].cords ] = mouseGroup.Fnum[i].img;
		
		layers[ layerPOS ] = canvasTex; ##########



func  _draw():
	for p in range(0, 5 ):
		
		for i in canvasTex:
			
			if ( layers.has(p) ):
				draw_texture(   layers[ p ] [i]   ,  i   );
	for p in range(0, 5 ):
		for i in canvasTex:
			#if ( layers.has(p) ):
			draw_texture(   layers[ p ] [i]   ,  i   );

error: Invalid get index ‘(480,352)’ (on base ‘Dictionary’)

Most likely ‘layers[p]’ has an empty dictionary, I’d check to make sure you are putting a canvasText dictionary object in the right indices of the layers array.

What you can do is print out ‘layers[p]’ before you call draw_texture and see if it has anything in it, and if it’s empty look at the way you are filling that layers array.

ive printed it… but i dont know how to fecth this values…
in here ive set layerPOS to ‘0’ and ‘3’

i also think hes putting the values in layerPOS[0] into layerPOS[3] and vice versa instead of keeping them seperate

[{ (448, 384): <ImageTexture#-9223371992932023014> }, { }, { }, { }, { }, { }, { }, { }, { }, { }, { }, { }]
[{ (448, 384): <ImageTexture#-9223371992932023014> }, { }, { }, { }, { }, { }, { }, { }, { }, { }, { }, { }]
[{ (448, 384): <ImageTexture#-9223371992932023014> }, { }, { }, { }, { }, { }, { }, { }, { }, { }, { }, { }]
[{ (448, 384): <ImageTexture#-9223371992932023014> }, { }, { }, { }, { }, { }, { }, { }, { }, { }, { }, { }]
[{ (448, 384): <ImageTexture#-9223371992932023014> }, { }, { }, { }, { }, { }, { }, { }, { }, { }, { }, { }]
[{ (448, 384): <ImageTexture#-9223371992932023014> }, { }, { }, { }, { }, { }, { }, { }, { }, { }, { }, { }]
[{ (448, 384): <ImageTexture#-9223371992932023014> }, { }, { }, { }, { }, { }, { }, { }, { }, { }, { }, { }]
[{ (448, 384): <ImageTexture#-9223371992932023014> }, { }, { }, { }, { }, { }, { }, { }, { }, { }, { }, { }]
[{ (448, 384): <ImageTexture#-9223371992932023014>, (416, 384): <ImageTexture#-9223371975685044919> }, { }, { }, { (448, 384): <ImageTexture#-9223371992932023014>, (416, 384): <ImageTexture#-9223371975685044919> }, { }, { }, { }, { }, { }, { }, { }, { }]
[{ (448, 384): <ImageTexture#-9223371992932023014>, (416, 384): <ImageTexture#-9223371975685044919> }, { }, { }, { (448, 384): <ImageTexture#-9223371992932023014>, (416, 384): <ImageTexture#-9223371975685044919> }, { }, { }, { }, { }, { }, { }, { }, { }]
[{ (448, 384): <ImageTexture#-9223371992932023014>, (416, 384): <ImageTexture#-9223371975685044919> }, { }, { }, { (448, 384): <ImageTexture#-9223371992932023014>, (416, 384): <ImageTexture#-9223371975685044919> }, { }, { }, { }, { }, { }, { }, { }, { }]
[{ (448, 384): <ImageTexture#-9223371992932023014>, (416, 384): <ImageTexture#-9223371975685044919> }, { }, { }, { (448, 384): <ImageTexture#-9223371992932023014>, (416, 384): <ImageTexture#-9223371975685044919> }, { }, { }, { }, { }, { }, { }, { }, { }]
[{ (448, 384): <ImageTexture#-9223371992932023014>, (416, 384): <ImageTexture#-9223371975685044919> }, { }, { }, { (448, 384): <ImageTexture#-9223371992932023014>, (416, 384): <ImageTexture#-9223371975685044919> }, { }, { }, { }, { }, { }, { }, { }, { }]
[{ (448, 384): <ImageTexture#-9223371992932023014>, (416, 384): <ImageTexture#-9223371975685044919> }, { }, { }, { (448, 384): <ImageTexture#-9223371992932023014>, (416, 384): <ImageTexture#-9223371975685044919> }, { }, { }, { }, { }, { }, { }, { }, { }]
[{ (448, 384): <ImageTexture#-9223371992932023014>, (416, 384): <ImageTexture#-9223371975685044919> }, { }, { }, { (448, 384): <ImageTexture#-9223371992932023014>, (416, 384): <ImageTexture#-9223371975685044919> }, { }, { }, { }, { }, { }, { }, { }, { }]
— Debugging process stopped —

i have a project on github / inside folder test2 / testdel_2.gd, line 189, 190, 191
it draws canvasTex has it should, the moment i use an array there noway to retrieve any value.

You can see it running for yourself although i thought you will be able to do anything… if you put canvasTex inside an array[layerPOS]

gitHub proj:

godot-4-TileDraw/test2/testdel_2.gd at main · jonSP12/godot-4-TileDraw · GitHub

layers[p][i]

This will fetch the ‘canvasTex’ dictionary value located at ‘layers[p]’ in the layers list, but if the key ‘i’ does not exist in that dictionary, it will give you an error.

I would add a debug print before the draw_texture function where you should print out ‘layers[p]’ and then the key ‘i’, and see if there is a match.

You could also add in a line for checking if the key exists, like:

if (!layers[p].has(i)):
  continue;

and do something with that.

thanks… it worked

for p in range(0, 5 ):
	for i in canvasTex:
		if ( layers[p].has(i) ):
			draw_texture(   layers[ p ] [i]   ,  i   );

but iam back at the beginning…
the dictionary keys get replaced if they are identical, this is replacing the images in layer 0 by images in layer 3

i think the problem is here…

if ( Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) && mouseJustPress == true ):
	for i in range (0, mouseGroup.Fnum.size() ):
		var coords = mGpos + mouseGroup.Fnum[i].cords;   ### vector2(x, y)
		var imgText  = mouseGroup.Fnum[i].img;   ########## image_texture
		canvasTex[ coords ] = imgText;

Ive put the values in vars coords, and imgText, to look simpler

if i could put ‘canvasText’ inside an array[ layerPOS ], and still have canvasText with a key coords, it might work…

var canvasTex = {};

ive tried this: but nothing seems to work, its same problem i cant fecth the values or they get replaced in all layers…

canvasTex[  [coords]  ] = imgText; # this will print out a ZERO

It depends on what you are trying to do, seems like you want to draw an image on the canvas item object at the place the mouse is clicked? How are the canvas items created and what are they supposed to do after they are?

Do you have an outline of what the game loop here that you want?

it picks up a texture from an image and draws it using the draw function

the code is in folder test2 / testdel_2.gd,
lines 129, and 191

the image is broken into small pieces, stored in a dictionary, then drawn on (func draw)

Iam not creating canvas items, that would be a problem for me, i would have to acess the draw func in diferent nodes from 1 parent script… it would create a lot of problems that i dont know how to work with.

its simple…
went to lunch, got it working in 10 sec…


var mouseGroup = {
	'Fnum': [  ]
}

var canvasTex = {};

var mouse_tex;

var layers = {
	'Lnum': [ {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} ]
}
var layerPOS = 0; 





func _process(_delta):
	if ( _image.mouse_tex != null ):
		mouse_tex = _image.mouse_tex;
	
	var mXG = snapped(  get_global_mouse_position().x, 32 );
	var mYG = snapped(  get_global_mouse_position().y, 32 );
	mGpos = Vector2 (mXG, mYG);
	
	if ( Input.is_action_just_pressed("ui_page_down") ):
		layerPOS -= 1;
		layerPOS = clamp(layerPOS, 0, 10 );
	elif ( Input.is_action_just_pressed("ui_page_up") ):
		layerPOS += 1;
		layerPOS = clamp(layerPOS, 0, 10 );






	if ( Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) && mouseJustPress == true ):
		
		for i in range (0, mouseGroup.Fnum.size() ):
			
			var coords = mGpos + mouseGroup.Fnum[i].cords;
			var imgText  = mouseGroup.Fnum[i].img;
			
			layers.Lnum[ layerPOS ][ coords ] = imgText; ##################




func  _draw():
	if ( mouse_tex == null  ):
		return;



	for p in range(0, 5 ):
		
		for i in layers.Lnum[p]:
			if ( layers.Lnum[p].has(i) ):
				draw_texture( layers.Lnum[p][i], i );

layers [ 0 ] draws the textures
layers [ 1 ] draws the textures on top of layers [ 0 ]

switch back to layers [ 0 ]
draw a texture in a place were a texture is already drawn and the old texture gets replaced by the new…
its the same thing on layers [ 1 ]

i had already tried this solution, but it didnt worked out, there were errors…
i was missing this small check on the for loop — if ( layers.Lnum[p].has(i) ):