How do I combine "And" and "Or" blocks in an event?

0 favourites
  • 11 posts
From the Asset Store
Publish and Subscribe to events in event sheets and from the scripting interface
  • I'd like to select 2 objects in the same event and was trying to do something similar to the following:

    (Cell.Column = GemX2 AND Cell.Row = GemY2) OR System.Pick.GemFamily.instance(0)

    But I'm finding that as soon as I include the "OR" into the event all the conditions use an "OR" operator. How do I accomplish selecting both a cell object and a Gem object using a pick index n and 2 local variable comparisons to gem instance variables so that the action has both the gem and the cell in scope?

    Thanks for your help!

    Ben

  • I wound up splitting the predicate from the first and condition from the predicates of the or condition into 2 event blocks and just storing the index of the first object in a local variable to be used in the second action of the second event. This seems like a hokey and cumbersome way to achieve what I'm trying to do. But pragmatically it works. I'd love to hear of a more elegant solution if someone has one.

    Thanks!

    Ben

  • I wound up splitting the predicate from the first and condition from the predicates of the or condition into 2 event blocks and just storing the index of the first object in a local variable to be used in the second action of the second event. This seems like a hokey and cumbersome way to achieve what I'm trying to do. But pragmatically it works. I'd love to hear of a more elegant solution if someone has one.

    Thanks!

    Ben

    Isn't what you require covered in THIS section of the manual?

  • I do see this text in the manual, "You can combine the block types by using sub-events. This allows you to build up more advanced logic, such as an 'Or' block followed by an 'And' block." But from my understanding the first condition will filter out what I'm trying to pick in the second condition. Maybe my understanding is wrong, but I'm thinking that what happens is that each event will select a group of objects and each sub event will select a group of objects selected from the parent event. Very much like method chaining in other programming languages. If this is the only way event/sub-event object scope works then I can't achieve what I'm trying to do. But if there's a way to get the parent to select from a global scope of all objects followed by the sub-event selecting from all global objects while not filtering out the selection from the parent object, then what I'm trying to do might work. I did notice the System.Pick events, which are probably what I need to achieve my goals. I'd love to understand this better, so please correct me of I'm wrong.

    Thanks,

    Ben

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Ok, I think I see a way to accomplish what I'm after without using the temp variables. I think the original logic here:

    (Cell.Column = GemX2 AND Cell.Row = GemY2) OR System.Pick.GemFamily.instance(0)

    can be rewritten to something like this:

    (Cell.Column & ", " & Cell.Row = GemX2 & ", " & GemY2) OR System.Pick.GemFamily.instance(0)

    This is still not a great solution as it's not as readable/maintainable as it could be. But it's better.

  • Your confusion is valid, since Construct 2 does not allow mixing of AND and OR together.

    Suppose you have to do something when [ A = true AND B = true ] OR C = true,

    My solution to the above problem, using Construct 2 event sheet, would be:

    ------------------

     A = true              [Do something]

     B = true

    ------------------

       | Else                [Do something]

       | C = true

       |---------------

  • I have similar problems... but i have two 'OR' conditions plus an 'AND'

    i'm new to construct 2 having previous experience in basic languajes.

    I can't belive that construct 2 is so easy to manage and make games really faster but a simple expression like this:

    'IF (a OR b) AND c' is giving me headaches to make it work (if it is possible, guess it is)

    Yes i know i have to RTFM just commenting... it's so frustating.

  • SERGIOManOwaR

    Use System-> Compare two values

    ((a | b) & c) equals 1

  • SERGIOManOwaR In this case, and due to how events work, have your main event be a OR block, and then have a sub-event checking C.

    In this sub-event, have your actions.

    The first event will be checked and if either the first or second condition is true, it will then check for the sub-event. If the condition of the sub-event is true, it will then execute the actions which logically is pretty much : "If A or B is true AND C is true then do stuff".

    If any of the conditions are false, then the actions won't be executed.

  • Thanks for response i thoght this was dead...

    dop200 i will chek that thanks!

    Kyatric thats what i'm currently doing but it works sometimes and sometimes not, i don't know why.

    for testing purposes, i made a main event make it 'OR' block containing 'any touch start' OR 'any key pressed' then a sub-event 'key SPACE pressed" then in the subevent block put a goto next layout.... whay i expected to do is this:

    Any touch (mobile or clik) OR any key...AND space key.. goto next layout... surprisingly it does not work exactly.... first condition works... if you touch OR press key causes no effect unless you have also pressed space key.. so far so good! But the surprise comes when you press space without touching or pressing key.. and it also triggers goto next layout!

    ...

    However in the game i'm currently doing i make another similar structure to compare in an "OR" block several conditions that causes game over and a sub-event to chek if game-over events are actually disabled, then enable.

    And in this case, this combination of (a OR b OR c) AND d works fine!

    I'm on it i'm learning my own, don't worry.. thank you very much for feedback.. just wanted to post something cos i'm new to C2, greetings!

  • Jesus i just realiced what was failing! because the "any key pressed" includes the SPACE key, so when you press the space key the condition "OR any key pressed" matches.. sorry so silly me!

    It's ok, events and sub-events works fine then! THANKS!!

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