How do I store city distances?

So I made a flat map of “cities” with x amd y points for locations.

I have a way to calculate the distances between the cities to use later in my game (using pythagoras theorem).

But what is the best way to store these distances?

I want to later be able to ask the game “what is the distance between these 2 cities” and it returns the answer.

How should I store this information? would an array work?

Why not store the cities themselves and query them? All permutations of 50 cities would result in over 1000 results. Any reason you cannot simply ask for this data dynamically? Asking as you may have your reasons, ofc.


What are cities in this scenario? Just coords? No names? No objects?

It will be used a lot for the pathfinding, so I assumed it was better to process the distance calculations one time and store them.

Cities are just randomly placed cubes in the world with x and y coordinates (no z coordinates, but I might add that later for city height!). I can get their x and y coordintes that part is easy, just storing that information with their distance in a way that I can query is the struggle…

How about this:

You can use actors / tags / names as keys. This can be dynamic and all you need is a list. The list can be a *.csv file imported into a Data Table, a manual Data Table or punch the data directly if you have the patience. Adding a city to such a list at runtime could look like so:

image


But sure, you can pre-calculate it, too. I’d still use a map - so you can refer to it by name:

Which would print:


LogBlueprintUserMessages: [Ship_Player_C_1] OsloOslo -> 0
LogBlueprintUserMessages: [Ship_Player_C_1] OsloMogadishu -> 67.742
LogBlueprintUserMessages: [Ship_Player_C_1] OsloLondon -> 13.454
LogBlueprintUserMessages: [Ship_Player_C_1] OsloHanoi -> 102.694
LogBlueprintUserMessages: [Ship_Player_C_1] OsloPrague -> 10.77
LogBlueprintUserMessages: [Ship_Player_C_1] MogadishuOslo -> 67.742
LogBlueprintUserMessages: [Ship_Player_C_1] MogadishuMogadishu -> 0
LogBlueprintUserMessages: [Ship_Player_C_1] MogadishuLondon -> 66.528
LogBlueprintUserMessages: [Ship_Player_C_1] MogadishuHanoi -> 62.936
LogBlueprintUserMessages: [Ship_Player_C_1] MogadishuPrague -> 57.14
LogBlueprintUserMessages: [Ship_Player_C_1] LondonOslo -> 13.454
LogBlueprintUserMessages: [Ship_Player_C_1] LondonMogadishu -> 66.528
LogBlueprintUserMessages: [Ship_Player_C_1] LondonLondon -> 0
LogBlueprintUserMessages: [Ship_Player_C_1] LondonHanoi -> 109.202
LogBlueprintUserMessages: [Ship_Player_C_1] LondonPrague -> 14.036
LogBlueprintUserMessages: [Ship_Player_C_1] HanoiOslo -> 102.694
LogBlueprintUserMessages: [Ship_Player_C_1] HanoiMogadishu -> 62.936
LogBlueprintUserMessages: [Ship_Player_C_1] HanoiLondon -> 109.202
LogBlueprintUserMessages: [Ship_Player_C_1] HanoiHanoi -> 0
LogBlueprintUserMessages: [Ship_Player_C_1] HanoiPrague -> 95.509
LogBlueprintUserMessages: [Ship_Player_C_1] PragueOslo -> 10.77
LogBlueprintUserMessages: [Ship_Player_C_1] PragueMogadishu -> 57.14
LogBlueprintUserMessages: [Ship_Player_C_1] PragueLondon -> 14.036
LogBlueprintUserMessages: [Ship_Player_C_1] PragueHanoi -> 95.509
LogBlueprintUserMessages: [Ship_Player_C_1] PraguePrague -> 0

You may need to filter out same city results, ofc. The script can be improved, it was just a quick example.


but I might add that later for city height!

image

This then instead. Better make this choice now or you’ll need to re-factor lots of stuff later.

1 Like

Thank you! The concept of maps was the piece I was missing!

1 Like

There are also Sets. This way you only store the city and fetch its location from the world or its variables.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.