Changing the alignment of UI elements with code?

Godot Version

Godot 4.4

Question

I’m trying to make a cell phone texting interface. I want some lines of text to appear on the right side of the screen, and some lines to appear on the left. However, when I try to change the alignment property, I get an error. I’ve tried typing the variable as a number and as text, but both give me errors. Is there a way to display text on two alternating sides of the screen?

Here is the code for the alignment that I am trying to use. The code references a scroll container that contains the VBox container used to display lines of text:

func _on_button_pressed() -> void:
	var entry = TEXTS.instantiate()
	dialogue_entries.add_child(entry)
	
	if _next_content_index >= CONTENTS.size():
		print("The last line has already been added!")
		return
	if _next_content_index >= SPEAKERS.size():
		print("The last line speaker has already spoken")
		return
	if _next_content_index == 2 or 3 or 5 or 9:
		scroll_container.AlignmentMode = 2
	else:
		scroll_container.AlignmentMode = 0
	entry.set_content(SPEAKERS[_next_content_index], CONTENTS[_next_content_index])
	_next_content_index += 1

Here is the display that shows when I roll over the VBox alignment property:

Property Alignment: AlignmentMode = 0

This is what the project currently looks like:

It looks like AnchorPreset is what I should be working with, but the inspector says it cannot be changed with code. Maybe changing the position directly would work?

I’m trying to change them on position with this code:

func _on_button_pressed() -> void:
	var entry = TEXTS.instantiate()
	dialogue_entries.add_child(entry)
	
	if _next_content_index >= CONTENTS.size():
		print("The last line has already been added!")
		return
	if _next_content_index >= SPEAKERS.size():
		print("The last line speaker has already spoken")
		return
	if _next_content_index == 2 or 3 or 5 or 9:
		entry.Vector2 = Vector2(0, 300)
	else:
		entry.Vector2 = Vector2(0, 0)
	entry.set_content(SPEAKERS[_next_content_index], CONTENTS[_next_content_index])
	_next_content_index += 1

However, I am getting an error that says: “Invalid assignment of property or key “Vector 2” with value of type “Vector2” on a base object of type “MarginContainer(texts.gd).”

Set Layout Mode to Anchor and Anchor Preset to Custom. Now you’ll get Anchor Points and Anchor Offset properties. Play with those until you get the result you want and then set those properties to those values from code.

Thanks, I’ll try that!

I tried changing the values. They showed up in the viewport of the child scene, but the instantiated version in the main scene remained the same. Apologies for the new post, I thought that the main scene remaining static constituted a different issue.

Read again what I wrote:

Play with those until you get the result you want and then set those properties to those values from code.

Set them in editor to see the values that give you the wanted result. Remember those values and assign them to corresponding properties of the instance from script, every time you create one.