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