SRL Archive - SRL Suggestions

IsChatBoxTextAnyLineMulti


mpd

2012-04-16 14:23:07

Hello,

The follow code takes multiple words and multiple colors to search in the chatbox.
It will search all words in all colors.

(*
IsChatBoxTextAnyLineMulti
~~~~~~~~~~~~~~~~~~~~

.. code-block:: pascal

function IsChatBoxTextAnyLineMulti(Text: TStringArray; Colors: TIntegerArray): Boolean;

Just like IsChatBoxTextAnyLine, only takes an array of text to search and an array of colors.
Each text is searched in each color

Example:

.. code-block:: pascal

if ( IsChatBoxTextAnyLineMulti( ['bot', 'other'], [16711680, 239]) ) then

*)
function IsChatBoxTextAnyLineMulti(Text: TStringArray; Colors: TIntegerArray): Boolean;
var
i, j, tCounter, cCounter: Integer;
begin
tCounter := High(Text);
cCounter := High(Colors);
for i := 0 to tCounter do
for j := 0 to cCounter do
if ( IsChatBoxTextAnyLine(Text[i], Colors[j]) ) then
begin
Result := True;
Exit;
end;
end;

putonajonny

2012-04-16 14:31:29

Looks good, btw just wondering if you know…

is there a reason not to just do this:

for i := 0 to High(Text) do
for j := 0 to High(Colors) do
I have seen loads of scripts where people have done it like you do, is there a reason to?

Abu

2012-04-16 14:36:39

Instead of using tCounter and cCounter, use t and c, it'll save a few milliseconds;)

mpd

2012-04-16 14:39:31

In this case there isn't a reason to, I figure there could be if you would reuse the variable, then it wouldn't be affected by any function name changes.

I only realised myself it isn't necessary untill I posted the code.

masterBB

2012-04-16 14:41:49

From: putonajonny
Looks good, btw just wondering if you know…

is there a reason not to just do this:

for i := 0 to High(Text) do
for j := 0 to High(Colors) do
I have seen loads of scripts where people have done it like you do, is there a reason to?


Yes, it will make a function call every loop. Though a function call will not take that long, functions like this should be the fastest way possible.

From: abu_jwka
Instead of using tCounter and cCounter, use t and c, it'll save a few milliseconds;)


Are you sure? I thought that it didn't matter anymore, maybe that was in LAPE ;). I recommend using full names though, for the readability.

edit:
I just read what this actually does. There is no reason to speed it up. I do recommend to call freeze and unfreeze. This will make a significantly performance improvement:
function IsChatBoxTextAnyLineMulti(Text: TStringArray; Colors: TIntegerArray): Boolean;
var
i, j: Integer;
begin
Freeze;
for i := High(Text) downto 0 do
for j := High(Colors) downto 0 do
if ( IsChatBoxTextAnyLine(Text[i], Colors[j]) ) then
begin
Result := True;
Exit;
end;
UnFreeze;
end;

Abu

2012-04-16 14:45:22

From: masterBB

Are you sure? I thought that it didn't matter anymore, maybe that was in LAPE ;). I recommend using full names though, for the readability.


I dunno I've ready many tutorials where they used one letter integers to represent Array Lengths and said it saves time…

masterBB

2012-04-16 14:49:36

From: abu_jwka
I dunno I've ready many tutorials where they used one letter integers to represent Array Lengths and said it saves time…


Yeah, maybe that is pascalscript. I know in most other languages it doesn't matter. I hope lape doesn't got the same problem. Notice how I removed it entirely in the above code. Also it doesn't really matter, the function calls to the other chat box read function takes 99.99% of the time. The freeze will make a big difference though.

mpd

2012-04-16 14:56:44

From: masterBB
Yeah, maybe that is pascalscript. I know in most other languages it doesn't matter. I hope lape doesn't got the same problem. Notice how I removed it entirely in the above code. Also it doesn't really matter, the function calls to the other chat box read function takes 99.99% of the time. The freeze will make a big difference though.


I do get an error if I add the freeze in text.simba:

Error: Exception: Cannot get image target while frozen at line 832

Edit: The error occurs in text.simba

masterBB

2012-04-16 14:59:27

From: mpd
I do get an error if I add the freeze in text.simba:

Error: Exception: Cannot get image target while frozen at line 832


Maybe it is only possible in pascal… I will look into it.

masterBB

2012-04-16 15:40:11

I don't know why it happens… It shouldn't. The idea of using freeze and unfreeze was that you will create a buffer from the screen. This buffer will be used multiple times, instead of getting the buffer again and again. Normally that will take a good amount of time with every color search. And though it works with the other color find function the OCR seems to have some troubles with it. The exception is raised by the following function in the input output manager:

procedure TIOManager_Abstract.GetImageTarget(var idx: integer);
begin
if IsFrozen then
raise Exception.Create('Cannot get image target while frozen');
idx:= GetTargetIdx(image);
end;

I might have already lost you there. But I can't find a fix for it at this moment.

mpd

2012-04-16 16:25:37

I understand you perfectly fine and thank you for looking into this matter. I'll see if I can come up with a solution myself aswell.

Edit:

I understand why it throws the error: Whenever there's a text found, it results true and exits while leaving the screen frozen.

function IsChatBoxTextAnyLineMulti(Text: TStringArray; Colors: TIntegerArray): Boolean;
var
i, j: Integer;
begin
Freeze;
for i := High(Text) downto 0 do
for j := High(Colors) downto 0 do
if ( IsChatBoxTextAnyLine(Text[i], Colors[j]) ) then
begin
Result := True;
UnFreeze;
Exit;
end;
UnFreeze;
end;

masterBB

2012-04-16 16:46:34

From: mpd
I understand you perfectly fine and thank you for looking into this matter. I'll see if I can come up with a solution myself aswell.


Now I look into the color functions itself, I feel like an idiot. Though I do see some things that might improve performance, I'm not sure if it would make that much. I'm also fairly sure that it will be just a waste of time. I did find a double findcolortol method, so I might push that later, but that is not related to this suggestion

edit:
Nice you found the error ;). I'm already looking through the simba source for over an hour :P

Jakkle

2012-04-16 18:12:43

Why don't you just do it like this ?

function IsChatBoxTextAnyLineMulti(Text: TStringArray; Colors: TIntegerArray): Boolean;
var
i, j: Integer;
begin
Freeze;
for i := High(Text) downto 0 do
for j := High(Colors) downto 0 do
Result := (IsChatBoxTextAnyLine(Text[i], Colors[j]));
UnFreeze;
end;

masterBB

2012-04-16 18:21:14

From: Jakkle
Why don't you just do it like this ?

function IsChatBoxTextAnyLineMulti(Text: TStringArray; Colors: TIntegerArray): Boolean;
var
i, j: Integer;
begin
Freeze;
for i := High(Text) downto 0 do
for j := High(Colors) downto 0 do
Result := (IsChatBoxTextAnyLine(Text[i], Colors[j]));
UnFreeze;
end;


Cause your code doesn't work. It needs to exit at the first found true. Let's say color 1 is true and color 2 is false. It will just set the result at false instead of true.

Jakkle

2012-04-16 18:25:33

From: masterBB
Cause your code doesn't work. It needs to exit at the first found true. Let's say color 1 is true and color 2 is false. It will just set the result at false instead of true.


Ah yeah I see the reason it's done like that now. Thanks

masterBB

2012-04-16 18:29:29

From: Jakkle
Ah yeah I see the reason it's done like that now. Thanks


In Lape it could look something like:
function IsChatBoxTextAnyLineMulti(Text: TStringArray; Colors: TIntegerArray): Boolean;
var
i, j: Integer;
begin
Freeze;
for i := High(Text) downto 0 do
for j := High(Colors) downto 0 do
if (Result := IsChatBoxTextAnyLine(Text[i], Colors[j])) then
Break(2);
UnFreeze;
end;

Can't wait till we dump pascalscript.

Jakkle

2012-04-16 18:40:04

From: masterBB
In Lape it could look something like:
function IsChatBoxTextAnyLineMulti(Text: TStringArray; Colors: TIntegerArray): Boolean;
var
i, j: Integer;
begin
Freeze;
for i := High(Text) downto 0 do
for j := High(Colors) downto 0 do
if (Result := IsChatBoxTextAnyLine(Text[i], Colors[j])) then
Break(2);
UnFreeze;
end;

Can't wait till we dump pascalscript.


Ah I see, looks better.
What about this,
[SIMBA]function IsChatBoxTextAnyLineMulti(Text: TStringArray; Colors: TIntegerArray): Boolean;
var
i, j: Integer;
begin
Freeze;
for i := High(Text) downto 0 do
for j := High(Colors) downto 0 do
if (IsChatBoxTextAnyLine(Text[i], Colors[j])) then
Result := True
UnFreeze;
end;
I was just thinking of how to make it shorter thats all :p

masterBB

2012-04-16 18:44:52

From: Jakkle
Ah I see, looks better.
What about this,
[SIMBA]function IsChatBoxTextAnyLineMulti(Text: TStringArray; Colors: TIntegerArray): Boolean;
var
i, j: Integer;
begin
Freeze;
for i := High(Text) downto 0 do
for j := High(Colors) downto 0 do
if (IsChatBoxTextAnyLine(Text[i], Colors[j])) then
Result := True
UnFreeze;
end;
I was just thinking of how to make it shorter thats all :p


You just moved the problem one line ;).

Jakkle

2012-04-16 19:28:05

From: masterBB
You just moved the problem one line ;).


Haha not thinking at all there :duh:

Daniel

2012-04-21 09:53:47

From: putonajonny
Looks good, btw just wondering if you know…

is there a reason not to just do this:

for i := 0 to High(Text) do
for j := 0 to High(Colors) do
I have seen loads of scripts where people have done it like you do, is there a reason to?

Supposedly "faster". Although the difference would be so insignificant, it doesn't really matter in this instance (if you are doing quite a bit of iterations, it may save at most 5ms).

From: abu_jwka
Instead of using tCounter and cCounter, use t and c, it'll save a few milliseconds;)


No it won't.