Godot Version
Godot 4.2.2
Question
I have set the player position when the button is clicked and when I press on go back, it cannot walk. I have set the Global variable to check whether the button is clicked.
1st_floor.gd
@onready var paper_scene = load("res://parallax_gameplay/paper_scene/paper_scene.tscn").instantiate()
func _ready():
GlobalPaperCounter.counter = 1
add_child(paper_scene)
paper_scene.set_visible(false)
func _process(_delta):
GlobalDirection.update_scene("1st_floor")
func _on_paper_pressed():
GlobalDirection.update_player_position($Player.global_position.x, $Player.global_position.y )
GlobalDirection.update_paper_scene(true)
if GlobalPaperCounter.counter == 5 and GlobalDragAndDrop.Paper1 == false and GlobalDragAndDrop.Paper2 == false and GlobalDragAndDrop.Paper3 == false and GlobalDragAndDrop.Paper4 == false and GlobalDragAndDrop.Paper5 == false:
pass
else :
paper_scene.set_visible(true)
Player.gd
@export var speed = 200
@export var paper_scene = false
func _ready():
## Set player direction from GlobalDirection when changing to previous scene
if GlobalDirection.player_direction == 1:
$AnimatedSprite2D.flip_h = false
elif GlobalDirection.player_direction == -1:
$AnimatedSprite2D.flip_h = true
## Player cannot move until the dialogue is end.
## @tutorial: https://forum.godotengine.org/t/player-is-able-to-move-while-dialog-is-running/80815/4?fbclid=IwY2xjawIeq2RleHRuA2FlbQIxMAABHWxapb7LmNPThKGUi0aGpv5VAx1klHjEnHtFqgCHiz4ZKjTRuubKV0oZXA_aem_BjXf78ZqB3NMFBRg14sYFA
Dialogic.timeline_started.connect(set_process.bind(false))
Dialogic.timeline_started.connect(set_process_input.bind(false))
Dialogic.timeline_ended.connect(set_process.bind(true))
Dialogic.timeline_ended.connect(set_process_input.bind(true))
## Set player animation and speed
## If the paper_counter is clicked, the player cannot walk.
func _process(delta):
paper_scene = GlobalDirection.paper_scene
if paper_scene == false:
velocity = Vector2.ZERO
var x_input = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
if GlobalDirection.come_back == true:
self.global_position.x = GlobalDirection.player_position_x
self.global_position.y = GlobalDirection.player_position_y
move_and_slide()
if x_input != 0:
print("walk")
$AnimatedSprite2D.play("walk")
velocity.x += x_input * speed
## movement speed per frame
position += velocity * delta
## Update direction in GlobalDirection
if x_input > 0:
GlobalDirection.player_direction = 1
$AnimatedSprite2D.flip_h = false
elif x_input < 0:
GlobalDirection.player_direction = -1
$AnimatedSprite2D.flip_h = true
## Player is idle.
else:
$AnimatedSprite2D.play("idle")
elif paper_scene == true :
self.position = Vector2(0,0)
set_process.bind(false)
set_process_input.bind(false)
## Change camera2D based on the current scene
## @tutorial: https://youtu.be/v3v01p26NP0?si=S5mkc9jqiJwklMV7
func current_camera():
if GlobalDirection.current_scene == "1st_floor":
$first_camera.enabled = true
$second_camera.enabled = false
if GlobalDirection.current_scene == "2nd_floor":
$first_camera.enabled = false
$second_camera.enabled = true
Paperscene.gd
func _on_go_back_pressed():
GlobalDirection.update_scene("1st_floor")
GlobalDirection.update_paper_scene(false)
GlobalDirection.update_come_back(true)
self.set_visible(false)
GlobalDirection.gd
extends Node
var player_direction = 1
var current_scene = ""
var paper_scene = false
var player_position_x = 0
var player_position_y = 0
var come_back = false
func update_scene(scene:String):
current_scene = scene
func update_paper_scene(check:bool):
paper_scene = check
func update_player_position(x:int,y:int):
player_position_x = x
player_position_y = y
func update_come_back(check:bool):
come_back = check