Problem with an Godot SMRT system

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

Hello! I’m testing a textbox system made for a old version of godot, it’s works fine, but the question/answer
feacture doesn’t. I found out that the problem it’s the fact that the index of the pressed button it’s alwais 0 for some reason.

func question(answer_array):
	if show_debug_messages:
		print("STARTED QUESTION FUNCTION")
	btn_answers = VBoxContainer.new()
	btn_answers.set_alignment(HALIGN_LEFT)
	
	btn_answers.set_anchor(MARGIN_LEFT, ANCHOR_BEGIN)
	btn_answers.set_anchor(MARGIN_TOP, ANCHOR_BEGIN)
	btn_answers.set_anchor(MARGIN_RIGHT, ANCHOR_END)
	btn_answers.set_anchor(MARGIN_BOTTOM, ANCHOR_END)
	
	dialog_dup = duplicate()
	dialog_dup.rect_size = btn_answers.rect_position
	dialog_dup.visible = false
	btn_answers.rect_position = (Vector2(0,0))
	dialog_dup.set_script(null)
	add_child(dialog_dup)
	for child in dialog_dup.get_children():
		child.queue_free()
	#remove_child(btn_answers)
	dialog_dup.add_child(btn_answers)
	btn_answers.set_focus_mode(Control.FOCUS_CLICK)#grab_focus()
	btn_answers.rect_size = (Vector2(0,0))
	if position <= 1:
		dialog_dup.rect_position = (Vector2(textObj.rect_position.x, get_viewport_rect().size.y/2))
	else:
		#question will appear on top of the dialog
		dialog_dup.rect_position = (Vector2(textObj.rect_position.x, rect_size.y*-1))
	var index = 0
	for answer in answer_array:
		var b = Button.new()
		b.set_text(answer)
		b.add_font_override("font", font)
		b.set_flat(true)
		b.add_stylebox_override("normal", StyleBox)
		b.add_stylebox_override("selected", StyleBox)
		b.add_stylebox_override("pressed", StyleBox)
		b.add_stylebox_override("hover", StyleBox)
		b.add_font_override("font_selected", font)
		btn_answers.add_child(b)
		b.connect("pressed",self,"selected_answer",[index])
		if index > 0:
			var last_child = btn_answers.get_child(index-1)
			b.set_focus_neighbour(MARGIN_TOP,last_child.get_path())
			btn_answers.rect_size.y += b.rect_size
			index+=1
	
	yield(get_tree(),"idle_frame")
	btn_answers.get_children()[0].grab_focus()
	yield(get_tree(), "idle_frame")
	btn_answers.set_margin(MARGIN_RIGHT, 0)
	dialog_dup.visible = true
	dialog_dup.rect_size = btn_answers.rect_size

Can someone help me?
The problem it’s in this line:

b.connect("pressed",self,"selected_answer",[index])

I’m using this system:

:bust_in_silhouette: Reply From: Lopy

Index is not updated inside the loop:

var index = 0
for answer in answer_array:
    ...
    b.connect("pressed",self,"selected_answer",[index])
    if index > 0:
        ...
        index+=1

Putting index += 1 one indentation level higher should solve the issue.

Edit: Fixing index staying at 0 led previously unreachable code to be run. btn_answers.rect_size.y += b.rect_size should probably be btn_answers.rect_size.y += b.rect_size.y or btn_answers.rect_size += b.rect_size.

Nop it jus gave me a error in:

btn_answers.rect_size.y += b.rect_size

Error:

Invalid operands 'float' and 'Vector2' in operator '+'.

X | 2021-05-16 01:31