![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Wraith1978 |
In the http://docs.godotengine.org/en/3.0/getting_started/step_by_step/your_first_game.html tutorial, the _process function begins with the line:
velocity = Vector2()
but the beginning of the script already declares the variable and ?sets its type? outside of the function with the line:
var velocity = Vector2()
I am having trouble understanding what this line in the function is accomplishing. I know it is necessary, because the behaviour of the object is drastically changed when I comment it out. I am sure there is something profound that I am missing here that will cause me further problems as I move forward. I have a suspicion that this is something to do with the “dynamic typed language” of GDScript, but ANY guidance is definitely appreciated.
Thanks
extends Area2D
export (int) var SPEED # how fast the player will move (px/sec)
var velocity = Vector2() # the player's movement vector
var screensize # size of the game window
func _ready():
# Called every time the node is added to the scene.
# Initialization here
screensize = get_viewport_rect().size
func _process(delta):
# Called every frame. Delta is time since last frame.
# Update game logic here.
velocity = Vector2()
if Input.is_action_pressed("ui_right"):
velocity.x += 1
if Input.is_action_pressed("ui_left"):
velocity.x -= 1
if Input.is_action_pressed("ui_down"):
velocity.y += 1
if Input.is_action_pressed("ui_up"):
velocity.y -= 1
if velocity.length() > 0:
velocity = velocity.normalized() * SPEED
$AnimatedSprite.play()
else:
$AnimatedSprite.stop()
position += velocity * delta
position.x = clamp(position.x, 0, screensize.x)
position.y = clamp(position.y, 0, screensize.y)