Author Topic: String Manipulation [causes strange crash]  (Read 2002 times)

This is my first time seriously diving deeper in manipulating a string. The following code cuts out a set of specified words from a range. The problem that I seem to be having, is that Blockland crashes whenever the following function is used. (executes fine)

Code: [Select]
function TC_lovetract(%msg, %phrase)
{
%dp = strpos(%phrase, "-");
%lw = getSubStr(%phrase, (%dp + 1), 1);
%fw = getSubStr(%phrase, 0, (%dp - 1));
%wc = getWordCount(%msg);
for(%a=0;%a<%wc;%a++)
{
if(getWord(%msg, %a) $= %fw)
%fwl = %a;

else if(getWord(%msg, %a) $= %lw)
%lwl = %a;

if(%a >= 30)
return error("Stopping endless loop!");
}

return getWords(%msg, 0, (%fwl - 1)) SPC getWords(%msg, (%lwl + 1), %wc);
}
If you don't mind, please tell me what I did wrong.
« Last Edit: June 22, 2013, 06:46:36 PM by Honorabl3 »

Add echos at various points in your code and use setLogMode(1); , then run your function and check console.log after Blockland crashed.

Why change the log mode? All that does is close the file after it writes each line.

Why change the log mode? All that does is close the file after it writes each line.

If you don't do that, the echos from your function won't reach console.log before the game crashes.

I placed three echo tests in the function, all displayed in console before crashing.

I placed three echo tests in the function, all displayed in console before crashing.
Then apparently it is after those three echos.

Can you return the error function like that? Lol, what does that function even return?

Can you return the error function like that? Lol, what does that function even return?
The point is that it breaks out of the extraction function and errors at the same time. What I don't understand is why you have the possibility of an endless loop. Is there any situation when %wc wouldn't be correctly defined

Can you return the error function like that? Lol, what does that function even return?
It will say something about using return value of void function call in the console

I added that stop to the loop originally because I thought it was crashing from the loop somehow; going on forever.
« Last Edit: June 22, 2013, 07:06:32 PM by Honorabl3 »

I added that stop to the loop originally because I thought it was crashing from the loop somehow; going on forever.
Add more echos to check.

Is it possible the string is becoming larger than 4095 characters long? Supposedly that crashes Blockland (although in my experience that only happens if you DISPLAY a string that long).

Is it possible the string is becoming larger than 4095 characters long? Supposedly that crashes Blockland (although in my experience that only happens if you DISPLAY a string that long).
I've had weird instances where my script doesn't cause Blockland to crash. But, that's when I am only experimenting it on small strings. Although, when I try to recreate them - it sometimes doesn't work?
« Last Edit: June 23, 2013, 11:40:33 PM by Honorabl3 »

I've had weird instances where my script doesn't cause Blockland to crash. But, that's when I am only experimenting it on small strings.

Weird instances that follow a pattern are not weird instances. Try echoing the strLen of the string at various points in the function.

RESULTS:





So by my understanding, what is causing this thing to crash:
- It is either incorrectly executing the ending return.
- Echo'ing the results are causing it to crash. - Tested it without echo'ing it, seems to crash as-well.

More edits:

Code: [Select]
return getWords(%msg, 0, (%fwl - 1)) SPC getWords(%msg, (%lwl + 1), %wc);%fwl, and/or %lwl is being defined wrongly. Edit: Which also means that %fw, and %lw could be defined wrongly.

More important update with question:

%phrase = "dealer-is";
%dp = 6;
Code: [Select]
%lw = strchr(%phrase, (%dp + 1));It seems the code above doesn't define %lw correctly. As %lw isn't being defined at all.
« Last Edit: June 24, 2013, 01:08:54 AM by Honorabl3 »