in my project, i tried to save andload player position. However i found that player position keeps changing between every single frame. And what is weird is that one of the two kinds of position data is real position while another one is fake. If player is in the main scene, there would be a fixed difference between the true one and the fake one, but in other scenes it would be a random difference.
here is the debug output and the player script is below. I really wonder what's wrong.

`extends CharacterBody2D

@onready var menu = get_node("Menu")
@onready var timer: Timer = $Timer

const SPEED = 10000.0
const JUMP_VELOCITY = -400.0

var run_times = 1

#if menu:
	#print("Menu node successfully retrieved!")
#else:
	#print("Menu node not found")

func _process(delta: float) -> void:
var local_pos = position
var global_pos = global_position

print("Local Position: ", local_pos, "Global Position: ", global_pos)

func _physics_process(delta: float) -> void:

对话等情况下禁用移动

if Global.is_dia_active == true:
velocity = Vector2(0,0)
return

# 检查是否按住shift键来改变奔跑速度
if Input.is_action_pressed("run"):
	run_times = 1.5
else:
	run_times = 1.0
	
var direction = Input.get_vector("left","right","up","down")

if direction:
	velocity = direction * SPEED * run_times * delta
else:
	velocity.x = move_toward(velocity.x,0,SPEED * delta)
	velocity.y = move_toward(velocity.y,0,SPEED * delta)
move_and_slide()

func _input(event: InputEvent) -> void:

if event.is_action_pressed("menu"):
	if menu:  # 检查 menu 是否存在
		#print("Menu visibility: ", menu.visible)  # 输出当前可视性
		if not menu.visible:  # 使用 not 进行检查
			#print("show menu")
			menu.show_menu()
		else:
			#print("hide menu")
			menu.hide_menu()
	else:
		print("Menu node not found during input")

`
if more script code is needed plz tell me

  • archetypeaki When you set up a global script, the node will be instantiated at startup and added to the scene. So if there is player node already in the scene, you'll have two instances of that script running. Global scripts are not meant to be instantiated manually in the editor.

there seems to be a problem about the code form......

  • xyz replied to this.

    archetypeaki There may be two instances of the script running. When printing positions, print self as well to determine which node is running the script.

      xyz thanks! i ll post code again. i tried to use self.position but the output didnt change anyway 🙁

      • xyz replied to this.

        here's the code

        extends CharacterBody2D
        
        @onready var menu = get_node("Menu")
        @onready var timer: Timer = $Timer
        
        const SPEED = 10000.0
        const JUMP_VELOCITY = -400.0
        
        var run_times = 1
        
        
        
        func _process(delta: float) -> void:
        	# position debug
        	var local_pos = self.position
        	var global_pos = self.global_position
        	print("Local Position: ", local_pos, "Global Position: ", global_pos)
        
        
        func _physics_process(delta: float) -> void:
        	# disable movement in some condition
        	if Global.is_dia_active == true:
        		velocity = Vector2(0,0)
        		return
        
        
        	# change to run speed if shift pressed
        	if Input.is_action_pressed("run"):
        		run_times = 1.5
        	else:
        		run_times = 1.0
        		
        	var direction = Input.get_vector("left","right","up","down")
        	
        	if direction:
        		velocity = direction * SPEED * run_times * delta
        	else:
        		velocity.x = move_toward(velocity.x,0,SPEED * delta)
        		velocity.y = move_toward(velocity.y,0,SPEED * delta)
        	move_and_slide()
        		
        func _input(event: InputEvent) -> void:
        	if event.is_action_pressed("menu"):
        		if menu:
        			if not menu.visible:
        				#print("show menu")
        				menu.show_menu()
        			else:
        				#print("hide menu")
        				menu.hide_menu()
        		else:
        			print("Menu node not found during input")

        archetypeaki No, not self.position. That's the same as what you're currently doing. Print only self, or better yet self.get_path()

          xyz I just found out that this is somehow because I set the player script as a global script and as I delete it from global auto load, this bug disappeared. I wonder if setting player script as global script is a bad action?

          xyz And u were right about the 'there's two instance' stuff. Thanks a lot 🙂

          • xyz replied to this.

            archetypeaki When you set up a global script, the node will be instantiated at startup and added to the scene. So if there is player node already in the scene, you'll have two instances of that script running. Global scripts are not meant to be instantiated manually in the editor.