Tag Info

Hot answers tagged

13

A common approach for this is to use the update loop and delta time. float timerCurrent = 0f; float timerTotal = 5f; Update() { timerCurrent += deltaTime; if(timerCurrent >= timerTotal) { //do timer action timerCurrent -= timerTotal; } } This supposes that you have access to a deltaTime variable. Delta time is simply the ...


11

TotalMilliseconds is a double, so you should not be doing an equality comparison there. There is every chance that the timer gets a fraction of a millisecond off, and your conditional never triggers. Also, there is no mechanism in your description for that code to run more than once per update. I recommend storing an integer frame number somewhere: int ...


11

Game Coding Complete describes an Update loop well. It's simply a while loop running in your main class that does the following (more or less): while( game.isRunning ) { GetInput(); Update( dt ); Render(); } You pass in a dt (delta time) depending on whether you want your game running at a fixed or variable frame rate. For a fixed frame rate, ...


11

TL;DR The author is not suggesting you implement this in your game. He's telling you that the precision will be slow changing, but bad. This means the float you're using to track your game time would start at 2^32. Because setting the number that large to start with, whatever you add on to it in the next 136 years, won't change the exponent. Though, the ...


10

I have a damage system is my game and I would really love to stay away from float damage values, both for memory and game design reasons. My first instinct is to question your assumptions. It's extremely unlikely that using floats rather than ints for something like this is going to affect your memory usage in any noticeable way. On most systems, they ...


7

What are the factors that determine the default frequency of a shader call? There's no such thing as a default frequency for a shader call. It depends of course on the game's frame rate, but not only. Vertex shaders are called for each vertice of a primitive rendering. The call frequency will depend on how many primitives you render, on the current ...


7

Most games have a function that calculates the current time -- perhaps using QueryPerformanceCounter(), perhaps using GetTickCount64(), perhaps using something else. Normally this function is designed so that it initially returns zero, and then gradually returns larger numbers. What the author is saying (and I can be definitive about this because I am the ...


6

It's not the short timer code you posted, any number of those would be fine, it's what doStuff you do with each one. One problem I can foresee is that if you have timers of different frequencies some of them will sometimes "beat" and trigger their doStuff code all at the same time. For example, if you have a timer for 2.0 and one for 3.0, then every 6.0th ...


6

You mean games like Ogame? In my opinion it is best to calculate the value when it is needed, not on a regular event as Jari Komppa suggested. You could even go a step further and calculate the value on a client side sometimes. In case of Ogame: Imagine that server increases amount of resources every minute for millions of users (where probably half or ...


6

For a basic game loop, you'd run a while loop, within which you'd get the time using nanoTime(), determine how much time has passed since the last frame, then update your gamestate and render. Using http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/System.html#nanoTime%28%29 , you can poll for the elapsed time. Basically... public static void ...


6

I would not put the main loop in a timer. Rather I would make a 'while' loop that processes every frame and then use some sort of timekeeping functionality (it sounds like in Java that would be System.nanoTime) to calculate how much time has passed since the last frame/last iteration of the loop. Certain languages are exceptions here (eg. JavaScript, ...


6

The general solution to this problem is to have an update function with a completely fixed timestep. Unity calls this FixedUpdate, in physics engines you can run the simulation several times before updating the world, etc. but it's all the same concept. While this fixed update function will always be slightly out of sync with everything else in your world, ...


5

You only need to update the database in two cases: the user performs an action or requests information from the server another player wants to interact with said user Periodically checking if the database needs to be updated would put a lot of strain on your servers. Instead, information should be updated only when it is needed. In other words, you don't ...


5

Your own proposed answer is spot on; to determine the finish time for a car, you take its position on the frame before finishing and its position on the frame after finishing, and determine at what point during that frame it actually crossed the finish line. If (for example) it crossed the finish line at 40% of the way from its previous position to its ...


5

Instead of sleeping to regulate game speed, use the time elapsed per frame to give everything a consistent speed. myObject.Move(elapsedTime*moveSpeed,0,0); Something to that effect. No matter what speed the processor runs at or the framerate, the object would always move at the same speed. Make sure any movement or rotation that requires a specific speed ...


5

Your timer event has an infinite loop. This blocks the event dispatch thread, blocking any other GUI events like painting or the frame closing from occurring. Learn the Java code conventions, they make your programs much easier to read. Variables should not start with an uppercase letter.


4

That is the standard way of getting the time from C#. And using a BackgroundWorker I think is better than multi threading it in this particular scenario. Though I think the above comments are a bit confused as with most games, there is no waiting around for inputs its always updating. It will be hard to perform time based actions when you aren't using a ...


4

Most game loops I've encountered like to check an input state of some kind (not wait around for input, instead have it buffered). I'm assuming your making some game with the Command Prompt in C#. You'll probably want to buffer input via ReadLine() in a thread safe queue. You should then implement a more normal game loop in a separate thread that checks this ...


4

Timer can handle it, your main concern will be memory usage with thousands of requests. A suggestion is to create an Async timer with a callback to signal the table or player of an action. The function signature is this : Timer( TimerCallback callback, object state, uint dueTime, uint period) so instead of keeping track of the timer queue yourself and ...


4

It's not clear why this should be a problem. If the delta is the time between updates, then it doesn't matter if it's called 1ms late providing it's consistently called 1ms late, which would appear to be the case. It's also not clear how you are observing that this is a problem given that presumably there is no visual output until the whole lot has returned. ...


4

Instead of grabbing the system time and calculating the delta time each frame, just keep the delta frame time constant in the debug build (deltaTime is just always 16ms, for example). This way each simulation frame has a fixed time step regardless of how much debugging you did in between frames. Of course this should only be done in your debug build.


4

Original answer: Why would you implement threads? Unless they are used to resolve a performance problem, they are a waste of time you can invest in your next game project. If the game is not processor heavy I am not sure threads are a valuable improvement. They mostly affect systems powerful enough to handle the game without threads in the first place. To ...


4

If you’re in 3D space it really depends on the axis on which you want to rotate. There are indefinite numbers of possibilities when you do not tell your function where it should rotate. With the current function signature there seem to be two possibilities: you rotate in 2D space you always want to rotate over the smallest distance I can only give you a ...


4

You should just read the current time from the system; for Windows, use QueryPerformanceCounter or timeGetTime. If you are using some free library for window management (which I highly recommend) like SDL or SFML, they have their own functions. Google 'real time game loop' or 'game loop' for multiple examples of this done right. BTW, that thread is the most ...


4

I think you're conflating render and logic updates. The delta returned by the Gdx.graphics object is an elapsed time value that probably should only be used for render updates, but the state an object is in is more of a game logic issue, and you generally want these things handled on different timers. This is because if you pause your game logic (by ...


4

If you're wanting to ensure the time is accurate, you should get the time from the internet or from the GPS on the device. If the user closes the app, there is nothing left to run a timer with. Even services are paired with a process and will get cleaned up if not being used. You can detect time changes when your app is running, but your app won't know ...


4

Timestamps are a terrible thing to use for IDs, even with a high-precision timer 2 entities do not necessarily have to have a different ID (think how little time can have passed between 2 calls to CreateEntity()). Also you cant fit time into a simple 32bit integer. If you're going to load entities from two different sources there is a much easier fix. Load ...


3

Basically, I use an update loop and not an event based system. To update monsters, you could iterate a list of monsters and update them in the update loop with, let´s say, monster.update() If a monster has to walk it has to update its position. So, in the update method I calculate the new position on base of the elapsed time, which is stored in a seperate ...


3

Memory reasons? In day to day C++ a float is 4 bytes, an int is 4 bytes, there is no memory reason. For example, to maintain a float value for shields for one million enemies is only 4M RAM, typical low end PCs have at least 2G so this kind of data storage isn't really a problem. Trying to process one million enemies, that's a problem =) As for game ...



Only top voted, non community-wiki answers of a minimum length are eligible