SRL Archive - OSR Help

Threads & Painting


Iamadam

2011-04-01 19:52:47

Hey

Is there any way I can continuously paint to the main screen (every 100ms or so), while my script is running. Most languages would use a thread for this - and I remember SCAR used to have a GUI thread you could use for things like that.

Do we have any functionality like that in Simba?

Brandon

2011-04-01 20:48:48

I've tried looking for threading in simba and scar and the only virtual threading I've seen is SendInterScarMessage.. which is useless for what your looking to do.

My attempting at doing this "Threading"

Procedure PrintMainScreen;
begin
//Do printing here..
end;
Procedure1;
begin
//DoStuff
PrintSmart;
//DoStuff
PrintSmart;
end;

procedure2;
var
PrevTime: Integer;
begin
PrevTime = GetsystemTime;
//Do something.
if(GetsystemTime - PrevTime >= 100)
begin
do another stuff;
end;
PrevTime = GetsystemTime;
//Do something.
if(GetsystemTime - PrevTime >= 100)
begin
do another stuff;
end;
PrevTime = GetsystemTime;
//Do something.
if(GetsystemTime - PrevTime >= 100)
begin
do another stuff;
end;
PrevTime = GetsystemTime;
//Do something.
if(GetsystemTime - PrevTime >= 100)
begin
do another stuff;
end;

end;

//MainLoop
begin
Procedure1;
PrintMainScreen;
Procedure2;
PrintMainScreen;

end.

Coh3n

2011-04-02 03:15:48


program new;
{$DEFINE SMART}
{$i srl/srl.scar}
{$i srl/srl/misc/paintsmart.scar}

var
i: integer;
begin
Smart_Server := 152;
Smart_Members := False;
Smart_Signed := True;
Smart_SuperDetail := False;

SetupSRL;
ClearDebug;

for i := 1 to 100 do
begin
smart_DrawBox(intToBox(10+random(100), 10+random(100), 100+random(100), 100+random(100)));
wait(100);
end;

smart_FreeDebug();
end.
:)

Iamadam

2011-04-02 03:29:57

Lol.
Not what I meant Coh3n. I know how to draw to smart, maybe didn't phrase the question well.
I want the painting to be in a separate thread to the rest of my script.
But thanks anyway :P

Floor66

2011-04-02 03:32:52

In SCAR you could use TTimers but I haven't gotten them to work with Simba.

Coh3n

2011-04-02 14:49:30

From: Floor66
In SCAR you could use TTimers but I haven't gotten them to work with Simba.

MSI's form uses a TTimer, so they do work. I don't know how to use them, really, but they work. :p

And threading isn't possible with pascal script as far as I know.

Floor66

2011-04-02 16:05:04

I've tried to copy MSI's exact code and trim it down/modify it to WriteLn something every sec but it won't fricken work, so I gave up after like 20 tries :P

Coh3n

2011-04-02 17:36:23

From: Floor66
I've tried to copy MSI's exact code and trim it down/modify it to WriteLn something every sec but it won't fricken work, so I gave up after like 20 tries :P


program new;

var
timer: TTimer;
t: integer;

begin
clearDebug();

timer := TTimer.Create(nil);
timer.enabled := true;


t := (getSystemTime + 5000);
while timer.enabled do
begin
writeln('timer is enabled');
wait(20);

if (getSystemTime > t) then
timer.enabled := false;
end;

timer.Free;
end.

Floor66

2011-04-02 17:49:02

Yeah but there you're in that loop eh…
I'm talking about the way to 'multi-thread' using TTimers.

Coh3n

2011-04-02 17:53:33

Well how did you do it with SCAR?

Wizzup?

2011-04-02 17:55:40

From: Iamadam
Lol.
Not what I meant Coh3n. I know how to draw to smart, maybe didn't phrase the question well.
I want the painting to be in a separate thread to the rest of my script.
But thanks anyway :P


There is no true multithreading. I'm unsure why you'd want to do it in a different thread. But you could use TTimer, I'm pretty sure it should work. Just don't expect it to also have the speed advantages of multiple threads.

Floor: What Coh3n showed you should work fine for drawing in another ``thread''. Imagine replacing the wait(20); with the script mainloop. Recall that a TTimer event can occur during any moment in the script. (iow you don't need logic for it, I suppose that is why Iamadam wants it)

Dgby714

2011-04-02 17:56:12

From: Floor66
Yeah but there you're in that loop eh…
I'm talking about the way to 'multi-thread' using TTimers.


Thats not multithreading.

You can only use TTimers correctly in ThreadSafeCall!

Iamadam

2011-04-02 18:16:27

My script loops through the paint procedure often, but not often enough. If you want to paint a crosshair that follows a target, without affecting the functionality of the script (and without significant overhead), the best way would be to use a separate thread. If I want the crosshair to move smoothly between targets, I NEED another thread (can't be sacrificing script running speed to paint something) :P

Sad that we can't multithread, but no problem :) Doesn't matter really, just wanted to see what simba was capable of.

<troll>/me moves back to SCAR. </troll>

Coh3n

2011-04-02 18:20:40

From: Iamadam
My script loops through the paint procedure often, but not often enough. If you want to paint a crosshair that follows a target, without affecting the functionality of the script (and without significant overhead), the best way would be to use a separate thread. If I want the crosshair to move smoothly between targets, I NEED another thread (can't be sacrificing script running speed to paint something) :P

Sad that we can't multithread, but no problem :) Doesn't matter really, just wanted to see what simba was capable of.

<troll>/me moves back to SCAR. </troll>

Multi-threading isn't possible in SCAR either… They both use PS.

Floor66

2011-04-02 18:28:46

From: Dgby714
Thats not multithreading.

You can only use TTimers correctly in ThreadSafeCall!


Hence my quotation marks.

Nava2

2011-04-02 18:51:46

TTimers have a onTimer property which is a functional pointer. You could use this for pseudo-threading practice.

It works, I've used it before.

Floor66

2011-04-03 03:15:13

I have too, Nava, just not in simba.

@Wizzup,
I'll play with it some more =p

EDIT:
So why won't this work? :/
program new;

var
Thread: Array [0..1] of TTimer;
t, c: integer;

function ShitsEnabled: Boolean;
begin
Result := (Thread[0].Enabled and Thread[1].Enabled);
end;

procedure OnThread1(Sender: TObject);
var
i: Integer;
begin
for i := c downto 0 do
c := c - 1;
end;

procedure OnThread2(Sender: TObject);
begin
WriteLn('Thread 2 reporting in: '+ ToStr(c));
end;

begin
ClearDebug;
c := 500;
for t := 0 to High(Thread) do
begin
Thread[t] := TTimer.Create(nil);
Thread[t].Enabled := True;
end;

Thread[0].OnTimer := @OnThread1;
Thread[0].Interval := 250;
Thread[1].OnTimer := @OnThread2;
Thread[1].Interval := 1000;

while ShitsEnabled do
begin
Wait(10);
end;

for t := 0 to High(Thread) do
Thread[t].Free;
end.

Also, Coh3n, the script you posted doesn't use the OnTimer event :/ That's just what I need.