Waiting for a thread to end?

Post your scripting questions / solutions here

Moderator: Moderators

Bjarne BZR
Site Admin
Posts: 3298
Joined: Wed Feb 05, 2003 2:04 pm
Location: Sweden
Contact:

Waiting for a thread to end?

Post by Bjarne BZR »

I wonder:

If I have this method:

Code: Select all

a_method:
  wait 10
end
( Just a stooopid example method )

...and I want the calling script to be able to halt until the method has ended. I cant do this:

Code: Select all

thread a_method
...because that would create a separately executing thread and the calling script will continue on its next line.

I want something like:

Code: Select all

thread a_method wait_til_done
...or just...

Code: Select all

a_method
I'm pretty shure I saw the syntax for this somewhere, but I can't seem to remeber / find it again....

I'm writing a tutorial on scripting, so I want to get it right.... ( Feedback is welcome )
Admin .MAP Forums
Image
Head above heels.
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post by jv_map »

waitthread a_method

8)
Image
Bjarne BZR
Site Admin
Posts: 3298
Joined: Wed Feb 05, 2003 2:04 pm
Location: Sweden
Contact:

Post by Bjarne BZR »

Yep, thats the one :-)

And while I have your atension jv:

Passing parameters at thread creation, wats the syntax for that? ( I'm not talking about setting the self object )

And how do you access the parameters in the thread?
Admin .MAP Forums
Image
Head above heels.
User avatar
mohaa_rox
Field Marshal
Posts: 2760
Joined: Mon Nov 11, 2002 7:05 am
Contact:

Post by mohaa_rox »

and exec and wait is

waitexec right?
Live to map, not map to live.
-mohaa_rox, .map
moderator
Bjarne BZR
Site Admin
Posts: 3298
Joined: Wed Feb 05, 2003 2:04 pm
Location: Sweden
Contact:

Post by Bjarne BZR »

This exec command... is that like:

exec <scriptfile>::label

is equal to

waitthread label

?

I havent really figured the exec command out...
Admin .MAP Forums
Image
Head above heels.
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post by jv_map »

waitexec is a little different as it waits for the entire thread group to finish.

To pass an argument:

Code: Select all

waitthread mythread 1 2 3

mythread local.one local.two local.three:
    println "I can count to three: " local.one " " local.two " " local.three
end
:)
Image
User avatar
mohaa_rox
Field Marshal
Posts: 2760
Joined: Mon Nov 11, 2002 7:05 am
Contact:

Post by mohaa_rox »

Maybe not, that's a thread and that's to execute. Things like "exploders", bombers, etc use "exec" at the top before level waittill spawn. And I think waitthreads are mainly used for xxx.scr::xx like objectives, and "exec" are maybe like "exec global/missioncomplete.scr".

In SP, it's like this:

levelend:
waitthread global/objectives.scr::current_objectives 0
wait 1
exec global/missioncomplete.scr
end
Live to map, not map to live.
-mohaa_rox, .map
moderator
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post by jv_map »

The only difference I know is that with exec you only have to supply a script file, thread actually requires a label.

Example:
exec global/exploder.scr
thread global/exploder.scr::main // (or something)
Image
User avatar
mohaa_rox
Field Marshal
Posts: 2760
Joined: Mon Nov 11, 2002 7:05 am
Contact:

Post by mohaa_rox »

Yeah, and

exec global/weather.scr
waitthread global/weather.scr::main

I think exec the .scr is the WHOLE thing, while thread the main may only be that part, provided if they don't link the threads.

I dunno.
Live to map, not map to live.
-mohaa_rox, .map
moderator
Bjarne BZR
Site Admin
Posts: 3298
Joined: Wed Feb 05, 2003 2:04 pm
Location: Sweden
Contact:

Post by Bjarne BZR »

So... if I use waitexec XXX, it will not return control to the caller until all the threads in the group has terminated ( including calls like thread YYY inside the XXX method) ?

But if I use waitthread XXX, it WILL return control to the caller as soon as the waitthread XXX has terminated ( ignoring if a thread like thread YYY that is created inside the XXX method has not terminated yet ) ?

Wild guessing here... set me straight...

( can you write waitexec XXX at all without defining the fully qualified label, including the script file name and path? )
Admin .MAP Forums
Image
Head above heels.
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post by jv_map »

Yes, all correct, but remember thread groups are a bit weird. In most cases, a new thread is part of a seperate thread group. A thread only belongs to the parent thread group if it's in the same file and self is not defined (equal to self of parent thread).

Example:

Code: Select all

$myobject waitexec <myscript>::mythread

mythread:
    thread myotherthreada
    self thread myotherthreadb
end

myotherthreada:
    wait 5
end

myotherthreadb:
    wait 10
end
In this example, myotherthreada belongs to the same thread group as mythread, but myotherthreadb has its own thread group (since it has 'self' in front of the call). Therefore the waitexec line will only wait till myotherthreada is finished.
Image
User avatar
tltrude
Chuck Norris
Posts: 4774
Joined: Sun Jul 07, 2002 4:03 am
Location: Oklahoma, USA
Contact:

Pop quiz for Bjarne BZR

Post by tltrude »

Ok here is a pop quiz for Bjarne BZR:

In the above example, how long will $myobject wait?

A. 5 seconds
B. 10 seconds
C. 15 seconds
D. Forever
E. None of the above
Tom Trude,

Image
Bjarne BZR
Site Admin
Posts: 3298
Joined: Wed Feb 05, 2003 2:04 pm
Location: Sweden
Contact:

Post by Bjarne BZR »

Pop quiz!!!! Wohoooo! :D

The anwer is E. None of the above...
...because the call $myobject waitexec <myscript>::mythread will fail. ( You can't name a file <myscript> ).

:twisted:

Thanx a bunch JV!
Admin .MAP Forums
Image
Head above heels.
User avatar
mohaa_rox
Field Marshal
Posts: 2760
Joined: Mon Nov 11, 2002 7:05 am
Contact:

Post by mohaa_rox »

THis works for an AI:

$stupidAI waitexec global/standstill.scr
OR
$stupidAI waitexec global/disable_ai.scr

Right?

And a driveable tank:

$mytank waitthread global/vehicles_thinkers.scr::players_tank

Mountable jeep:
$myjeep waitthread global/vehicles_thinker.scr::player_jeep_controller
Live to map, not map to live.
-mohaa_rox, .map
moderator
Bjarne BZR
Site Admin
Posts: 3298
Joined: Wed Feb 05, 2003 2:04 pm
Location: Sweden
Contact:

Post by Bjarne BZR »

Oh, and I found a command named 'throw( String label )' with the chrystal clear explanation: "Throws to the specified label.".

As my guess here is that its not an Exception throw, what is it? Anyone?
Admin .MAP Forums
Image
Head above heels.
Post Reply