No problem! I'm glad I was able to help :smile:
I don't recognize the tween.connect("tween_completed", self, "_on_tween_completed")
method.
This connects the tween_completed
signal to a function defined in the script (hence self
) called _on_tween_completed
. I added this because I couldn't find a good way to detect if the Tween node was moving the player or not without connecting the signal.
The documentation has a page about Signals that might be worth looking at, though I have not looked through it myself so I cannot say how useful it will be.
A brief (and probably terrible) explanation of how signals work:
Signals are emitted by nodes when something happens. You can connect a signal to a function so that when the signal is emitted, the connected function is called. You can use this to basically set up the code so that "when X happens, call function Y", which allows you to organize your code. Some things can only be determined by the signals the node emits, which can make things tricky.
Some signals pass variables containing information related to the action that caused the signal to be emitted. In the case of the code, the tween_completed
signal is emitted when the Tween node is no longer changing a value, and it passes a reference to the object that was having its value changed, along with a NodePath containing the name of the changed value.
(Let me know if this does not make sense and I'll try to reword it and/or find better references.)
on the _on_tween_completed function I don't understand what the nodepath_key == ":position"
comparation does.
What this comparison is doing is checking to see if the value the Tween node just finished changing is the position
of the player.
This isn't necessarily needed if you only use the Tween node for moving the player, but I wanted to add a safety check of sorts by only setting is_player_moving
to false
when the position is no longer being changed, just in case you need to use the Tween node later for something else.
To be honest, I'm not 100% sure why the nodepath key is equal to :position
instead of position
. I'm guessing that if the node was not the player, it would be something like node_name:position
, so maybe that is why, but I do not know for sure. I just printed nodepath_key
and made the comparison based on the printed value to be on the safe side :smile: