else statement not working

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

Am I missing something really obvious? Because I don’t understand why this is not working.
The problem is at the very end of the function but I thought I would paste everything just in case.

func create_Item_Grid_tag(item_data):
	print("\n :create_Item_Grid_tag:")
	var item_name :String = item_data[0]
	var item_anim :int = item_data[1]
	var item_button :Object = button_scn.instance()
	var item_object :Object = item_button.get_node("Item")
	item_object.frame = item_anim
	item_button.name = item_name
	Item_Grid.rect_scale = Vector2(0.5,0.5)
	#Item has no recipe
	if item_data[7] == null:
	#Item has recipe
	for material in item_data[7]["recipe"]: 
		var total_amount :int = WL.WList_mats[material]
		var inv_loop :bool = false
		var dict_amount :Dictionary
		#If Material Tag exist
		if Mat_Grid.has_node(material):
		#If Material Tag doesn't exist
			var inv_amount:int
			if !inv_loop:
				dict_amount = get_Mat_Grid_inv_amount([item_data[7]["recipe"].keys()])
				inv_amount = dict_amount[material]
				inv_loop = true
			if inv_loop:
				print("inv_loop Triggered")
				inv_amount = dict_amount[material]


if !inv_loop:

if inv_loop:

if if inv_loop is an else or even an elif inv_loop it won’t go inside that statement and call create_Mat_Grid_tag()

my whole idea was to get an array with all the recipe ingredients and search for all of them in the same inventory loop instead of looping thought the inventory multiple times but searching different ingredients. And then this happened. It works as intended with the if statement but I would like to know why the others don’t work in this case.

:bust_in_silhouette: Reply From: p7f

So, you are initializing inv_loop with false, so it enters the first if, and ofcourse, it wont enter the else, as it already entered the if
It does not matter if you change the value of inv_loop inside the if… once it entered the if, it wont enter the else.

Btw, if you want that part of code to be executed after the if, why dont you just put it inside the if?

Ye… my bad so dumb of me I’ve been looking at all this big functions and loops for 3 days.

for this to work I would need to get the inv_loop out of the for loop so it didn’re reset on the next iteration of the loop.

I don’t want to put the 2 if statements together because I only want the loop through the inventory once and get every ingredients amount in that single pass instead of having 1 loop for every ingredient where the loop is only looking for that single item I think this way is more resource efficient. I save the ingredients and their amounts from
get_Mat_Grid_inv_amount() and save them in a dictionary then call for every ingredient in the recipe create_Mat_Grid_tag() and use the dictionary to update the numbers in case the player already has any amount of that item in the inventory.

Right now I’m doing 2 loops through the inventory to get the same dict…

also I come from Construct2 and maybe I was just way too ignorant back then but sometimes if a function take too long the code would keep going so. If I called get_Mat_Grid_inv_amount() and it took too long It would start calling create_Mat_Grid_tag() while the previous function is still running so in this case It would crash because I need the values I didn’t know if this was the case in Godot but I always operated like it was a thing in programming.

Thanks tho I’m sure it would had taken me a while to realise such stupid mistake.

IHate | 2020-09-13 01:52