0 votes

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.

in Engine by (30 points)
edited by

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

What do you mean by keep track of the units?

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

1 Answer

+1 vote
Best answer

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.

by (5,183 points)
selected by

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:
elif map[2,1].unit.actor_type == "player"
elif map[2,1].unit.actor_type == "enemy"
    (cancel movement)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.