Jump to content
  • Advertisement

Recommended Posts

Hello, so I wanted to get an opinion on the best ways to manage gameplay logic in a multithreaded manner.

After watching these two presentations :

http://www.gdcvault.com/play/1020886/Killzone-Shadow-Fall-Threading-the

http://www.gdcvault.com/play/1022164/Multithreading

It seems that the best approach is to create a dependency graph for object updating, and for any object that needs to access another object that isn't a dependency, a message should be constructed and called after the "multithreading" phase. I wanted to know if anyone had any experience with multithreading game objects in this manner, or if they have any other solutions.

 

Share this post


Link to post
Share on other sites
Advertisement
17 hours ago, ApochPiQ said:

Unless and until you have a proven difficulty with your actual game objects getting work done in 1 CPU tick, multithreading is not the right tool.

[...]

 

Seconded.

Most games I've worked on typically update game logic systems between 10-20Hz on a single thread without any impact to the gameplay experience. There simply isn't enough going on every frame for gameplay to consume that much CPU, especially considering that humans themselves are limited in how fast they can respond anyway. As long as the key player "interactive" systems are real-time responsive (rendering, sound, input, etc.), the game as a whole will feel responsive. Also consider that in any multiplayer game, network latency is going to mask a lot of it.

Certain gameplay-relevant systems that are more computationally expensive (AI, pathfinding, physics) can be multithreaded independently as previously mentioned, but again those are very specific, well-defined problem domains.

Share this post


Link to post
Share on other sites

Thank you both for your responses. I guess what you are both saying makes sense. I currently have a task system and have jobified my rendering and wanted to take a similar approach to gameplay logic. I might just be focusing too much on preoptimization and also wanted to start leveraging multicore in more parts of my engine.

Share this post


Link to post
Share on other sites
6 hours ago, AxeGuywithanAxe said:

for any object that needs to access another object that isn't a dependency

If you're accessing another object, that's a dependency :P

The method that I've typically seen used on console games (which need this because console CPU's are terrriiiiiblleeee):

Try to do a lot of one operation at the same time (batch processing).
e.g. instead of for each entity, DoSomethingRandomViaAVirtual()
use: for each A, DoAStuff; for each B DoBStuff()

When a particular batch operation is identified as being an actual performance issue, figure out how to move it over to the job system.
To begin with, this is as simple as:
jobHandle = DoAStuffOnJobs()
Wait( jobHandle )
DoBStuff();// this depends on AStuff

Then to improve it further, try to move dependant operations earlier in the frame so that the "Wait" operation above hopefully doesn't actually have to wait.
jobHandle = DoAStuffOnJobs()
DoCStuff();
Wait( jobHandle )//hopefully doesn't actually pause any threads because AStuff jobs naturally finished during CStuff
DoBStuff();// this depends on AStuff

Share this post


Link to post
Share on other sites
11 hours ago, Hodgman said:

If you're accessing another object, that's a dependency :P

[...]

 

Well the issue I was having was with intertangeled dependencies. For example, let's say you have entity a and entity b. Lets say that entity a has a weapon attached to it, and entity b is attached to entity a. Entity a has to be updated before the weapon , before entity b. That fundamentally means that you can't batch process entities because of this dependency, unless you constantly update the "entity" array to make sure all dependent entities are ahead of it. There are certain components that can be easily batch processed, i.e a navigationcomponent , or ray tracing though.

Share this post


Link to post
Share on other sites
10 hours ago, AxeGuywithanAxe said:

Well the issue I was having was with intertangeled dependencies. [...]

2

It means you've got to apply the single responsibility principle more thoroughly.

"Attaching" one object to another is a problem for a transform-hierarchy. The transform hierarchy can "attach" different entities to each other without caring what those entitys are; it just operates on transform-nodes.
(Side note: This is where people say "Aha! That's ECS!". No, that's following OO properly, composing entities out of simpler objects, and obeying SRP.)

You can batch-process the entire transform hierarchy so that the location of every entity is known, and then batch process your weapons afterwards.

Having complex entities that do many different things is the opposite of the batch processing that I'm suggesting ;)

Share this post


Link to post
Share on other sites
10 hours ago, Hodgman said:

It means you've got to apply the single responsibility principle more thoroughly.

[...]

 

I don't necessarily understand how that would completely work then. How granular would your component system be? There can be dozens of different data dependencies that can exist between components and entities. Are you saying that you would not update on an entity basis , and instead update on a per component type basis? Let's say you have a skinned mesh component on one entity, and it performs animations, an entity attached to this entity would have to be updated after the skinned mesh component is updated. With your approach, how would you handle this? Would you update all of the components first and then batch process the entities based on their transform?

Share this post


Link to post
Share on other sites
1 hour ago, AxeGuywithanAxe said:

Are you saying that you would not update on an entity basis , and instead update on a per component type basis?

Well I'm using OO, not ECS so I don't have entities and components, just objects. Complicated objects ("entities") tend to be made up of simpler objects ("components").  I update objects in the order required by their dependencies. 

1 hour ago, AxeGuywithanAxe said:

Let's say you have a skinned mesh component on one entity, and it performs animations, an entity attached to this entity would have to be updated after the skinned mesh component is updated.

If the animation update produces data that's consumed by the transform/attachment update, which produces data that's consumed by the weapon update, etc... Then yes, update animations first, and then update transforms/attachments, and then update weapons.

It can actually be illuminating to try to look at your code from a procedural or functional perspective. Especially functional w/ immutable objects -- try to imagine how you'd structure your code if every object was immutable (read only after construction). This is a lot more like math. In math we can't say:
x = x+1
Because that's simply false (for real numbers)... So a mathematician would write something like:
\(x_{t+1} = x_{t}+1\)
And then the code would have to be something like:
thisFrame.x = prevFrame.x + 1

This is an interesting way to look at your code because in the immutable style, it's always painfully obvious as to which "version" of the x variable is being consumed by any step. In typical OO code with mutable objects, it's not always obvious which "version" of an object you're interacting with, and it can be very easy to accidentally consume an object at the wrong point in the frame (e.g. updating animations after updating attachments).

So, I would recommend structuring your code in such a way that the required update order is as obvious as possible.
e.g.
Instead of:
UpdateA();
UpdateB();//uses results from A internally
Try to write your high level update loop more like:
a = UpdateA();
UpdateB(a);
As in this style, it's actually impossible to call UpdateB without calling UpdateA first.

IMHO, this is one thing that most ECS frameworks completely suck at, and a lot of traditional OOP code as well...

Share this post


Link to post
Share on other sites
8 hours ago, Hodgman said:

Well I'm using OO, not ECS so I don't have entities and components, just objects. Complicated objects ("entities") tend to be made up of simpler objects ("components").  I update objects in the order required by their dependencies. 

[...]

1

Ah, so I think my method is similar to yours then. I don't use ECS , I use a main object (entity), that contains components , somewhat like ue4 . So I'm guessing that you update your entities and components at the same time then? Or do your components only contain data?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
  • Advertisement
  • Popular Tags

  • Similar Content

    • By Catalin Danciu
      Hello,
      In the hopes that my thread is not off-topic or offensive in any way, I dare to ask the following "noob" question:
      what would be the correct way to create sprite animations from 2D *.bmp files?
      I have for reference the 2001 game  Desperados:Wanted Dead or Alive.
      with the help of some tools i found , I was able to extract files containing what seems to be animation frames and frame shadow masks for animating a horse.
      Attached are the archived assets.
      My goal is to recreate the demo level of the game using Unity, for educational purposes.
      I have started by loading the background map layer (also a large bmp file), and next step is to load a 2d character object and animate on the perspective.
       
      Horse_Brown.rar
    • By JoAndRoPo
      While going through a Game Design Document Template, I came across this heading - Core Game Loop & Core Mechanics Loop. 
      What's the difference? Can you provide some examples of an existing game?  Suppose if I am including these topics in a Game Design Document, how should I explain it so that my team can understand? 
    • By HomeBrewArcana
      Hey All,
      I'm looking to get into the gaming industry. I've skirted around the idea for a long time, always thinking that I couldn't do it. I've finally decided to take the plunge.
       
      My question is whether it's worth going to school for game design/coding etc. I've been writing content for paper games for a while, and have a good idea of story and some basic design. But I have next to no technical know how. 
       
      My instinct is that such things can be learned with a lot of practice, video tutorials, and more practice. I've also heard that a degree is not really that important, since you get hired based on your portfolio/prototypes. Why not just make the games?
      But won't a degree help with contacts and mentoring--I'm not a great networker.
       
      Of course, it'll plunge me into more debt, but...
      If anyone has advice, let me know. Also any idea of a program to start with: Game Maker, Unity, Godot, Construct, Stencyl--I've heard good things about them all, so much so that I don't know which would be best to start with!
       
      Thanks
    • By Colm
      Hey all, I've been trying to work out how LittleBigPlanet handles its objects for a while now.
      For those unaware, LittleBigPlanet has a building component where you can build 2D-ish (there are 2 - 16 2D layers that you can build on) objects. There are a number of shaped brushes to do this with, from basic squares and circles to teardrops and eye shapes. There's a decent video showing this off, actually.
      Anyways, I've been trying to work out how this works for a while now. My current thought is that it might be along the lines of storing a list of object corners and then drawing an object within those bounds - this makes the most sense to me because the engine has a corner editor for making more advanced shapes, and because some of the restrictions in the engine are based around corners. Of course, that could also be completely wrong and it's something else entirely.
      What are your thoughts?
    • By Jemme
      Hello,
      I have built games in the past in unity and XNA, android SDK etc. but it’s always been quite specific, when building a small Game Engine or more accurately a sub-set of code that is reusable across projects the tendency to structure the project falls towards composition of objects that are more "generic" than toward inheritance-based models.
      For the Engine specific sub-systems, we have components such as a MeshInstance/Renderer that contains a Handle to a Mesh in a MeshLibary and a handle to a Material from the material library which in turn hold PSO data etc. We can also have Components such as SoundSorce, which are managed by an AudioSystem and contains the data to specify what sound, at what volume etc.
      These systems can be Set up and shut down from within the Engine/Game class such as:
      void Engine::Run() { if (Initialize()) { //Main loop call the systems //Dispatch Events } ShutDown(); } void Engine::Initialize() { //Init all sub-systems FileSystem.Init(); RenderSystem.Init(); PhysicsSystem.Init(); AudioSystem.Init(); WorldManager.Init(); //Creats Entities } void Engine::ShutDown() { //shutdown all sub-systems WorldManager.ShutDown(); AudioSystem.ShutDown(); PhysicsSystem.ShutDown(); RenderSystem.ShutDown(); FileSystem.ShutDown(); } But where does logic go? in smaller games like XNA or SFML you generally create some managers, let’s take the example of darkest dungeon.
      They have various managers like:
      Darkest Dungeon Manager Campaign Selection Manager Estate manager Party formation manager Town manager etc.. All the logic and systems to run the game are based within the managers, and objects contain scripts of data that are fed thorough the systems.
      In a small reusable code base how would you separate the logic from the engine, should like the smaller games all the managers just be shoved into the Engine Class even though that goes against a reusable data-driven framework? Should they just be stuck in a GameLogic system that's is initialized in the Engine Initialism function. And how do people tend to connect data scripts to the other various engine systems without causing too much game specific coupling. For example, you can use an Event System, but firing and event such as DAMAGED_BY_ZOMBIE and having the internal engine respond to that seems to break the separation from the low-high level of the engine system.
      Would be great to here some opinions from the community on this subject as it is quite a vital and potentially problem prone aspect of engine/game development.
      Thanks.
    • By Liquifire
      I'm making an small 2D engine using Kha and I have a timer class, which basically simply either waits a certain amount of time to call a function, or repeatedly calls a certain function after every x seconds. I simply want to know if I should have timers run on different threads. I'm aware that makes sense, but I might use many timers in a game for example, would that still be okay? Also I'm currently writing an animation components, which waits every x seconds to draw another image using the timer class. And in a normal 2D games, I would have many objects with animations on them, other than the other timers. So I just wanted to ask people who have more experience and knowledge than I have what I should do for timers: Either leave them on the same main thread, or make them run on different threads. Thanks in advance.
    • By Developer Dre Reid
      Hello fellow game devs I am seeking some answers to a somewhat simple question. Now I always thought that when a dev was going to work on a new game to publish he/she should keep some aspects secret infill time to release then he/she can give out needed information pertaining to the game to the public as a marketing tactic. However I had a discussion with other game devs who stated that my way was somewhat not right and the only reason I had such thoughts was due to me being a smaller indie dev who was afraid of having my idea stolen. 
      Therefore I would honestly like to know if my way was right or were the other devs correct. If I were to start working on a new game and give out information about it online via social media would I be risking my idea being stolen and it being made faster and better by another developer or team of developers. Am...Am I paranoid??? 
    • By Aaron Marsden
      Hey everyone! 
      My name is Aaron and I’m a writer, gamer, and marketer/campaign manager for PowerSpike, a startup in the Twitch space. For the past two years, I’ve had the opportunity to build and run professional Twitch influencer marketing programs for some great brands (a few clients include Soylent, Camp Mobile, CreativeLabs, and more). 
      I’ve been obsessed with Twitch as both an entertainment and marketing platform since 2014. Before entering the world of marketing, I was a broadcaster and a content creator myself and made YouTube videos in my spare time. 
      Recently many game developers have shown interest in collaborating with Twitch streamers to promote their games -- and I think I can help! 
      I’ve learned so much about entertainment and community development from studying the growth of popular streamers since then, and my current position has allowed me to learn an incredible amount about the process of promoting a product/game/service’s message to a large audience with the help of Twitch streamers. 
      I’d love to share what I’ve learned with anyone who has questions. Ask me anything! 

      If you’d like, you can follow me on Medium at https://medium.com/@aaronmarsden -- that's where I'll be posting both personal and PowerSpike articles on game dev marketing. I also just released my first article, "The Ultimate Guide for Promoting Your Game with Twitch Influencers," here on GameDev.net! You can check it out here:
      Thanks everyone!
    • By ethancodes
      I'm wondering if anyone has any examples or tips/ideas on how to handle pickups in a game. The game is an arkanoid style game. I'm going to have at least 5 different pick up types, and they are going to be in a queue, where only one can be active at a time. Once one pick up is expended, the next one should automatically start up. Some of the pick ups have an immediate effect, such as ball speed. Others will activate when the pickup is hit, but doesn't actually do anything until the ball hits the paddle. Those type of pick ups have a limited number of shots instead of a time limit. What I'm trying to figure out is what kind of structure I should have for a system like this? I'm just curious how these things are handled in other games, especially if anyone has any examples that would be great. Thank you!
    • By shadow_archer
      hello!
      so i had this idea of a game i wanted to make that i had in mind for years and i wanted to try to execute it (at least partiality).
      problem is, i don't really know what kind of game style this game can fall under and i don't really know where to start learning sources and design wise :/ .
      i want to create a management style game and all the controls the user has are through a menu of some sort similar to this (this particular example is from a game called '911 operator' by jutsu games):

      to find more information about how i can create such a game i really want to know what's the name of this style of game, mostly GUI controlled game.
      aside from that i'm looking for a bit of advice,
      which engine would be best for such a game? i'm fluent in C++,C# and python so any game engine you can throw at me is great!
      are there any good sources to learn from before jumping into this kind of game, tutorials of some sort perhaps?
       
      thanks so much to anyone who answers, this really means a lot to me!
  • Advertisement
  • Popular Now

  • Forum Statistics

    • Total Topics
      631375
    • Total Posts
      2999659
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!