| 09-19-2008, 03:48 PM | #1 |
Hey I decided to create AI for my game, and the game started lagging very badly when I added the AI to it. I want to eliminate the lag so that the game is playable, and wanted to start with the beginning before I start editing the rest. I heard something about the function GroupEnumUnitsOfPlayer is very costly so I'm not sure if thats the problem. (Keep in mind this is being ran every .01 seconds) JASS:function AI takes nothing returns nothing local integer a = 0 local real x local real y local group g = CreateGroup() local unit u loop exitwhen a>11 if( GetPlayerController(Player(a)) == MAP_CONTROL_COMPUTER ) then call GroupClear(g) call GroupEnumUnitsOfPlayer(g, Player(a), null) set u = FirstOfGroup(g) set x = GetUnitX(u) set y = GetUnitY(u) call MoveRectTo( udg_Region[a], x, y ) call AttackingAI( a, udg_Region[a]) endif set a = a + 1 endloop call DestroyGroup(g) set g = null set u = null endfunction //=========================================================================== function InitTrig_AI takes nothing returns nothing set gg_trg_AI = CreateTrigger( ) call TriggerRegisterTimerEvent( gg_trg_AI, 0.01, true ) call TriggerAddAction( gg_trg_AI, function AI ) endfunction |
| 09-19-2008, 04:29 PM | #2 |
don't use so low periods... imo for AI you don't need less than 0.1... |
| 09-19-2008, 04:43 PM | #3 |
Well what would be a good low number to use? I need it to be a low number because the function it runs checks if a unit is within range of it, and to switch targets between units to get the closest unit. Being ally or enemy. |
| 09-19-2008, 04:52 PM | #4 |
try 0.03125 or 0.04. |
| 09-19-2008, 06:17 PM | #5 |
Ok thanks. Do you guys see any problems with the script? I heard that GroupEnumUnitsOfPlayer is costly. Do you know if thats true, and if I should set the unit to a variable instead? |
| 09-19-2008, 07:03 PM | #6 |
call GroupEnumUnitsOfPlayer(g, Player(a), null) this leaks Add a TRUE function so you can add it to the GroupEnum function. JASS:function DoTrue takes nothing returns boolean return true endfunction call GroupEnumUnitsOfPlayer(g, Player(a), Condition(function DoTrue)) Additionally, if you're giving orders to the AI units, you shouldn't use low values, 0.1, 0.2 is enough. If the AI is like a normal melee one, then a value of 0.2 is enough. |
| 09-19-2008, 07:15 PM | #7 |
JASS:call GroupEnumUnitsOfPlayer(g, Player(a), Condition(function DoTrue)) Doesn't that leak as well? I would create a global private boolexpr and set it to Condition(function DoTrue) at map init, then use it for the groupenum |
| 09-19-2008, 08:01 PM | #8 |
Oh w/e, its still faster |
| 09-19-2008, 08:11 PM | #9 |
Ok thanks, does anyone notice any other problems with it? |
| 09-19-2008, 09:26 PM | #10 |
Are you saying that creating a boolexpr is faster than referencing a global? |
| 09-23-2008, 12:39 AM | #11 |
Would be nice to know whats faster :P, so if someone can tell me then that would be sick. (Want to use the faster one of course) Thank you. |
| 09-23-2008, 01:05 AM | #12 | |
Quote:
One isn't created; if it was, it would leak. |
| 09-23-2008, 01:11 AM | #13 |
Whats this do then: Condition(function DoTrue) ? |
| 09-23-2008, 01:44 AM | #14 |
references some sort of internal table and (if the boolexpr for that function doesn't exist, it creates it and stores it in the table and then) returns the stored boolexpr. |
| 09-23-2008, 07:50 AM | #15 | ||||||||||||||||||
Quick benchmark for you guys: JASS:scope Test initializer Init globals private timer t = CreateTimer() private boolexpr BOOL_TRUE private group g = CreateGroup() private integer j = 0 endglobals private function BoolTrue takes nothing returns boolean return true endfunction private function Test takes nothing returns nothing local integer sw = StopWatchCreate() //call GroupEnumUnitsOfPlayer(g, Player(0), BOOL_TRUE) //call GroupEnumUnitsOfPlayer(g, Player(0), Condition(function BoolTrue)) call DebugPrint(R2S(StopWatchMark(sw)*1000000)) call StopWatchDestroy(sw) call GroupClear(g) set j = j+1 if j >= 500000 then call PauseTimer(t) call BJDebugMsg("Done") endif endfunction private function Init takes nothing returns nothing call DebugPrint("=========="+" START "+"==========") set BOOL_TRUE = Condition(function BoolTrue) call TimerStart(t, 0., true, function Test) endfunction endscope Zero Units:
Ten Units:
|
