Speeding up numbering of neighbors neighbors

Hello,

For the a* pathfinding that i created i need a way to speed this part up(code below)), if there is a faster way. I am making the pathfinding for a TBS(turn based strategy) game, where i want to show the movement tiles according to movement points.

I got this all working, but at first, the logic usage went to the roof, 95-98% reducing the frame rate to around 11 fps. I optimised the tiles that are searched. Now the logic is around 20%, a huge increase in frames 60 fps stable.

this all is with a grid spawned out of hexagons (2x2m) total of: 2880 tiles: active 2267 / blocked 613
brings up the physics quite a bit as well 11-20%.

anyway, iâ€™m looking for a way to speed this part up, if it can be any faster/lighter, because this part is actually eating all the logic (code runs in true pulse).

tiles_in_range = kdtree

``````    own['start_tile']['number'] = 1

i = 1

while i < len(own['tiles_in_range']):

for co, index, dist in own['tiles_in_range']:

tile = own['limited_tile_list'][index]

#            if tile == own['end_tile']:
#                break

if tile['number'] == i:

for neighbor in tile['neighbors']:

if neighbor['number'] == 0 and neighbor['active_tile']:
neighbor['number'] = i+1

i += 1
``````

#edit a video:

• green are tiles allowed to move
• red-/ tiles that are selected and canâ€™t move to (not enough movement points)
• dark red = area outside of the search range (to give me a visual border)
• everything within the dark red tiles is the search/pathing area
2 Likes

hello,
nice job, nice feature for a turn based game

may be your â€śwhile loopâ€ť is against the â€śtic rateâ€ť
some suggest to replace the â€śwhile loopâ€ť with an â€śif statementâ€ť

2 Likes

Great game! This can be great for a turn based game. Though, what is the goal for this. Is it like a escape some room game and the first person to escape wins? Or something else?

1 Like

nah itâ€™s not against the ticrate, a while loop executes in 1 frame, so everything within will be executed before the frame goes to the next one. An if statement canâ€™t be run because it will get to an hold after the first tile, because i resets and so far i know you canâ€™t count numbers with an if statement.

@Aj8841
Itâ€™s not a game, itâ€™s only a pathing mechanism.

The type of game that i am after is like: For the kings.

2 Likes

Ok figured it out, redone the structure of the script, rewrote some functions and here is the result:
(still using the while loop, canâ€™t get it to work without)

Optimized even further. 4 tiles movement from Â± 20% logic usage to Â±5 % logic usage. From 10 movement @ 50%+ to Â± 11%.

going to limit the max to 8 i think, 6 rolled by dice and 2 as character upgrades(passive/skils/talent/etc) i think. Anyway I am very happy with the result.

2 Likes

the new path system is eye catching
again, good job

really like the numbers showed on the path and ascending colors

1 Like

Thanks!

Imagine the colors as border only, with a bloom/glow shader, would be neat.

The numbers where a bit tricky, adding text objects on the fly in the loops took a bit of performace, so i simply parented a text object to the tile, spawn the tile and text object is there, then just when the tile is in the path, activate visibility little to no performance cost, and what is not visible wonâ€™t be rendered as well.

To be honest i thought blender would get into trouble, because tiles+text = 5760 objects but itâ€™s actually not a problem.