About the scheduler in the game

Source: Internet
Author: User

1.1 schedule events in the game

One scheduling can effectively help the following game technologies, including physical simulation, character movement, collision detection, AI in the game, and rendering. One of the key issues with all these technologies is time. When hundreds of different objects and processes need to be updated at different times, many of these simulation technologies become very complex.

An important capability of the scheduler is that it can dynamically add and delete objects, which can smoothly add new objects to the game and participate in simulation together with other objects in the game, and then delete it from the scheduling when it is not needed.

1.1.1 scheduler Composition

The basic components of the scheduler include the task manager, event manager, and clock. With these component schedulers, you can generate time-or frame-based events and then call the corresponding event processor.

The task manager processes task registration and organization. Each task has an interface that contains a callback function that can be called by the manager. The task manager maintains a list of tasks, including the scheduling information of each task, such as start time, execution frequency, duration, priority, and other attributes. It may also contain user data pointers or performance statistics.

The event manager is the core part of the scheduler. Each task in the task manager defines one or more events to be processed. An event refers to the time required for a task to be executed. The responsibility of the event manager is to generate necessary events to execute corresponding tasks.

Real Time and virtual time: the scheduling of a real time is very simple in concept-the time manager keeps repeating and looks at a real time clock, it triggers an event whenever the target arrives. The scheduler of a Virtual Event divides the time into frames. Tasks are executed in batches between frames, run in virtual time, and then synchronized with the real time when each frame is rendered.

The clock component is used to track the real time, the current simulation time, and the number of frames. The time manager sorts and generates events. In some cases, multiple tasks may be set to run at the same time. Execute with a higher priority. If the priority is equal or the system has no priority, the task is executed in turn. We often need to dynamically change a registered task attribute, which may involve changing its priority, period, the duration, or you need to delete it before it is found. To update the attributes of a task, we must use an external method to locate it. A unique registration ID can be used to mark a task.

1.1.2 a simple Scheduler

The scheduler is designed mainly on two components-the scheduler engine itself and the ITask plug-in interface. To run the scheduler, you must have a program to call it. In a non-graphic program, this requires that it be placed in a loop and then executed in the execution order. While (running) schedframe. ExecuteFrame (); there are two ways to integrate the scheduler into a message-driven graphical interface. The first method is to modify the message loop to process messages and call the scheduler. This is the easiest way to think of, but there is a drawback, that is, when the window size is changed, the scheduler will stop working. The second method is to create a Windows clock and use the clock message to call the scheduler. Since the clock message is not interrupted by the window drag, the scheduler can continue to run in the background.

Simulation: The scheduler can be used to drive the simulation system. To achieve animation and collision detection, most simulation engines divide time into independent small pieces.

My understanding:

The scheduler is equivalent to a general controller or a coordinator, I call it manager. Most of the tasks in the game can be viewed as a task, such as rendering and collision detection. task execution is driven by events, and there are three types of events, that is, time-based events, frame-based events, and rendering events.

Time-based event: the event is determined by time, that is, when the time is reached, an event will occur.

Frame-based event: the event is determined by frame, that is, when the game runs to a specified frame, an event is triggered.

Rendering event: this event is special. It occurs in every frame, because every frame of the game is rendered.

In the game, we register all tasks in the scheduler, and then the task execution is completely handed over to the scheduler. The scheduler is responsible for when to execute the task, when to delete the task, and so on.

To enable the scheduler to process all registered tasks, the scheduler adds a task manager. In fact, a feature of the language is used here to define an abstract class:

Class ITask

{

Public:

Virtual void Execute (int id, int time, void * arg) = 0;

};

To use the scheduler, you must inherit from this class to process all tasks. Then the scheduler uses the ITask pointer to operate all tasks.

Let's take a look at the composition of the Scheduler:

To know when time-based events occur and when frame-based time occurs, the scheduler adds a Clock component (Clock ). Because there are three types of events, and of course there are three types of corresponding tasks, the scheduler maintains the three types of tasks, which are rendering tasks respectively (the corresponding event has only one ), time-based tasks, frame-based tasks. Therefore, there is more than one task based on time and frame, so two linked lists are maintained here.

Each frame of the game calls the ExecuteFrame of the scheduler. This function first updates the time and then executes all time-based and frame-based tasks that will be executed in the current frame, and rendering tasks.

Note that all tasks stored in the linked list are arranged according to their execution sequence, so that the program only needs to process the elements in the header each time. After the program executes a task, the program will check whether the task still needs to be executed. If yes, it will be inserted into the linked list again; otherwise, it will be marked as deleted.

The benefits of using virtual time are not quite clear.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.