Frictional Games Forum (read-only)
Custom function freezes the game. - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: Custom function freezes the game. (/thread-16801.html)



Custom function freezes the game. - Fearlessagent - 07-07-2012

So, in the custom story that I'm creating, I name things I need to interact with in the code quite precisely. Usually it takes the form of: ROOMNAME_ITEMNAME_TYPE

So for example, a note might be labled: lab_chemDiscovery_note

I wanted an easy function to extract the middle word, in the above case this would be "chemDiscovery". However, whenever the function is called, the game just locks up. Can anyone see what I might be doing wrong?

PHP Code:
string getProperName(string &in var){
    
int chr 0;             //character number in string
    
string newString "";   //where the new string will be stored
    
string letter "";      //where the current letter being tested is stored
    
int l 0;               //how many underscores we've found
    
while(2){ //we can stop evaluating after finding 2 underscores
        //using StringSub(string,int,int) to return a partial string between two positions in the string.
        
letter StringSub(var, chrchr+1);
        
AddDebugMessage("Analyzing Letter: " letter,false);
        
//Only append a letter to the new string if its not an underscore and we've already found an underscore.
        
if ((letter != "_") && (== 1)){newString newString letter;}
        
//increase underscore count when finding one
        
else if (letter == "_"){l++;}
        
//increase character count 
        
chr++;
        
AddDebugMessage("New String: " newString,false);
    }
    return 
newString;


I'm thinking I should be able to call this function as:
PHP Code:
getProperName("lab_chemDiscovery_note"); 
or even through passing a callback variable to it (the intention).


RE: Custom function freezes the game. - Your Computer - 07-07-2012

If i'm interpreting everything correctly, l would never be greater than 2.


RE: Custom function freezes the game. - Fearlessagent - 07-07-2012

(07-07-2012, 05:42 AM)Your Computer Wrote: If i'm interpreting everything correctly, l would never be greater than 2.
Correct, the goal is to stop the while loop after 2 underscores in the string are found, then return the string value.


RE: Custom function freezes the game. - Your Computer - 07-07-2012

Infinite loops halt the game. The only time the script increments l is when it finds an underscore and if the previous if statement evaluates to false. With a syntax of ROOMNAME_ITEMNAME_TYPE, it is unlikely that either if statement would evaluate to true.


RE: Custom function freezes the game. - Fearlessagent - 07-07-2012

(07-07-2012, 05:57 AM)Your Computer Wrote: Infinite loops halt the game. The only time the script increments l is when it finds an underscore and if the previous if statement evaluates to false. With a syntax of ROOMNAME_ITEMNAME_TYPE, it is unlikely that either if statement would evaluate to true.
I kind of figured that's whats happening but I don't understand is why neither would evaluate to true. It would advance the character number regardless if the conditionals evaluated, until it hits an underscore. Then it would increase l by one and every letter after that would be appended to newString until it hits the last underscore. Thats the intention anyways, did I miss something?


RE: Custom function freezes the game. - Your Computer - 07-07-2012

I did a test run, and i got the following output with "Text_Text_Text":

Code:
Analyzing Letter: T
New String:
Analyzing Letter: ex
New String:
Analyzing Letter: xt_
New String:
Analyzing Letter: t_Te
New String:
Analyzing Letter: _Text
New String:
Analyzing Letter: Text_T
New String:
Analyzing Letter: ext_Tex
New String:
Analyzing Letter: xt_Text
New String:
Analyzing Letter: t_Text
New String:
Analyzing Letter: _Text
New String:
Analyzing Letter: Text
New String:
Analyzing Letter: ext
New String:
Analyzing Letter: xt
New String:
Analyzing Letter: t
New String:
Analyzing Letter:
New String:
Analyzing Letter:
New String:
Analyzing Letter:
New String:
Analyzing Letter:
New String:
Analyzing Letter:
New String:
Analyzing Letter:
New String:
Analyzing Letter:
New String:
Analyzing Letter:
New String:
... (ad infinitum)

It is on an infinite loop, and as you can see letter never equals "_", so the issue is in the incrementing chr variable. Instead of adding 1, just put simply 1 -- that is, for StringSub.

I'm going to go to sleep now.


RE: Custom function freezes the game. - Fearlessagent - 07-07-2012

(07-07-2012, 07:44 AM)Your Computer Wrote: I did a test run, and i got the following output with "Text_Text_Text":

Code:
Analyzing Letter: T
New String:
Analyzing Letter: ex
New String:
Analyzing Letter: xt_
New String:
Analyzing Letter: t_Te
New String:
Analyzing Letter: _Text
New String:
Analyzing Letter: Text_T
New String:
Analyzing Letter: ext_Tex
New String:
Analyzing Letter: xt_Text
New String:
Analyzing Letter: t_Text
New String:
Analyzing Letter: _Text
New String:
Analyzing Letter: Text
New String:
Analyzing Letter: ext
New String:
Analyzing Letter: xt
New String:
Analyzing Letter: t
New String:
Analyzing Letter:
New String:
Analyzing Letter:
New String:
Analyzing Letter:
New String:
Analyzing Letter:
New String:
Analyzing Letter:
New String:
Analyzing Letter:
New String:
Analyzing Letter:
New String:
Analyzing Letter:
New String:
... (ad infinitum)

It is on an infinite loop, and as you can see letter never equals "_", so the issue is in the incrementing chr variable. Instead of adding 1, just put simply 1 -- that is, for StringSub.

I'm going to go to sleep now.
Man, I was never able to get the debug messages, my game just froze up...Thanks for your help. I completely forgot that the last argument in StringSub was the number of characters to read and not another character position. Ah well, just had one of those derp moments I suppose. Big Grin