Problems with tracking instance states

0 favourites
  • 8 posts
  • Hello everyone,

    I am encountering an amazingly frustrating problem that just has me ripping my hair out. It's bit complex so I will try to summarize.

    I have a large number of instances of the same EnemyMonster.

    The EnemyMonster has two instance variables important to this:

    1) Behavior - an int 0, 1 or 2

    2) searching - a bool

    I also have a global variable called "whoSearching"

    The behaviors are as follows

    0 - Idle

    1 - Attacking

    2 - Searching for target

    I am trying to track how many EnemyMonsters have Behavior set to 2, searching.

    All EnemyMonsters start with Behavior 0 and searching as False, and whoSearching starts at 0.

    When an EnemyMonster is set to Behavior 2 and its searching is False, I add 1 to whoSearching and set its searching to True so this will only run once for each EnemyMonster set to Behavior 2.

    When an EnemyMonster is set to Behavior 0 or 1 and its searching is True, I subtract 1 from whoSearching and set its searching to False.

    Being set to Behavior 2 is the only way to end up with searching as True on any given EnemyMonster, so it must pass through this state before being set to Behavior 1 or 0 has any effect on that instances searching bool or the whoSearching global.

    And yet I still get some instances ending up with searching as True without ever being set to Behavior 2 causing my count to be way off.

    I really do not understand why this is happening, it seems like such a simple concept but I just cannot seem to figure it out.

    If anyone has any ideas I would really appreciate it.

    And here is the .caproj for anyone who wants to check out the nitty-gritty details of my script, maybe I'm making a different mistake somewhere and it's effecting this.

    Thanks a lot for any help :)

  • Could you post .capx instead of .caproj? .caproj alone doesn't tell much :)

  • Ah, I see how that works now!

    sorry about that, I didn't know how to find the capx file.

  • It seems like it's a bit over complicated; few things:

    You can do stuff like this instead of keeping manually count of things:


    enemy.pickedcount = 0

    .. do stuff you do when no enemies are searching

    Also, 'trigger once while true' condition would be useful for you on few occasions. I'll modify your .capx a but and post it here later today if I got some time :)

  • Here you go, my take on what you did there:

    Wastes simplified

    I basically redid the whole thing, in my opinion at least it is a whole lot simpler to manage now :) I probably lost some of the functionalities you had there, but they should be pretty easy to add in there. I used only single variable for the AI state which makes it a lot cleaner, also I used picking objects bit more effectively than you did in the original. Hope this helps! :)

  • Thank you!

    Haven't had the chance to look it over yet (I'm still at work) but I will first thing when I get home.

    I had a feeling it was overcomplex as I added each new behavior I wanted over the previous stuff, but I was just too close to it all to see, thanks for the fresh eyes.

    I definitely did not know I could count the number of instances with a variable set to true! I look forward to learning how to do that from your changes to my script.

    I'm sure this will be very informative :)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Alright, I have had the chance to look it all over!

    Thanks a lot for taking the time to show me what's what :)

    While I didn't take your work verbatim, it did teach me what I needed to know to solve this problem and I really appreciate that.

    I am now able to move on with this project after being stalled for three days, I am in your debt.


  • No problems, your original approach was a bit 'coderish', C2 can do things a bit more efficiently at some part. Nice that I was able to help! :)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)