Stepan Michalek
Stepan Michalek

Reputation: 119

Can you activate a method on an Area2D node with the player's raycast2D in Godot?

I created a sort of old style jrpg movement system with a 3 character party. The first character (the player) uses a raycast for checking of surrounding tiles and movement.

I want the player to come close to a shop tile and hit a button to open a dialog. Player is in the middle, shop is above him, like so:

enter image description here

How would I activate a custom function on the shop tile?

I tried using raycasts and signals. The shop is an Area2D tile. Is it even possible to trigger an Area2D on_entered signal with a raycast? The default signals seem to be incorrect for this use, but so do custom signals.

There is probably something very simple I'am missing. Thanks for any help.

Upvotes: 0

Views: 379

Answers (1)

Camwin
Camwin

Reputation: 413

You could check what the raycast is colliding with. Since it looks like you are placing an Area2D on top of a TileMap, why not make the shop a StaticBody2D instead of a tile, and attach the shop script. It would be typical for game objects other than the walls to not be in the TileMap.

Somewhere with your player's movement code:

    if Input.is_action_just_pressed("interact"):
        var collider = raycast.get_collider()
        if collider != null:
            if collider.has_method("open_shop"):
                collider.call_deferred("open_shop")

I am assuming here that the raycast is properly updated already in your movement code, either by the raycast property enabled being true, or having a call to force_raycast_update().

Here, raycast should be replaced with any reference to the player's Raycast2D, and "open_shop" should be replaced with the function name (still as a string literal) from the shop script you wanted to call.

Upvotes: 0

Related Questions