Proper Programming Methodology

0 favourites
  • I've been working on a game for a while, and I'm starting to run into issues with slower performance machines.

    I've been trying to use DT everywhere I could, but I believe I may have been programming incorrectly, and I wanted to see what the best approach would be.

    Normally what I would do is set an instance variable (#) on an object. I would then set "if instance variable > 0, every 0.1 second, subtract 1 from instance variable". Then, whatever I wanted the sprite to do; attack, jump, etc. I would go "if instance variable = 0", do this and set instance variable to 10.

    Now, of course this works on probably all machines. But I'm using this for everything, and I'm afraid it's not a good way to do it. For instance, I have an air dash in my game that sets an instance variable to 4, and subtracts every 0.1 seconds moving 1500*dt pixels every tick. However, the end location doesn't match up when the fps is low (<25), and I can sometimes dash through walls, which is unintended.

    I've tried adding dt to the variable and setting when the variable > or equal to X (usually the number of seconds I want it to last), set back to 0, but it still doesn't have the same result everytime.

    I'm using this to change enemy attacks, as timers for reloading, damage timers, etc.

    So my questions is this: What am I doing wrong, and what should I be doing instead?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • There's no need to add "every 0.1 seconds".

  • So I should just add every tick?

  • - forgot to add this!

  • ome6a1717 for speed of 1500 pixels/sec for a performance of 10 FPS the sprite will move 1500*0.1=150px (more or less), so if you have problems with missing collision make you sprite bigger than the jump it makes every tick

    For the other problem of not stopping every time on the same location, you could store the end position in an instance variable and make the sprite snap to the stored position if it passes or is near the desired position.

    Another idea is to use dt to check the performance of the machine so the the final jump to end location look smooother.

    P.S. Bullet behavior uses dt to maintain speed on different FPS.



  • Cipriux - sorry for my ignorance, but I'm still a bit confused. The thing is, I enable the character to be able to dash through walls. To do this, I have an indicator sprite that basically states "if indicator is not overlapping wall or black space, disable collision and move towards X,Y at 1500*dt". So whats happening is I'm dashing into empty space, but because the distance isn't the same, it overshoots OVER a wall and into a solid.

    I see what you're saying about the instance variable, but I'm also trying to create a "push" at the end of the dash using a platform vector.

    Also, not to open up another issue, but you say that Bullet behavior uses dt for speed, but my bullets when at a low fps go right through solid objects and will only occasionally hit them.

    The way I understand dt is that you use it to make sure the same value of whatever action (rotating, moving, etc.) is always used based on actual time instead of ticks. Is this not the case?

    Again, sorry for the ignorance - I'm a complete noob when it comes to programming, and I'm trying to wrap my head around it.

  • ome6a1717 I am not an expert, no need to apologize.

    Hm...I do not understand the way your game is played...but in principle is similar to a pool/billiard game? I mean: the object is moving with a big speed, it has a direction vector.

    I do not know how to overcome this issue. Maybe have some variables to save the last position and compare to the current one, so you can calculate the direction vector...

  • Maybe build an invisible sprite representing the vector and use that for collision check (like a linear tail)

  • Maybe build an invisible sprite representing the vector and use that for collision check (like a linear tail)

    Cipriux - this is exactly what I'm doing, and it works brilliantly as long as the frame rate doesn't mess up my instance variables subtracting every 0.1 seconds.

    I think my main issue is more than just figuring that out or why the bullets don't collide at low fps - it's really learning the proper way to program in C2.

    Say I wanted to do this:

    Every 2 seconds, have an enemy choose whether or not he should attack or not (using the choose expression). How would you set the "every 2 seconds" so it's the same at every frame rate & every pc?

  • ome6a1717 Every x seconds is frame independent post. The problem appear when the framerate is below 10FPS....Construct 2 forces everything to behave like is moving at 10 FPS. So for example if you machine is soooo slow that the game runs at 5 fps...the timescale will slow down so that sprites will not appear to teleport around the screen. For this scenario 1 sec will be sloweed down to 2 seconds.

    Ashley says: "If the game runs *below* 10 FPS, it will start slowing down. At or above 10 FPS it should still run every 1 second if the event says every 1 second. If that's not happening for some reason, share an example."

    So every x seconds will trigger every x+/-dt (I think). It will not happen every 1000 milliseconds exactly...if you want to check for the exact number of ms you can use system.wallclocktime to verify and maybe update your events.

    p.s. Even in Microsoft Visual C# if you put a timer, the smallest precision will be more than 14 ms

    capx example

  • Cipriux - okay I think understand this. Then answer me this, if dt is frame independent as well, then why, using this code, is the player moving much further at a lower fps (still above 10)?

    <img src="" border="0" /><img src="" border="0" />

    <img src="" border="0" />

  • You should post the full capx. This lone excerpt is not enough to understand exactly what you're doing.

    You also don't need the "Wait 0" actions.

    Also remember how events work, every tick a condition is true, the actions will be executed.

    Perhaps what you need is to set the platform vector to dt to have the effect you're looking for (I'm not sure as it depends on the rest of your project).

  • ome6a1717 Is this event happening every x second, or everytick?

    <img src="" border="0" />

    If is everytick is the correct loop.

    I am afraid cannot help any further

  • Cipriux - it's happening every tick. Dash is being set to 0.35 (because it should only last 0.35 seconds)

    Here's a clean capx - click new game and then use right click to slide and jump right click to do the air dash to see what I'm talking about. Ideally I want to player to move this distance and then get pushed a bit in the direction he dashed.

    The code is in Movement > Air Dash (and Slide is doing something similar, but it would be easier to just look at air dash)

  • ome6a1717

    things to try:

    If subtracting 1 from instance variable every 0.1 seconds , to make sure it does not go below 0 (not to trigger "if variable=0") try using clamp Ex: clamp(var-1, 0 ,max), or use "if variable=0"

    instead of dashing depending on time (0.35 sec) try dashing depending on distance.

    For speed of 1500 pix/sec and 0.35 time the distance will be 1500*0.35= 525 px.

    So: before dashing, save player position x1,x2, and while dashing check the moved distance , if is above 525 make it stop and return to 525 (imagine a circle with a radius of 525 px and the player has to go back inside )

    If the movement is depending on time, than is depending on dt which for 20fps and a speed of 1500 px/sec the movement error will be 75 px. 1/20*1500=75

    Your project is overwhelming for me to try modifying myself.

    In programming math is your best friend.

    By the way, nice game.

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