How do I move an object to a different coordinate using a script?

Godot Version

4.2.2

Question

I’m transitioning between locations. It is designed in such a way that when the player’s character touches the trigger, the trigger saves the pre-recorded values to the global script variables. A total of three variables are used. Two of them are horizontal and vertical positions. The third only shows whether the player has been moved to another location and whether it is necessary to change his position. However, when I write in the script of the target location so that it would move the player to the desired coordinates after teleporting him, he does not move anything and the character does not appear at the entrance, but where I left him editing the scene. What do you need to do in general to be able to move any object to another coordinate point?

p.s. First, I indicated the text of the question in the version column :stuck_out_tongue:

i would need to know the code of how you currently move the character.

Teleporting an character is done by setting its position/global_position value.
Moving an object requires time duration and also whether to move him via physics (direction * speed) or constantly (maybe tweening the position)

I cleaned up this code. But I just used “$Frisk.velocity.x(or y)=SceneChange.target_tp_x(or y)” at the entrance to the location. “Frisk” is the name of the object that is the player’s character. “SceneChange” is a global script. In it, the target positions are stored vertically and horizontally. But it gave an error, so it is obvious that nothing will work like this.

You should only set the velocity from within the character’s _physics_process() method because otherwise it will be reset on each frame.
For now I believe you want to teleport the Frisk player to a specific point.
$Frisk.position = Vector2(SceneChange.target_tp_x, SceneChange.target_tp_y)
Right now I assume the player character is a CharacterBody2D

But it gave an error, so it is obvious that nothing will work like this.

Could you post the error so I am able to see what’s up? It can also be that SceneChange.target_tp_x is null which would mean that your position saving code is faulty.

E 0:00:05:0247 frisk_home.tscn::GDScript_j1eee:10 @ _on_body_entered(): Removing a CollisionObject node during a physics callback is not allowed and will cause undesired behavior. Remove with call_deferred() instead.

Can I ask you to post the code for the _on_body_entered() method? I believe the method is trying to alter the physic collision characteristic of a body while it’s still processing.

This is a global script. There are just variables here.

extends Node

var target_tp_x
var target_tp_y
var player_teleported

This is what the trigger does to transport the ace to another location. (yes, I haven’t come up with a proper name for the target location yet)

extends Area2D



func _on_body_entered(body):
	if body.name=="Frisk":
		SceneChange.target_tp_x=168
		SceneChange.target_tp_y=176
		SceneChange.player_teleported=true
		get_tree().change_scene_to_file("res://Scenes/a.tscn")

And this is the only function of the location to which the player is transferred.

func _physics_process(_delta):
		if SceneChange.player_teleported==true:
			$Frisk.velocity.x=SceneChange.target_tp_x
			$Frisk.velocity.x=SceneChange.target_tp_y
			SceneChange.player_teleported=false

I also tried to use
_ready():
Nothing came of it.

I would propose a bit of a change in logic

# SceneChange
extends Node

var target_tp: Vector2
var player_teleported: bool

func scene_transition(tp_position: Vector2) -> void:
    target_tp = tp_position
    player_teleported = true
    get_tree().change_scene_to_file.bind("res://Scenes/a.tscn").call_deferred()
# frisk_home
extends Area2D

func _on_body_entered(body):
	if body.name=="Frisk":
		SceneChange.scene_transition(Vector2(168, 176))
# player script
func _ready():
	if SceneChange.player_teleported:
		position = SceneChange.target_tp
		SceneChange.player_teleported = false

Does something like this work?

Yes! :smiley: The player is transported to the desired location. But in the console it gives two errors. “E 0:00:04:0827 scene_change.gd:9 @ scene_transition(): This function can’t be used during the in/out signal.”
and
“E 0:00:04:0827 scene_change.gd:9 @ scene_transition(): Removing a CollisionObject node during a physics callback is not allowed and will cause undesired behavior. Remove with call_deferred() instead.”

But nevertheless, nothing bad happens to the game itself. So I guess I can just leave it at that.

This error is caused by the presence of this line:
get_tree().change_scene_to_file("res://Scenes/a.tscn")
Do you really need it? I guess you are in this scene right now

Could I ask you to change the line
get_tree().change_scene_to_file("res://Scenes/a.tscn")
into
get_tree().change_scene_to_file.bind("res://Scenes/a.tscn").call_deferred()

I am currently trying a workaround as proposed in Errors when changing scene using change_scene_to_file() · Issue #85852 · godotengine/godot · GitHub

1 Like

No errors now👍

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.