"For Each" Expression and Overlapping Objects

0 favourites
  • 7 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • Hoping someone can shed some light on where I'm going wrong here

    In a nutshell: I'm making a city-building game, and when players construct housing blocks next to each other I'd like to add a bonus to the number of people they can house. By default, a housing block adds space for 10 units of population. Adjacent housing blocks should increase that by an additional 5 units. So if you build three housing blocks in a row, the centre block will provide 20 units, and the outside blocks will provide 15 units each.

    What I've tried: I'm running this event sheet section under a "System: Every 1.0 Seconds" check, with the idea that the game will regularly be checking each housing block to see if it has become eligible for bonus population, but without running the checks every single tick:

    The yellow SlotHousingBonus object is created on top of each SlotHousing object (and will be invisible). I tried this because asking the event to check if each SlotHousing was overlapping another SlotHousing at an offset wasn't working. This attempt isn't working either, and I'm not sure why. Population stays at 10 units for each block instead of adding the bonus amounts as well.

    Does the "For Each Object" command not work this way? Is there a mistake I'm making in the checks? I can't figure this one out myself

    Many thanks for reading.

  • My guess would be to not use collisions checks for that for 2 reasons:

    First it is not easy to do like that

    Second: you would be better off trying to make an array to reference each building placement in a grid when needed, as well as referencing the bonus for each case of the grid, which will be better performances wise, and also will let you have more control over that

    But if you want to reallu go with what you have made, make sure that slothousing bonus isn t too wide (if it is 48 on 48, it will actually overlap at the border I think)

    Also the for each is redundant

  • Thanks for the reply

    They're 48x48 objects, so figured an overlap check at self.X -/+ 48 would work.

    I've tried changing it to an overlap check of -/+24, tried getting rid of the "for each" part, and tried checking for housing overlapping housing again rather than overlapping the additional yellow box. Still not working and I have no idea why

    As for arrays, I've never understood them. I'm a very visual person, good with art, terrible with numbers. I can puzzle things out with collision checks because it's a much more visual process. Arrays... just confuse the hell out of me

    If anyone has an idea about why this collision check is failing, I'd love to hear from you. Even if it's just a complete guess!

    Many thanks!

  • Here's a quick working example. Since you have a license I've used families as it will save you having to create the "slothousingbonus" object for every "housing" object.

    [attachment=0:3ftv0yga][/attachment:3ftv0yga]

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thank you SO much! Using families solved the problem

    Added bonus - I had no idea families had been added to C2! I was still patiently waiting, expecting them to one day pop up in the same place they were in Classic, somewhere around C2's containers section in the left-hand panel

  • Thank you SO much! Using families solved the problem <img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt=":D" title="Very Happy">

    Added bonus - I had no idea families had been added to C2! I was still patiently waiting, expecting them to one day pop up in the same place they were in Classic, somewhere around C2's containers section in the left-hand panel <img src="{SMILIES_PATH}/icon_lol.gif" alt=":lol:" title="Laughing">

    I suggest you to read the manual more often: https://www.scirra.com/manual/1/construct-2

    not saying that with a mean spirit, just families are I think here since.. 2012? ( https://www.scirra.com/construct2/releases/r75 )

  • > Thank you SO much! Using families solved the problem <img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt=":D" title="Very Happy">

    >

    > Added bonus - I had no idea families had been added to C2! I was still patiently waiting, expecting them to one day pop up in the same place they were in Classic, somewhere around C2's containers section in the left-hand panel <img src="{SMILIES_PATH}/icon_lol.gif" alt=":lol:" title="Laughing">

    >

    I suggest you to read the manual more often: https://www.scirra.com/manual/1/construct-2

    not saying that with a mean spirit, just families are I think here since.. 2012? ( https://www.scirra.com/construct2/releases/r75 )

    Yep, I've just not been active with C2 before <img src="{SMILIES_PATH}/icon_razz.gif" alt=":P" title="Razz"> Bought it at launch to support the Scirra team, but have been working with Construct Classic up until now.

    Thanks for the link though, I'll definitely be paying more attention to C2 from now on!

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