Rodrigo Ponce
Rodrigo Ponce

Reputation: 37

Godot/GDScript Grid Movement Tweening

So I've been using KidsCanCode's Godot tutorials/documentation to help me create a Pokemon-like grid-based movement for a project I'm working on. For all intents and purposes, I would like to create a movement system as close to that in the earlier handheld Pokemon games as possible.

I would like to add two things before I start; one, I have grown fond of the way KidsCanCode attempted to teach grid-based movement, so while other ways of coding it may be simpler such as those that can be found on videos such as this one (https://www.youtube.com/watch?v=jSv5sGpnFso), I would like to hard-headidly stick to this method of coding it... you'll see what I mean when you read the code. Lastly, I would like to add that I had this code working before ! I actually haven't made any changes to the code since it was last working, however, for some reason it no longer seems to work, I'm not sure if that's due to Godot updating since, but hopefully someone can help me out with that.

So first of all, this is my player scene node tree. The most important parts of this being the RayCast2D and Tween nodes.

And this is my code for the main Area2D Player node:

extends Area2D

const tile_size = 16
export var speed = 5

var inputs = { "ui_right": Vector2.RIGHT,
            "ui_left": Vector2.LEFT,
            "ui_up": Vector2.UP,
            "ui_down": Vector2.DOWN }

func _ready():
    position = position.snapped(Vector2.ONE * tile_size/2)

func _unhandled_input(event):
    if $Tween.is_active():
        return
    for dir in inputs.keys():
        if event.is_action_pressed(dir):
            move(inputs[dir])

func move(dir):
    $RayCast2D.cast_to = inputs[dir] * tile_size
    $RayCast2D.force_raycast_update()
    if !$RayCast2D.is_colliding():
        move_tween(dir)

func move_tween(dir):
    $Tween.interpolate_property(self, "position", position,
        position + inputs[dir] * tile_size, 1.0/speed, Tween.TRANS_SINE, Tween.EASE_IN_OUT)
    $Tween.start()

To quickly explain, func _ready(): snaps the player to the grid. func _unhandled_input(event): then checks to see if a Tween is occurring, and if not, calls func move(dir). This function raycasts to the given direction input, forces a raycast update, and if no static body is in the given direction, calls func move_tween(dir). This last functions handles tween interpolation to the given direction and starts the tweening process. That's pretty much it. Once again, this used to work just fine.

However, now when I try to run this, I get an error "Invalid get index '(0, 1)' (on base: 'Dictionary')" where "(0, 1)" changes based on what direction I tried to move in when the game was running.

In the Debugger dock, underneath Stack Frames, it gives me errors on lines "22 - at function; move" $RayCast2D.cast_to = inputs[dir] * tile_size and "19 - at function: _unhandled_input" move(inputs[dir]).

The code on the website had these say (dir) only instead of (inputs[dir]). But doing so only gives me another error. If anyone smarter than me has any idea what's going on, I would very much appreciate any and all insight. Thank you !

Upvotes: 1

Views: 1239

Answers (1)

Theraot
Theraot

Reputation: 40295

Understanding the problem

Alright, let us see. The variable inputs has your dictionary:

var inputs = { "ui_right": Vector2.RIGHT,
            "ui_left": Vector2.LEFT,
            "ui_up": Vector2.UP,
            "ui_down": Vector2.DOWN }

The keys are String, and the values are Vector2.

Thus, here:

    for dir in inputs.keys():
        if event.is_action_pressed(dir):
            move(inputs[dir])

The variable dir is going to be a String. Which is what you need for is_action_pressed, so that is correct.

And inputs[dir] is going to be a Vector2. Which means that in move you are getting a Vector2 as argument.

Now, in move you say::

func move(dir):
    $RayCast2D.cast_to = inputs[dir] * tile_size

But remember that the argument you are passing is a Vector2, and the keys of input are all String. So it fails here: inputs[dir].


Early warning for similar problems

Using types can help you identify this kind of problems early. Sadly in Godot 3.x there is no way to specify the the keys and values of a Dictionary.

Arguably you could use C# and use .NET Dictionary<TKey,TValue> from the System.Collections.Generic, which would let you specify the key and value types. Yet, we are not talking about those dictionaries here.

What you can tell with GDScript is that your parameters are either Vector2:

func move(displacement:Vector2):
    # …

Or String

func move(dir:String):
    # …

This way Godot can tell you when you are calling them with the wrong parameter.


Another thing that will help. Although it is more on the discipline side, is to keep consistent names. If the names you use have a concrete meaning in your system, they will help you.

For instance, you call move like this:

move(inputs[dir])

Meaning that what you are passing is not called dir※. But you have move defined like this:

func move(dir):
    # …

So move expects something you call a dir. And you would see that when you are typing the call to move.

※: I'd say you are passing one of the values of inputs, so what you are passing is called an input. Or you could call them action, given that you use them in is_action_pressed. Which, again, would be using names in a way that helps you.


Solving the problem

The way I would solve this is by using the String and inputs in _unhandled_input only (after all, that function is meant to deal with inputs). And work with Vector2 from there on. This means that:

  • The other methods would also be useful if in the future you wanted a movement that does not come from one of the inputs.
  • You are not repeating the effort of looking up in the Dictionary.

Admittedly, these aren't a huge deal for your game right now. And ultimately what you do is up to you. Yet, consider this approach submitted to your consideration.

This is the code (I have added some type annotations):

extends Area2D

const tile_size:float = 16
export var speed:float = 5

var inputs = { "ui_right": Vector2.RIGHT,
            "ui_left": Vector2.LEFT,
            "ui_up": Vector2.UP,
            "ui_down": Vector2.DOWN }

func _ready():
    position = position.snapped(Vector2.ONE * tile_size/2)

func _unhandled_input(event:InputEvent) -> void:
    if $Tween.is_active():
        return
    for dir in inputs.keys():
        if event.is_action_pressed(dir):
            move(inputs[dir])

func move(displacement:Vector2) -> void:
    $RayCast2D.cast_to = displacement * tile_size
    $RayCast2D.force_raycast_update()
    if !$RayCast2D.is_colliding():
        move_tween(displacement)

func move_tween(displacement:Vector2) -> void:
    $Tween.interpolate_property(self, "position", position,
        position + displacement * tile_size, 1.0/speed, Tween.TRANS_SINE, Tween.EASE_IN_OUT)
    $Tween.start()

Or you can using String thought out, and querying the dictionary every time. Which, I believe, is what you intended. Like this:

extends Area2D

const tile_size:float = 16
export var speed:float = 5

var inputs = { "ui_right": Vector2.RIGHT,
            "ui_left": Vector2.LEFT,
            "ui_up": Vector2.UP,
            "ui_down": Vector2.DOWN }

func _ready():
    position = position.snapped(Vector2.ONE * tile_size/2)

func _unhandled_input(event:InputEvent) -> void:
    if $Tween.is_active():
        return
    for dir in inputs.keys():
        if event.is_action_pressed(dir):
            move(dir)

func move(dir:String) -> void:
    $RayCast2D.cast_to = input[dir] * tile_size
    $RayCast2D.force_raycast_update()
    if !$RayCast2D.is_colliding():
        move_tween(dir)

func move_tween(dir:String) -> void:
    $Tween.interpolate_property(self, "position", position,
        position + input[dir] * tile_size, 1.0/speed, Tween.TRANS_SINE, Tween.EASE_IN_OUT)
    $Tween.start()

Notice here that _unhandled_input is passing dir to move. The same way that move is passing dir to move_tween.

Upvotes: 1

Related Questions