How to find objects that don't share certain values?

0 favourites
  • 4 posts
From the Asset Store
Tap the ball, control it, avoid touching spikes and score higher!
  • I have 2 object types, A and B, and both have an instance variable called matchID.

    At certain points in the game, every B instance should be destroyed if there isn't an A instance whose matchID equals B.matchID.

    Here’s my pseudocode:

    - IF there is a B instance whose matchID instance variable does not equal the matchID instance variable of any A instances, THEN Destroy B instance

    What’s a clean way to implement this with C3? I'm probably overthinking this but I cant come up with something elegant.

    Thanks

  • Cleanest way I can come up with is to also add a Boolean variable to A and B, and do this:

    A: set paired to false
    B: set paired to false
    
    for each A
    B: matchID = A.matchID
    pick B instance 0
    -- A: set paired to true
    -- B: set paired to true
    
    A: [inverted] is paired
    -- A: destroy
    
    B: [inverted] is paired
    -- B: destroy
  • Thanks R0J0hound for the suggestion, similar to what I was thinking - but better!

    I was curious if there was a way to use elegant pick logic to do this in a 1- or 2-line event. I've seen some beautiful code suggestions in these forums that I never would have come up with, mainly because my brain just doesn't think that way.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I’m always trying to find ways to simplify the events if I can.

    Another idea instead of the Booleans is to use a dictionary. I’ll call the dictionary “pairs”. Nice way to use less variables, probably faster for a large amount of objects. The only downside is it doesn’t handle if multiple A or multiple B have the same matchId. But that may not be an issue if that can’t happen.

    for each A
    -- pairs: add key A.matchId with value A.uid
    
    for each B
    -- pairs: has key B.matchId
    -- -- pairs: remove key B.matchId
    -- else
    -- -- B: destroy
    
    pairs: for each key
    A: pick by uid pairs.curValue
    -- A: destroy
    -- pairs: remove key pairs.curKey
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)