Invalid set index 'rect_position' (on base: 'Vector2') with value of type 'Vector2'.

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

I have followed a youtube tutorial to make a joystick for my player everything works fine except the joystick once moved it will disappears and gives this error :
Invalid set index ‘rect_position’ (on base: ‘Vector2’) with the value of type ‘Vector2’.

I am giving my joystick code and player code

joystick code :

extends Control

var start_pos: Vector2 = Vector2.ZERO
var end_pos: Vector2 = Vector2.ZERO
var valid_pos = false

onready var js_pos = get_node("background").rect_position
onready var js_bg = get_node("background")
onready var js_handle = get_node("background/handle")

signal swipe_detect(swipe_direction,strength)
signal swipe_end

func _input(event : InputEvent) -> void:
	if valid_pos:
		if event is InputEventScreenDrag:
			if start_pos == Vector2.ZERO:
				start_pos = event.position
				js_bg.rect_position = Vector2(start_pos.x - 128, start_pos.y - 128)
			end_pos = event.position
			var direction = (end_pos - start_pos)
			if direction.length() < 120:
				js_handle.rect_position.x = 78 + direction.length()
				js_handle.rect_pivot_offset.x = 50 - direction.length()
			js_handle.rect_rotation = rad2deg(direction.angle())
			emit_signal("swipe_detect",direction, direction.length())

func _on_areaclickbutton_button_down():
	valid_pos = true

func _on_areaclickbutton_button_up():
	js_bg = js_pos
	start_pos = Vector2.ZERO
	end_pos = Vector2.ZERO
	valid_pos = false
	js_handle.rect_position.x = 78
	js_handle.rect_pivot_offset.x = 50

I have set the numbers according to my sprites size as mentioned in the tutorial

player code :

extends KinematicBody2D
var speed = 300
var direction = Vector2.ZERO

func _physics_process(delta):
        move_and_slide(direction * speed)
func _on_Joystick_swipe_detect(swipe_direction,strength):
	direction = swipe_direction.normalized()
	$".".rotation_degrees = rad2deg(swipe_direction.angle())
	if strength < 300:
		speed = strength

func _on_Joystick_swipe_end():
	direction = Vector2.ZERO
:bust_in_silhouette: Reply From: jgodfrey

So, here…

onready var js_pos = get_node("background").rect_position
onready var js_bg = get_node("background")

… you assign js_pos to the rect_position of some control. So, that’s now a Vector2. However, js_bg is just the control itself. Later, in _on_areaclickbutton_button_up() you do this:

js_bg = js_pos

So, you assign the Vector2 to the var that used to contain a control reference.

Somewhere else you do this:


I assume, based on the execution timing of all of the above, that you’ve made js_bg a Vector2 and then sometime after the code immediately above is executed. So, you have a Vector2 that you’re trying to to get a rect_position of, which isn’t possible.

I assume the real problem is here:

js_bg = js_pos

The left side was originally a Control and the right side is a Vector2. I’d guess you don’t intend to make exactly that assignment…

yes, it was correct I made that mistake when I typed it. it was actually meant to be
js_bg.rect_position = js_pos thankyou so much

abhiram-ms | 2023-05-16 12:02