Advertisement

structuring time into sfml game

Started by May 11, 2015 12:58 AM
5 comments, last by Evan Gordon 9 years, 7 months ago

Hello everyone.

Ive been off an on here working on my current project, its an RPG more or less. the code i currently have is over on github -HERE-

(the mapmaker section is what im currently working on inside of the globals class)

Anywho im running into a bit of a problem where im not sure how to implement time. I know how to use the elements of time that are in sfml, but actually adding it to my game has proven difficult. I have the delta time and such inside my main loop. However, every time i try and implement it i realize that ive structured it in such a way that it doesn't work for a new part of the game im adding.

So heres my question. so should i create some sort of time object that has some basic functions and variables related to time, and put an instance of that in every class, like the playerCharacter and such. or is that too much and should i have the code related to time inside of the function in each class related to drawing that object?

Any other insight would be appreciated :) im always looking for feedback, or links to articles that are good reads on the subject.

What is it that you need time to do, exactly, and how do you want your game objects work with time changes? That will help determine what design approaches will work.

Rather than creating an instance of a Time object as a member of each game class, it seems more straightforward to me to use a global time object, or maybe just a time variable, which objects access through a method call from their own Update methods. How they consume the current time to update their data should be handled within each object, including tracking duration from a given time. Finally, whatever methods you are using to draw each object should probably just represent the object's state as it is in that frame, not interacting with time at all.

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~

Advertisement

thanks for your response.

So i havent been able to read a lot of articles related to this subject, so the current method im using is mostly just what if figured on my own.

Basically ive got my game loop and in it is my clock and time instances from sfml, every time the game loop loops the clock resets so only the current delta time is recorded.

Ive currently got time implemented in my cooldownBar class, which is a simple cooldown bar that when it needs to be drawn adds the current delta time to a timer that i have inside the object and when the timer gets to be greater than the set time it stops drawing the object.

However when i implemented that i ended up removing the 60fps restriction i had placed in the game, and that then fucked up my player movement(because now if saw W is pressed every time the game loops he moves, which makes him rocket off into the sunset). so now im just looking for suggestions on what i should do, or rather how to implement time into my game so that i can fix this, and also try to prevent further problems related to time in the future.


Ive currently got time implemented in my cooldownBar class, which is a simple cooldown bar that when it needs to be drawn adds the current delta time to a timer that i have inside the object and when the timer gets to be greater than the set time it stops drawing the object.

That's more or less how I would do it. Are you happy with how the cooldown bars have been performing, other issues aside?


However when i implemented that i ended up removing the 60fps restriction i had placed in the game, and that then fucked up my player movement(because now if saw W is pressed every time the game loops he moves, which makes him rocket off into the sunset). so now im just looking for suggestions on what i should do, or rather how to implement time into my game so that i can fix this, and also try to prevent further problems related to time in the future.

This sounds more like an input problem to me-- it worked at 60fps, but arbitrarily, and there isn't sufficient input state or game object state checking to handle anything else. You could use an enumerated state member in your Player class to prevent "doubling up" on actions:


 
// Somewhere else in your project...
public enum PlayerState { Nothing, Jumping, Ducking, Blocking, Attacking }
//
 
public partial class Player
{
     #region Fields
 
     PlayerState currentState;
     ...
 
     #region Methods

     public void HandleInput(InputKey theKey)
     {
          if (theKey.Code == Keys.Spacebar)
          {
               // If the player is ready to jump (i.e., not jumping right now) then initiate a jump. Otherwise do nothing.
               if (currentState != PlayerState.Jumping)
                    Jump();
          }
     }
 
     private void Jump()
     {
          currentState = PlayerStates.Jumping;
 
          // Whatever physics/movement code you have defining a jump goes here
          // When the player lands, set currentState back to Nothing
     }
}

You can also adjust how frequently the SFML window will interpret a held-down key to be pressed (sort of like simulating a KeyPressed event if isKeyPressed == true after a certain amount of time has passed), or use object states to deal with the input however much time has passed (like with jumping above-- if the player is jumping, no additional jump-inducing input is considered).

I think I've interpreted your issue correctly, but if not then it might help to post the code that isn't behaving as you intended.

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~


However when i implemented that i ended up removing the 60fps restriction i had placed in the game, and that then fucked up my player movement(because now if saw W is pressed every time the game loops he moves, which makes him rocket off into the sunset).

Movement shouldn't be frame-based, but time-based.


Movement shouldn't be frame-based, but time-based.

Related, and required reading:

Fix your timestep.

Hello to all my stalkers.

Advertisement

Ah Thank you everyone, i was able to fix my problem, im still a little iffy on the subject. but i made a lot of progress so thats what i needed. Delta Time and all that is an interesting and hard topic.

This topic is closed to new replies.

Advertisement