Topic was automatically imported from the old Question2Answer platform.
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:
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)