Increasing the height of buildings every x seconds

  • Hey guys,

    In my game, my people will be constructing buildings using a 'growing' method where the width stays at the maximum possible value but the height will start at 0 but will gradually increase in height every 0.1 seconds. For this example, the total height of the building is 128 and the construction timer is currently set to 30 seconds.

    What I'm trying to achieve is no hard code, basically; If I adjust the height or the construction_timer values simply for different kinds of buildings, then I want construct 2 to adjust to the new values automatically. Unfortunately my attempt here demonstrates that the building gets completed 9 seconds early (or if the construction time is 10 seconds, its out by 3.7 seconds early) (you will need to copy the image address to get the full image because this post is cutting important information out):

    Please tell me what I'm doing wrong.

    Thanks in advance for your wonderful help!

  • Not sure I understand your problem with the Construction_Timer variable...

    I strongly recommend using Timer behavior instead of the "System-Every X seconds" event.

    You have much more control over the Timer - each building will be running its own timer, you can have different duration for different buildings, pause timers when the game is paused etc.

    Also, I think your Construction_Timer variable should be instance variable. (different for each building)

  • Thank you for your response dop2000

    Yes my Construction_Timer variable is an instance variable for the Construction_Site object which is only going to be used during a construction and is set to instances of that object.

    Basically when a building is being constructed, I flatten the height of the building to 0 and then every 0.1 seconds, I want the building to increase in size by 1% of the total height of the building until it reaches its total max height (so it looks like it's growing from the ground). But this needs to happen over a specified construction time, so if I make the construction timer 10 seconds, then it will be 1% of the buildings total height every 0.1 seconds but if the building's total construction timer is 30 seconds, then obviously the 1% will change over that period of time, because some buildings are expected to take longer than others to build depending on the type of building I have for my game, then I need the right formula to get this right.

    Let me provide a more specific example of what I'm experiencing:

    Let's put the construction timer at a total of 10 seconds, so it will take 10 seconds for my building to complete. The total height of the building is 128 pixels. The construction_timer variable reduces every 0.1 seconds, so we have to consider changing the height of the building every 0.1 seconds.

    My old code was this.

    The building grows and finishes exactly as the construction_timer finishes.

    However, if I do this (where I want to reduce hard-coding and magic numbers):

    The building finishes growing at the construction_timer value of 3.7, so the building finishes growing 3.7 seconds earlier than it should.

    I will try your suggestion of using the timer behaviour but please tell me what's wrong with my formula in those event blocks, because I will be using the same sort of idea with the timer behaviour.

    Edit: I tried using the timer but it does not allow me to perform actions every 0.1 seconds. I do not believe I can achieve what I want to achieve with this behaviour, so I will need to do it the way I have been, but I need to work out what I am doing wrong here.

  • I believe this is the correct formula:

    Building Set height to Self.Height+ ((Self.Total_Height-Self.Height) / (Self.Construction_Timer/0.1))

    Timer behavior definitely allows to perform actions every 0.1s

    So your events with Timer could look something like this:

    Building on created: Start Timer "Grow" for 0.1s

    Building on Timer "Grow":

    ....Building Set height to Self.Height+ ((Self.Total_Height-Self.Height) / (Self.Construction_Timer/0.1))

    ....Building subtract 0.1 from Construction_Timer

    ....If Building.Construction_Timer > 0 : Start Timer "Grow" for 0.1s

  • Thanks again dop2002

    I'll definitely look into the Timer and how I can use it to make my game better.

    Just one thing though. Since I implemented the formula you suggested, the growing rate is working perfectly with the construction_Timer now, but I'm struggling setting the position of the building so it only looks like it's growing from the ground not growing out from the middle if that makes sense.

  • Try moving the Origin image point in the sprite to the bottom of the image. Then it should grow up.

  • Oh wow, it was that simple. It just goes to show how much I'm over complicating things with my project. I definitely need to work on that!

    Thanks for your help again, Dop. You're amazing!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • No worries!

  • Hey dop2000

    This seems to work all right, but I just wanted to share my C2 code to see if I've done it properly:

  • The first event is correct.

    But I'm pretty sure that the second event won't work if you build several building at the same time.

    System-Trigger Once doesn't work per instance, it is a global condition. Once it's true for any building, this event will not be triggered again when you start construction of another building.

    There are many ways you can fix this - you can convert this event to a function and call it once when the building starts. Or you can rename this instance variable to "Building_Progress" and use different values for it: 0-planning, 1-construction started, 2-in construction, 3- construction finished etc.

  • Hey dop2000

    Thanks again for your feedback and suggestions. Sorry I took so long getting back to you, here. To my dread, I had a number of boring real life things to deal with, as you do.

    I took up your recommendation about the progress variable which will change for each building depending on the stage it is up to. I think based on this, it should become very easy to create more buildings simply by checking the "Currently_Building" variable on the construction site:

    I hope this is good now.

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