Convert argument 1 from Object to Vector2 Error

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

I’m trying to implement a choose gender only changing the animated sprite, so choose to create a separated scene for each gender it should spawn the chosen character under the level with the Position2D node, using a singleton variable, but I’m having the error Invalid type in function ‘set_position’ in base ‘KinematicBody2D (Player.gd)’. Cannot convert argument 1 from Object to Vector2… Could someone help me? Please

The code in the level goes like this:

extends Node2D
var Fem = load("res://Scenes/FTests.tscn")
var Mal = load("res://Scenes/MTests.tscn")
onready var spawn = get_node("Level1/Position2D")
func _process(delta):
if Global.chosen_character == 0:
	spawn = Mal.instance()
	spawn.set_position(Position2D)
	add_child(Mal)
else:
	spawn = Fem.instance()
	spawn.set_position(Position2D)
	add_child(Fem)

The code of the defined variable in the singleton named Global is:

extends Node
#Player 
var chosen_character

And the script where I do the gender choosing goes:

extends Control
func _on_BackButton_pressed():
get_tree().change_scene("res://Scenes/TitleScreen.tscn")
pass
func _on_MaSelButton_pressed():
	get_tree().change_scene("res://Scenes/Level1.tscn")
	Global.chosen_character = 0  #global variable
	pass
func _on_FeSelButton_pressed():
	get_tree().change_scene("res://Scenes/Level1.tscn")
	#Global.chosen_character = 1  #global variable
	pass
:bust_in_silhouette: Reply From: Inces

You pass incorrect argument into set_position() function. You are supposed to pass literal position - measured in vector2, x and y. Instead You pass Position2D, which is a Node. If You wanted spawn to have the same position as Position2D, than it should go spawn.set_position(Position2D.position)

Thanks for the reply, when I edit it to

spawn.set_position(Position2D.position)

it now drops Invalid get index ‘position’ (on base: ‘GDScriptNativeClass’). But I can’t find where do the error drops

widows | 2021-03-26 21:36

Position2D is a script class, which You can instance a lot of times in form of unique node. In code You are supposed to reference one of these unique nodes instead of a class as a whole. I see You made some weird mess in code : first You defined var spawn as unique node of position2d class and then You overrided it to be one of gender scenes. So You lost reference to position node. All You need to do now is keep this reference, and make separate variable for gender instance :
Var girl = Fem.instance()
girl.position = spawn.position
add_child(girl)

Inces | 2021-03-27 07:10

:bust_in_silhouette: Reply From: Bean_of_all_Beans

I’m assuming the "Level1/Position2D" is the Position2D Node, which inherits Node2D. This is a simple fix, really. Change:

spawn.set_position(Position2D)

to

spawn.set_position(Position2D.position)

and it should work.
What you are currently doing is passing a node to the method set_position, instead of a Vector2 that is the node’s position.