Checking node type from script

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JymWythawhy
:warning: Old Version Published before Godot 3 was released.

Say I had a map object that was a dictionary that mapped tile locations (Vector2’s) to Tile Objects, which would hold information about that tile location, like the Tile Type (wall, floor, lava, etc), and if it contained a Unit (which could be empty, or the Player, or an ogre/zombie/weasel/whatever).

How could I check the Map location to see if a unit was contained there (map[Vector2].Unit != nil ?), and then see what kind of Unit was contained there? For this scenario, I would probably have a base Actor class that the Player and Enemy type would inherit from.

So, to further explain my goal- say an Enemy was at spot 1,1. The AI tells him to move one square east, so he tries to move to 2,1. The movement script would first check to see if that square is occupied. If it is not occupied, then the Enemy would complete the movement from 1,1 (which would now be unoccupied) to 2,1 (which would now be occupied by this Enemy. If, instead, 2,1 was occupied by another Enemy, nothing would happen, and the AI would chose another direction to try to move. If, in the third case, the space was occupied by the Player, the Enemy would deal damage to the Player, but would not move into the Player’s spot.

I am having trouble understanding your question - I think.
Could you not keep track of units as they move, and then use that data?

Tybobobo | 2016-11-01 16:48

What do you mean by keep track of the units?

I’ve added some more explanation up above- does that make sense?

JymWythawhy | 2016-11-01 18:34

:bust_in_silhouette: Reply From: avencherus

Depending on your design and the performance impacts, you need to store a reference of the node somewhere.

If you’re just querying a tile directly, you probably want store a reference to the node that occupies it inside your dictionary.

If you’re asking on how to check what that object is once you have it, there are a variety of things you can. For built-in types there is typeof() and get_type(). You can manually store your own property for handling a type inside the classes. Or you can stash and retrieve and compare meta data from objects.

Basically the dictionary map would just be a data structure that would tell me information about each tile of the map (passable or not passable, lava or stone, etc) and would have a “slot” for a reference to a unit. The tile itself wouldn’t be a node- its just a data structure. Maybe I am not designing this in a very Godot-centric way- I am still getting used to it (but liking it).

So I could do something like this, if my actor script had a variable for actor type?

if map[2,1].unit == null:
    (move)
elif map[2,1].unit.actor_type == "player"
    (attack)
elif map[2,1].unit.actor_type == "enemy"
    (cancel movement)

JymWythawhy | 2016-11-02 13:20

Yeah that would be entirely fine, I’m sure you can see the results for yourself.

If you prefer to use numeric values you can also create your own globally scoped constants, like:

Somewhere global:

const PLAYER = 0
const ENEMY = 1

and then:

elif map[2,1].unit.type == PLAYER

The algorithms and data specific to your game are going to be exactly what they need to be. Godot gives you the space and tools to implement it.

avencherus | 2016-11-02 14:03