2 registered members (Akow, tomaslolo),
1,536
guests, and 12
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: Reducing Function Times
[Re: Dooley]
#481189
08/12/20 06:22
08/12/20 06:22
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
Senior Expert
|
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
Hey!
I guess there are lots of things which can affect function times slower. But for me the biggest slowdown was using while loops for each entity (npc, items, objects, etc). How many while loops do you have in your game running at once? As a workaround, I would suggest to stop using while loops for main game logic at all. Instead, I would suggest using 'ENABLE_FRAME' for decor objects and on_frame for main game loop. I got this from one of the Superku's posts around the forum, this will help you with structuring function execution, as well will help to avoid using 'wait' (it sucks and can cause lots of troubles) and also will help to avoid using lots of while loops. If you need more examples, take a look at the source code of my Retro-FPS which is published on forum and can be downloaded from my github.
Greets.
|
|
|
Re: Reducing Function Times
[Re: Dooley]
#481190
08/12/20 10:11
08/12/20 10:11
|
Joined: Feb 2005
Posts: 48 Hamburg, Germany
Jerome8911
Newbie
|
Newbie
Joined: Feb 2005
Posts: 48
Hamburg, Germany
|
Hi Dooley, Do you have any general tips on reducing function complexity? I'm dealing with the same issues, since a RTS gots tons of functions with loops. For analyzing which part of the functions took most time to calculate I used dtimer() and displayed the time on screen.
void* gameloop()
{
...
dtimer();
check_unit_integrity();
time_unit_loop = dtimer();
dtimer();
levelloop();
time_level_loop = dtimer();
...
}
function show_info_txt()
{
STRING* s1 = " ";
...
str_for_num(s1, time_unit_loop);
draw_text("Rechenzeit Unitloop", 10, 275, WHITE);
draw_text(s1, 140, 275, WHITE);
str_for_num(s1, time_units);
draw_text("Rechenzeit Einheiten", 10, 315, WHITE);
draw_text(s1, 140, 315, WHITE);
...
}
Searching structures took most of the execution time, so I would start from there. At start I even used ent_next to search for specific entities, obviously that takes a lot of time. Saving specific objects (like the units) in its own search lists saved a lot of time for me:
//search list for units
int no_of_units;
int max_units = 256;
UNIT_STRUCT unit[256];
//search list for buildings
int no_of_buildings = 0;
int max_buildings = 64;
ENTITY* building_array[64];
...
Another thing you might consider is to "update" some functions less frequently (not every frame, but every other frame or even less frequent). Best regards
Last edited by Jerome8911; 08/12/20 10:13.
|
|
|
Re: Reducing Function Times
[Re: Dooley]
#481197
08/12/20 16:51
08/12/20 16:51
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
Senior Expert
|
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
As for the ent_next, I would try to avoid cycling through all entities, I think this might be slow too (on big levels). Let's say you have 1000 entities on the level (sprites, models, etc) and you only need 100 of them, so instead of cycling through all 1000 and finding those that 100 you need, you can right at the start of the level, add those 100 into ENTITY array list (don't forget to have integer counter, to register total amount of entities) and then cycle within that list. ENTITY *list[999];
int counter = 0;
action add_to_list()
{
list[counter] = my;
counter++;
}
void cycle_in_list()
{
int i = 0;
for(i = 0; i < counter - 1; i++)
{
if(list[i] == NULL)
{
continue;
}
// do something with the entity
}
}
|
|
|
|