Boolean condition

  • So these are very simple problems I'm having I can't seem to figure out. (EDIT, FIXED! JUST THE SECOND PROBLEM) One, I'm trying to make is so if any instance has a boolean set, then an action. But the ANY factor is my problem. It works if ALL instances have it set, but not any.

    Second, I can't seem to trigger an action just once when a boolean is set. It triggers constantly. And to my knowledge the "Trigger once" condition is literally only one time, not once for each time the other condition becomes true.

    Any suggestions? Thanks

  • This code should work if ANY instance has boolean set:

    x----------------------------------x
    |Sprite -> Is boolean b set        |  do something
    |System- > Trigger once while true |
    x----------------------------------x[/code:1mpcjwqr]
    
    And it will only be triggered once. If after that you set boolean to other instances, it will not be triggered again.
    If you reset b to false for all instances, and then set one to true, it will be triggered again.
  • Thank you, but I have found a solution to the problem.

  • So do you still have the second problem with "Trigger once"? If you do, please share a screenshot of your events.

  • What I need help with may have changed a bit, ive been doing a lot to my project. So I highlighted in red boxes the events I'm having trouble with, I tried doing a function instead of just "when isSearching", This actually MAY work for me, but it ends up they "search" for me as soon as the layout starts because the trigger is simply when it can't see me, not after having seen me and then not being able to see me that they start searching.

  • There are many issues with your code..

    If enemy is not in range or has no LOS, all highlighted events are executed on every tick. This is clearly wrong.

    Also, you need to pick the correct enemy instance in your function, otherwise you are assigning isSearching=true for all enemies.

    "Wait 5 seconds" inside an event which is triggered on every tick also makes little sense.

    I would add a Timer behavior to enemy and do this:

    Enemy NOT overlapping NoticeRange   Function call "StartSearch" (parameter: Enemy.UID)
    and Enemy isSearching=false        
    
    Enemy NOT has LOS to Player   Function call "StartSearch"  (parameter: Enemy.UID)
    and Enemy isSearching=false        
    
    On function "StartSearch"
      Enemy pick by Unique ID = Function.Param(0)
         Enemy set isSearhing=true
         Enemy start timer "StopSearch" for 5 seconds (once)
         Enemy start timer "SearchForPlayer" for 0.3 seconds (recurring)
    
    Enemy On Timer "SearchForPlayer"
        For each Enemy   // as Timer may be triggered for several enemies at the same moment
              ............... find path etc.
    
    Enemy On Timer "StopSearch"
       Enemy set isSearching=false
       Enemy stop timer "SearchPlayer" 
    [/code:1j18ux2j]
  • Thank you for the help, I still do not know everything about C2. I added what you wrote, with a few extra things and it works great! I believe I've learned a bit from this and can reproduce in the future, thank you!

  • Uglypenguin

    I forgot one thing - in the first two events you need to add "For each enemy":

    Enemy NOT overlapping NoticeRange   
    and Enemy isSearching=false   
        For each enemy                    Function call "StartSearch" (parameter: Enemy.UID)
    [/code:10fox8yi]
  • I don't know what Ive done wrong. It was working but the more things I try and add to the AI the more it doesn't function correctly. I don't know if you could tackle trying to fix the entire AI for the enemy but some things just aren't working. let me know if I should send the capx, I can give a description of what exactly they are supposed to do.

  • Sure, if you share the capx here or PM me, I can take a look.

  • https://www.dropbox.com/s/0akbww8zm73pn ... .capx?dl=0

    Here it is. The Enemy is supposed to attack whichever enemy it "sees" or is in range of, after each attack moving to a place around his target and attacking again (surrounding) then being able to acquire a target of whomever he sees (CanSee, CanSeeNpc, enemy instance variables). If the player or NPC get out of range the enemy will search(isSearching), this is a function for each Enemy to go to its target plus in a random vicinity.

    Click on the NPC to have him follow if you wish when testing.

    The player can crouch (crl), sprint(shift), and attack(hold left click).

    The player can attack the enemy and kill him when its AttackRange sprite is overlapping him, it can do critical attacks, and loot the body.

    The enemy can attack the player and NPC when its AttackRange sprite is overlapping the player

    The issue seems to be the conditions, the triggers, my events not being right cause multiple instance variables are going off at the same time for all the enemies. I tried to organize it well so pieces of code are easier to find.

  • Your game looks great!

    However, it seems like it's going to be a big project and it's already quite messy. You need to get a few things right ASAP!

    1. First, start using families. I imagine wolf will not be the only enemy in your game. You don't want to duplicate hundreds of lines of your code when you add a new enemy type!

    Create families for all objects that can be logically combined - Enemies (include wolf, bear, zombie etc.), Friendlies (Player, NPC), SceneryObjects etc.

    Every time you add new events and feel like you may need to re-use them for a different object, consider creating a family.

    Note, that you can have the same object in several families.

    Say, wolves can be in Enemies family for all enemy-related things (attacking, pathfinding etc.) and in AliveObjects family (vulnerable to cold, leave blood splatter when killed etc.).

    You will need to move all instance variables and behaviors to family level. It's a time consuming task, but you need to do it now, otherwise it will take 10x more time later when your project grows bigger.

    Follow this tutorial:

    https://www.scirra.com/tutorials/535/ho ... o-a-family

    Also, if two objects are very similar (for example different types of trees), it's better to use one sprite with different animations.

    2. Use containers.

    If you add Enemy, EnemyRange, EnemyHealth to the same container, you'll only need to create an Enemy and pin EnemyRange and EnemyHealth.

    You don't need to worry about creating or destroying EnemyRange and EnemyHealth, it will be done automatically.

    You don't need to link them together using EnemyID variable. Every time you pick one object from the container, others will be picked automatically.

    For example:

    Bullet on collision with Enemy

    -> Subtract 1 from Enemy.health

    -> EnemyHealthBar set width to Enemy.health (you don't need to pick EnemyHealthBar by EnemyID)

    With families and containers you should be able to significantly optimize and de-clutter your code.

    3. Use different event sheets. 260 events in one sheet is a bit too much.

    I would definitely move all enemy AI stuff to a separate sheet.

    4. Create a new layout, name it Assets and dump all your objects to that layout. I'm talking about shadows, corpses, steps, range circles, text captions etc.

    C2 requires one instance of each object to be added to a layout, but it shouldn't necessarily be your main Game layout.

    Sorry, I didn't get to the AI part, I think you need to fix all these issues with your project first...

  • A few other thoughts -

    5. You can get rid of all range circles (PlayerRange, AttackRange, NoticeRange).

    Use instance variables and distance() expression instead.

    PIck Enemies if distance(Enemies.x, Enemies.y, Player.x, Player.y)<=Player.attackRange

    will pick all Enemies within the attack range from the player.

    6. Name your layers correctly and use layer names instead of numbers. If later you have to add another layer between 0 and 1, you'll not need to change layer number in all events.

    7. Use constants instead of "magic numbers".

    E.g. NoticeRange=MAX_NOTICE_RANGE, instead of NoticeRange=600

    See this post for more useful tips:

  • Thank you for the tips! I tried messing with containers once, what I tested didn't seem to work well so I didn't end up using it after that. I read about families and I think I know how it works now, what I don't understand yet is the containers.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • So I changed over the wolf AI to a family called wolf. Its... not working right though. I did nothing with containers yet. Do you want to take a look?

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