Skip to main content
added 608 characters in body
Source Link
AturSams
  • 10.6k
  • 1
  • 35
  • 60

InFirst and foremost, use a profiler and check what are the processor hogs in your game. Otherwise you are spending time augmenting and optimizing something that will not necessarily improve game performance noticeably.

If you already checked and are sure you wish to optimize AI to run a few times per second then in the abstract theoretical sense, you probably want to allocate 1/30 processor time for the AI to run asynchronously instead of lending it 100% of the processor time for 1/60 of a second. In practice in the pragmatic sense I would consider running the AI of different units in random varying times or breaking down the aiAI process into chunks or enabling the process to run 1-2 milliseconds, stop and pick up again later. This could be done by checking a timer in some pre-determined checkpoints in the program and if the time is up, storing the current relevant data and returning back to the main loop.

i.e.:

ai_work(){ load_current_progress_state(); while(work_left){ do_a_little_bit_of_work(); if(check_time_spent() > allotted_time){ save_current_progress_state(); return; } } return; } 

Running the code only once every X milliseconds would not necessarily yield the benefit you want because it could theoretically cause spikes in frame times.

In the abstract theoretical sense, you probably want to allocate 1/30 processor time for the AI to run asynchronously. In practice in the pragmatic sense I would consider breaking down the ai process into chunks or enabling the process to run 1-2 milliseconds, stop and pick up again later. This could be done by checking a timer in some pre-determined checkpoints in the program and if the time is up, storing the current relevant data and returning back to the main loop.

i.e.:

ai_work(){ load_current_progress_state(); while(work_left){ do_a_little_bit_of_work(); if(check_time_spent() > allotted_time){ save_current_progress_state(); return; } } return; } 

First and foremost, use a profiler and check what are the processor hogs in your game. Otherwise you are spending time augmenting and optimizing something that will not necessarily improve game performance noticeably.

If you already checked and are sure you wish to optimize AI to run a few times per second then in the abstract theoretical sense, you probably want to allocate 1/30 processor time for the AI to run asynchronously instead of lending it 100% of the processor time for 1/60 of a second. In practice in the pragmatic sense I would consider running the AI of different units in random varying times or breaking down the AI process into chunks or enabling the process to run 1-2 milliseconds, stop and pick up again later. This could be done by checking a timer in some pre-determined checkpoints in the program and if the time is up, storing the current relevant data and returning back to the main loop.

i.e.:

ai_work(){ load_current_progress_state(); while(work_left){ do_a_little_bit_of_work(); if(check_time_spent() > allotted_time){ save_current_progress_state(); return; } } return; } 

Running the code only once every X milliseconds would not necessarily yield the benefit you want because it could theoretically cause spikes in frame times.

Source Link
AturSams
  • 10.6k
  • 1
  • 35
  • 60

In the abstract theoretical sense, you probably want to allocate 1/30 processor time for the AI to run asynchronously. In practice in the pragmatic sense I would consider breaking down the ai process into chunks or enabling the process to run 1-2 milliseconds, stop and pick up again later. This could be done by checking a timer in some pre-determined checkpoints in the program and if the time is up, storing the current relevant data and returning back to the main loop.

i.e.:

ai_work(){ load_current_progress_state(); while(work_left){ do_a_little_bit_of_work(); if(check_time_spent() > allotted_time){ save_current_progress_state(); return; } } return; }