The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

I have a variable that keeps track of which direction my player is facing. I want this variable to be at a different default values depending of the current level being played.
For example,in Level 1 he faces left by default,but I might want him to face right in Level 2.

Here's what I tried to do:

extends Node2D

func _ready():
    var facing_x = $KinematicBody2D.get("facing_x")
    facing_x = -1

This is the node of the game's level,in case the information is relevant.

in Engine by (19 points)

2 Answers

+2 votes

Hi, actually, get will only give you the value of that variable. Modifying it wont do anything to the original variable.

you should try something like

extends Node2D

func _ready():
    $KinematicBody2D.facing_x = -1
by (3,505 points)
+1 vote

Your variable is defined in your player script that extends a KinematicBody2D, right?

I think you should access it by simply using dot notation.
eg:

onready var player = $Player_node
func _ready():
    var facing_x = player.facing_x  #copy value from node
    ...
    facing_x = -1 #change variable in function
    player.facing_x = -1 #change value in node

If you changefacing_x by assigning a new value, it happens only locally (within the _ready function but does not apply to the actual Player node).

Anyway if you only need to change the sprite you could simply use flip_h().

by (291 points)

Thank you very much,this solved the problem.

I have a variable that keeps track of what direction the player is facing not only to set animations,but also because I intend to add a dash feature to my game,and want it to happen in the direction the player is currently facing,so flip_h wouldn't be enough.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.