| 01-31-2004, 05:45 AM | #1 |
I'm not even sure how to describe this. I have a loop calling a function, which checks to see if a substring exists in a larger string. For some reason, if I add in delays, the loop works correctly. If I don't add delays, the loop will halt completely after about five iterations. I'm using a bunch of large strings and constantly passing them into and out of functions. (Large as in about 162 characters.) Any ideas on to what is going on here? I'm not using any global variables, so I know that there couldn't be any code crossing over into another function's data. Is there some problem with strings that I don't know about? |
| 01-31-2004, 05:55 AM | #2 |
You can only execute for a certain amount of time before the game thinks your script stalled and ends it. Sleeping any amount of time resets the counter. |
| 01-31-2004, 06:12 AM | #3 |
I see. I put a TriggerSleepAction(0.01) for the inner part of my loop, and it works, only, it executes achingly slow. It must be something in the strings that I use that makes it halt on the iterations. Is there a fix for this I'm not seeing? Should I just recode without strings? |
| 01-31-2004, 11:57 AM | #4 |
Maybe you could post your code, so we can take a look at it. |
| 01-31-2004, 02:35 PM | #5 |
you can reduce the wait time to TriggerSleepAction(0) although it won't make much of a difference. My suggestion is to make it a recursive function that you call through a timer. Timers run a hell of a lot faster then waits. The problem is you will need a global or cache var for count heres an example Code:
function foo takes nothing returns nothing local timer t=CreateTimer() ...actions of the loop here... set udg_count=udg_count+1 if(count=exitwhen number)then call DestroyTimer(t) set t=null return endif call TimerStart(t,0.001,false,function foo) call TriggerSleepAction(0) call DestroyTimer(t) set t=null endfunction |
| 01-31-2004, 11:20 PM | #6 |
Would it be any improvement replacing TriggerSleepAction(?) with Sleep(?)? |
| 02-01-2004, 12:04 AM | #7 |
sleep is AI Only I think... |
| 02-01-2004, 11:11 AM | #8 |
I guess you are right, it is defined in common.ai. |
| 02-01-2004, 11:14 AM | #9 |
Optimizing your code would help. If you can't think of anything, post it here and maybe other people can help. Putting it in a timed loop (event - timer expires blah blah) helps to make the loop execute correctly and not make the game go 5 fps, but it won't execute any faster. |
| 02-02-2004, 04:17 AM | #10 |
Thanks for the input; I've optimized the code quite a bit. Each of my functions work perfectly on a case by case basis. However, they stop working when inside multiple nested loops. This is the function where the program halts - unless TriggerSleepAction is called. Code:
function isPairInSet takes integer x, integer y, string s returns boolean
local integer currentPair = 1
local integer numPairs = getNumPairsInSet(s)
local string pairToFind = I2S(x) + I2S(y)
loop
exitwhen currentPair > numPairs
if getPairFromSet(currentPair,s)==pairToFind then
call TriggerSleepAction(0.01)
return true
endif
set currentPair = currentPair + 1
endloop
return false
endfunctionThe general idea is using a string to store information about a set of coordinate pairs - a simulation of a class. I may have to try the game cache version of classes instead. Does anyone know if SubStringBJ() can be too slow at times? I use that function often. Here is the code for getPairFromSet(), which is used in the previous function. Code:
function getPairFromSet takes integer p, string s returns string
local integer size = getNumPairsInSet(s)
if p > 0 and p <= size then
//returning a coordinate pair from a string of many pairs, such as "54"
return SubStringBJ(s,(p*udg_pairLength)-(udg_pairLength-1)+udg_numDigitsForSetSize,(p*udg_pairLength)+udg_numDigitsForSetSize)
endif
call error("Pair Index out of range!")
return ""
endfunctionIs it possible that my halting loop problem comes from frequently using strings and substrings? Are strings generally slow? Should I go ahead and scrap this, and try a different way to store a set of coordinate pairs? Thanks for the input. |
| 02-02-2004, 08:51 AM | #11 |
Hmm, i can't see anything horribly wrong there. Although you don't need to order a sleep just before returning, heh. But, this doesn't look too intense a algorithm for jass to handle, and i'm not sure why it stops. I reccomend you to check out the string-handling functions here, as they are generally optimized and work great. http://kattana.users.whitehat.dk/browsefuncs.php Cubasis |
| 02-02-2004, 09:22 AM | #12 |
Besides the fact that you have a multitude of bugs in your code, I don't see why that code is slow. Except for maybe your getNumPairsInSet is bugged (did you test its return value?), or maybe strings and SubStringBJ are simply slow to use. As an alternative, you could try usin 2 arrays of integers... |
