[SOLVED] Specific NPC behaviour not executed in full - possibly boolean switch..


  • *
  • Posts: 89
Hi folks,

I'm working on a prototype of a top-down RPG set in medieval North Asia, with focus on tactical problem-solving (varied combat options vs AI manipulation - avatar will be able to play a tune that changes the NPC behaviour patterns - cautious vs reckless, peaceful vs aggressive). Currently finishing the core of combat AI. The artwork is currently placeholder opensource tileset, and the level just a testing ground for the mechanics :)

While putting together Main AI Switch (the "updating" event, which manages NPC behavior), I ran into a problem.

Expected behavior: When the player attacks the NPC outside the NPC’s detection range, the NPC should stop its current “Wander” behavior, switch to “PullToAvatar” (move in a straight line towards the avatar), then if the NPC collides head-on with a Tile, switch to “PathfindingToAvatar” (simple movement on a tile-based path created with the AI Tools extension). Once “PathfindingToAvatar” is initiated, it should switch the “path_initiated” boolean attribute ON, and there is a special block in the “Main AI Switch” event (see the first “Otherwise if” block that initiates the “PathfindingToAvatar” event) that ensures that while “path_initiated” = TRUE or “path_to_avatar” list  contains any items, the NPC executes the “PathfindingToAvatar” event until reaching the end of the path or until getting within the weapon range of the player.

Current state: either the NPC doesn't move on the path at all OR the NPC executes pathfinding once but for new instances of pathfinding it just stands facing a tile. Please also note that in the “Pull path” video the number of remaining "steps" to travel is changing as the avatar moves on the scene, which is incorrect behaviour. It means that the pathfinding movement cycle is not properly initiated. Please also note that the pathfinding algorithm itself used to work perfectly in my previous (less complex) versions of NPC AI. I suspect there is a fault in my Boolean switches and their resets (or in how the engine handles the triggered events, maybe it doesn’t get enough time to turn the “path_initiated” switch ON and executes the “Main AI Switch” event again?

Please see an export of the game, a few screenshots and two videos here: https://www.dropbox.com/sh/dxn9cbjddmnzg87/AAAmbxwa3dEHkA6iVMW9mLtRa?dl=0

If you import the game, please note that controls are currently keyboard only: arrows or WASD for movement, space bar for attacking (single press for normal attack, very long press for aimed attack), tab and arrows for changing weapon (press tab again to close the weapon selection menu).

I’ll appreciate any leads, this bug has been really frustrating over the last 2-3 days...
Thank you!

« Last Edit: January 14, 2019, 06:47:56 pm by dtishin »


  • *
  • Posts: 2742
Please also note that in the “Pull path” video the number of remaining "steps" to travel is changing as the avatar moves on the scene, which is incorrect behaviour.
I don't think that's incorrect behavior by your description. It hit a tile, so it turns on your pathfinding and is constantly calculating the number of steps as the player actor moves. It appears to be calculating the path by the video, it's just not executing your movement code, or executing only for one frame, or something is turning your pathfind on and off constantly. Could also be a conflict between multiple movement events.

You seem to know where the problem could be. Your best option for debugging is to place print blocks for every variable in an if statement that you suspect is not executing, as well as print block inside the if statement in case it executes. If any variable is not the expected value, go to all the points where you change that variable, and see if any of them is changing when it shouldn't. And while doing that, retrace the steps to see if your code is doing what you think it should be doing. Also, disable all events not related to what you are debugging, to isolate it. If your code works, enable and test the events one by one until you find which one causes the issue (you can disable events by unchecking the check box by its name).
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.


  • *
  • Posts: 89
Thanks for your advice! Pathfinding is now fixed and expanded. I've implemented dynamic pathfinding, plus NPCs now run away from bombs and from the player when badly hurt (actual threshold HP depends on the current 'behaviour' profile of the NPC).

The main thing was splitting pathfinding into two separate events - 1) an Updating event that watches for changes of the player or the NPC's tile, and 2) a custom event launched from the Main AI Switch (essentially a behaviour tree). I also dropped the "direct motion to the player" behaviour since dynamic pathfinding is much handier anyway; I didn't need the check for collisions this way either. You can see code screenshots attached and a video of updated game mechanics under the URL :) https://www.dropbox.com/s/7dbsmfu2x9obnec/Updated%20pathfinding.mp4?dl=0

Thanks again!