Game Main Loop Timing (desiring feedback) (solved)

Game development specific discussions.
badidea
Posts: 1354
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Game Main Loop Timing (desiring feedback)

Postby badidea » Dec 09, 2018 23:31

leopardpm wrote:cool! will implement - how does it work on yours with this code?

Looks good (32-bit & 64-bit), a steady 60 Hz. 78% of time sleeping.
Also here at 500 Hz the last digit sometimes different, still pretty stable, no sleeping any more.
leopardpm
Posts: 1791
Joined: Feb 28, 2009 20:58

Re: Game Main Loop Timing (desiring feedback)

Postby leopardpm » Dec 09, 2018 23:35

excellent! looks like I have a solution - thanks badidea for testing and dodi for the tip on how to force SLEEP to 1 ms... nice!

and thanks Paul Doe for the links on what I need to structure next....
badidea
Posts: 1354
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Game Main Loop Timing (desiring feedback) (solved)

Postby badidea » Dec 09, 2018 23:52

Meanwhile, my game is not progressing at all. Only helping the competition :-) Need to cut some features, making it too complicated.
leopardpm
Posts: 1791
Joined: Feb 28, 2009 20:58

Re: Game Main Loop Timing (desiring feedback) (solved)

Postby leopardpm » Dec 10, 2018 0:12

badidea wrote:Meanwhile, my game is not progressing at all. Only helping the competition :-) Need to cut some features, making it too complicated.

my main problem is that I hyper-focus on insignificant details (esp speed optimizations...) which are unnecessary to the final 'product'...resulting in not finishing anything but a series of discreet (but fast!) routines... but no coherent game...
dodicat
Posts: 5757
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Game Main Loop Timing (desiring feedback) (solved)

Postby dodicat » Dec 10, 2018 0:42

I think they call it procrastination.
Many people (and some animals I believe) indulge in it.
But this thread is an extreme case. Basking sharks would be quicker off the mark for an equivalent prize (a ton of plankton)
<goodnight folks>
BasicCoder2
Posts: 3349
Joined: Jan 01, 2009 7:03

Re: Game Main Loop Timing (desiring feedback) (solved)

Postby BasicCoder2 » Dec 10, 2018 6:55

Having the smoothest most sophisticated game engine at your disposal is of no use without a game idea to use it.
Although I have had plenty of spare time to work on the competition I was stymied by not having a nice FreeBASIC theme.
Animated horses would be too complicated and fighting C++ robots too lame.
Pitto seems to have a good FreeBASIC theme so I will be interested to see how he eventually implements it.
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

Re: Game Main Loop Timing (desiring feedback) (solved)

Postby Tourist Trap » Dec 10, 2018 15:17

Hi everyone,

I finally decided to pass a head around here. I also tried to deal with a kind of cycle monitoring in the main loops a year or 2 ago now (I dont even remember when to be true). It didn't give birth to anything really done, unless for a very first attempt put in the tips and tricks section. But here below is my essential point of view summarized (if I had to go further today).

In a game we may want to keep a full cycle (a full main loop time) done in a given time. Some cycles will not be too much busy (not a lot of stuff to refresh/ compute/ and so on), and we will just have to fill the blank with a sleep delay. And some other cycles will simply not be able to see all the tasks completed in full unless we go beyond the cycle time limit we wished to be constant at first. So here is how I decided in a not-implemented-yet blueprint to deal with this second case.

  • have a global ellasped time value returned and incremented each time a function is called from the main loop
  • if the ellasped time starts being to close to the cycle limit then the further calls for the functions will ask for a fast version of the function to be executed (a degraded version)
This implies that the programmer must implement every functions with 2 versions inside, a normal version and a fast version, and an argument to let the caller decide what version he wants to be returned at any time. This way the game should be able to keep its cycles under control, knowing by advance what will be the kind of degraded state we will face. My opinion here is that it's better to choose what will be sacrified, rather than being awaiting for the cpu to make this choice for us ( as far as I know this will result only in lag ;) ).

In brief, this kind of code would result of all of what I'm saying:

Code: Select all

function ACTION1(degraded as boolean, ...)
   select case degraded
   case false
      'do normal stuff
   case else
      'do fast stuff
   end select
end function

'....

'main loop

dim as double cycle_eaten, ellaped = TIMER
do
   ACTION1(degradedmode , ...)
   cycle_eaten += TIMER - ellapsed
   ellapsed = TIMER      
   if cycle_eaten > somelimit then degradedmode = TRUE
   
   ACTION2(degradedmode , ...)
loop until '...

'end

Don't know if it's really a good idea. There are my 2 cents however. If there are obvious reasons not to proceed this way I would of course be pleased to ear about them.

(maybe all of what I wanted to add here is already answered in the previous posts, if so I appologize, I just did not undersand !)

(I posted that in 2016, so yes it was a long time now : viewtopic.php?f=7&t=25264&p=227156&hilit=cycle+udt#p227156)
leopardpm
Posts: 1791
Joined: Feb 28, 2009 20:58

Re: Game Main Loop Timing (desiring feedback) (solved)

Postby leopardpm » Dec 10, 2018 15:38

what you are proposing is a kind of LOD (level of Detail) for actions depending upon how much time is available. This works pretty good for graphics (LOD is usually part of the process for 3D display with far away things using low-poly meshes, etc) - but it is not often based on CPU/GPU cycles available (I don't think?). Here is a common example in most 2D games:

Say you have a scrollable map which only shows a certain amount of the world. In the world are 100 agents moving around. Only the agents which are 'seen' or otherwise on the map are displayed and any agent outside the map are not displayed.

I am planning on using some LOD for pathfinding - if an agent is on the map or close by, then a 'high detail' A* path might be generated for them... if they are far away, then just a formula (like Euclidean distance + some percentage) is used to determine the time it might take for them to get to their destination and they magically appear at the destination after the time has passed. Special Case: if an agent is 'in transit' and the character view has come closer so that the agent is now 'near', then determine an estimated x,y location depending on how much time has passed, and A* from that point to the endpoint - thereby placing them back into the world.

LOD only works for certain things, and it complicates things in general. My thought is to code the program and see if/where it needs speed optimization and then focus on those areas, perhaps using LOD at that point. To try to use LOD in all things at all times to get maximum speed always... this will just be a tremendous effort with possibly NO effect on the program. don't fix a problem that isn't there....
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

Re: Game Main Loop Timing (desiring feedback) (solved)

Postby Tourist Trap » Dec 10, 2018 15:53

leopardpm wrote:what you are proposing is a kind of LOD (level of Detail) for actions depending upon how much time is available. This works pretty good for graphics

Yes, but in the context of games, I think that the graphics will be the the most cycle consuming task generally.

But you are right this may not solve evrything very well at all if used without a deeper view. My idea was more to say, the time consummed will be taken into account at the very start of the programming task. And the coder will provide at least a void shell to deal with low performances from the start. It's more this way of thinking rather than a systematic implementation of multiple versions of each functions. I just think that this may be helpful as a framework to leave some easy way to monitor the game speed at any stage of the development. It's still time consuming to think of this in terms of framework and placeholders, but who knows, it may be interesting on the long term. I don't know if you'd agree. But once again this means inflated code and potentially useless monitoring activity:

leopardpm wrote: To try to use LOD in all things at all times to get maximum speed always... this will just be a tremendous effort with possibly NO effect on the program. don't fix a problem that isn't there....
leopardpm
Posts: 1791
Joined: Feb 28, 2009 20:58

Re: Game Main Loop Timing (desiring feedback) (solved)

Postby leopardpm » Dec 10, 2018 16:03

Tourist Trap wrote:Yes, but in the context of games, I think that the graphics will be the the most cycle consuming task generally.

agreed...but...
My own thinking is that graphics has reached a sort of 'plateau' - we have the ability to do realistic 3D graphics at high resolution with shadows and all manner of realistic effects. My opinion is that now we(as in coders) finally can focus on AI to get realistic behaviors from our agents - LOD for graphics is straightforward, LOD for AI is not so much.

My goal is to have MANY realistic agents in the world for the player to interact with and this will be my speed bottleneck, I fear.
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

Re: Game Main Loop Timing (desiring feedback) (solved)

Postby Tourist Trap » Dec 10, 2018 16:25

leopardpm wrote:
My goal is to have MANY realistic agents in the world for the player to interact with and this will be my speed bottleneck, I fear.

Ah I see. If it's about the modern GPUs you are right, even if I don't have here the kind of modern stuff involved. Anyway I dont know if we can ask a modern GPU, which is something known to host a lot of chips, working in parallel, to do other things than graphics? If possible then, computing multipe agents behaviour in a single pass should be really made faster. I cant remember where I heard something like that for the last time...
leopardpm
Posts: 1791
Joined: Feb 28, 2009 20:58

Re: Game Main Loop Timing (desiring feedback) (solved)

Postby leopardpm » Dec 10, 2018 16:40

I have looked into some of the CODA programming (to code different things for the GPU to do) and it looks promising... but I will not foray into that area EVER... leave it to the experts.

I am more concerned with the AI behavior and possibly how to spread AI computations throughout a network of machines to handle the quantity. For instance, in your typical MMORPG, the server does all the AI (even though very basic) and sends the results to the client(player) machines. My thinking is that the machines that the players are using these days can easily handle the graphics already and could probably now share some of the load of the AI - the trick is accounting for the worst case scenarios - players abruptly disconnecting, etc - if you are fighting some monster, you don't want it to all the sudden stop and look stupid because the machine that was running the AI for that monster suddenly disconnected from the network! I was thinking having redundant routines running on multiple machines that would be 'synched' by the server then sent out appropriately. But this stuff is just a thought experiment as I am nowhere near getting close to that kind of testing, etc. Just thinking....
badidea
Posts: 1354
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Game Main Loop Timing (desiring feedback) (solved)

Postby badidea » Dec 10, 2018 20:01

Sounds like a an idea for a specific task that can take a lot of CPU-time. E.g. 100 AI-enemies trying to find a way to you in a big complex map. Disadvantage is maybe a different game experience on a faster/slower PC. A path finding algorithm that does only path-finding for a part of the path each loop might be better, but maybe more complex to program.
leopardpm
Posts: 1791
Joined: Feb 28, 2009 20:58

Re: Game Main Loop Timing (desiring feedback) (solved)

Postby leopardpm » Dec 10, 2018 20:16

badidea wrote:Sounds like a an idea for a specific task that can take a lot of CPU-time. E.g. 100 AI-enemies trying to find a way to you in a big complex map. Disadvantage is maybe a different game experience on a faster/slower PC. A path finding algorithm that does only path-finding for a part of the path each loop might be better, but maybe more complex to program.

yes partial path finding could be a part of the answer, but.....

I am using a Planning method as a portion of my AI (Goal Oriented Action Planner [GOAP] in particular), will make a forum post once a basic routine is completed)... this means that during the planning stage, there may be many possible Plans, each with multiple paths, to compare against in order to come up with a 'good' plan... in such a case, it is pretty much required to be able to compute the distance of all the paths in order to compare them...partial paths don't work. For a majority of things (static objects), I am using HeatMaps/Flow Maps/Influence Maps (all basically the same thing) to do the pathfinding because the time is trivial. But in the case you specify: 100 AI enemies pathing to you - the options are very limited and expensive(time intensive) because both start and endpoints to the path are dynamic, not static... best I can figure out at this point is to space out all the planning across multiple game loops to 'spread the pain' and not influence framerate (much)... this is a part of the reason for this whole thread.... But, I am completing at least ONE agents' entire planning (with multiple pathfinds....) within each game loop so hopefully that will always take less time than allocated before the next display update....

I haven't figured out how to logically spread the Planning process across multiple game loops though... ug, but I hope I won't need to....
paul doe
Posts: 913
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Game Main Loop Timing (desiring feedback) (solved)

Postby paul doe » Dec 10, 2018 20:22

Goal Based Vector Field Pathfinding.
That's all the pathing you'll ever need to achieve really complex behavior for thousands of agents at the same time, and then some more, with minimal computational cost. And it's trivial to implement to boot.

@leopardpm: You should really start looking to learn OOP. Especially Design Patterns, such as the Command pattern, can provide the distributed solution you seek.

Return to “Game Dev”

Who is online

Users browsing this forum: No registered users and 1 guest