Hi All,
I've just started using scripts and I can't seem to find a way to have a script that runs in the background at some constant interval to be perform something.
I tried to set it as global task but that didn't work.
Anyway this.can be achieved?
Running a script in the background
Moderators: Scripting / Modding Moderators, Moderators for English X Forum, Moderators for the X3:FL Forums
-
- Posts: 2009
- Joined: Tue, 2. Dec 08, 01:00
Re: Running a script in the background
First off, I made this script for AP so it may or may not work in FL. Testing is definitely recommended. All right, onwards ...
I used a while-loop to do this in my personal mod. Probably not the most elegant solution, but it worked for me.
It goes a little something like this ...
A brief breakdown:
"$timer.task" is a variable that exist outside the loop and will provide a reference point. "playing time" is a script engine function that returns how many seconds have passed since a new game was started or a save was loaded.
"while 1" creates an endless loop. Make sure you check and double check your code, as a runaway loop can crash your game if you're not careful. Been there, done that.
"$time.var" tell how many seconds have passed since $timer.task was last updated. In this example it goes up in 20 second increments.
"if $time.var >= 600" is an IF loop that runs when $time.var equal is equal to or exceed 600 seconds.
"$timer.task = $playtime" update the reference value and make sure the time since last check is calculated correctly.
"wait 20000 ms" adds a 20 second wait at the end. It's there to keep the loop from running continuously and potentially bog down the game. After the wait is over the script will jump back to the while statement and start over.
Footnote: You will have to manually start this through the script engine interface. If you want it to start when you start a new game or load a save, you will have to make a startup script to turn it on.
I used a while-loop to do this in my personal mod. Probably not the most elegant solution, but it worked for me.
It goes a little something like this ...
Code: Select all
$timer.task = playing time
while 1
$playtime = playing time
$time.var = $playtime - $timer.task
if $time.var >= 600
$timer.task = $playtime
*insert thing you want to do here*
end
@ = wait 20000 ms
end
return null
"$timer.task" is a variable that exist outside the loop and will provide a reference point. "playing time" is a script engine function that returns how many seconds have passed since a new game was started or a save was loaded.
"while 1" creates an endless loop. Make sure you check and double check your code, as a runaway loop can crash your game if you're not careful. Been there, done that.
"$time.var" tell how many seconds have passed since $timer.task was last updated. In this example it goes up in 20 second increments.
"if $time.var >= 600" is an IF loop that runs when $time.var equal is equal to or exceed 600 seconds.
"$timer.task = $playtime" update the reference value and make sure the time since last check is calculated correctly.
"wait 20000 ms" adds a 20 second wait at the end. It's there to keep the loop from running continuously and potentially bog down the game. After the wait is over the script will jump back to the while statement and start over.
Footnote: You will have to manually start this through the script engine interface. If you want it to start when you start a new game or load a save, you will have to make a startup script to turn it on.
I'm sorry, I can't hear you over the sound of how awesome I am 
DiDs:
Eye of the storm Completed
Eye of the storm - book 2 Inactive
Black Sun - Completed
Endgame - Completed

DiDs:
Eye of the storm Completed
Eye of the storm - book 2 Inactive
Black Sun - Completed
Endgame - Completed
-
- Moderator (Script&Mod)
- Posts: 22436
- Joined: Sun, 14. Nov 04, 23:26
Re: Running a script in the background
a few things to add. If you plan to use a setup script to run this automatically, you will need to add lock, so it only runs once. A setup script runs everytime you load you game, so it will create a new instance each time you load.
so at the start of the script, you can store a global variable so you know its running
This prevents the script from running again if one is already running. Using the script name as the unique global variable
using the pid check also means if the script is to stop for some reason, then it can still be run again even if the global variable is still set
Also, you may want to update the script in an existing save game, so you can add a version check
This will check if the version number has been changed in the script, kill the existing one, and start a new one.
Another way of doing this is using an AL plugin, which can run a script on a timer. this also has the advantage of allowing you to enable/disable it via the AL Plugin menu
so at the start of the script, you can store a global variable so you know its running
Code: Select all
$scriptname = get script name
$pid = get global variable: name=$scriptname
if $pid
do if is global script task running: pid=$pid
return null
end
set global variable: name=$scriptname value=$pid
while 1
.....
using the pid check also means if the script is to stop for some reason, then it can still be run again even if the global variable is still set
Also, you may want to update the script in an existing save game, so you can add a version check
Code: Select all
while 1
if new script version is available
$scriptname = get script name
set global variable: name: $scriptname value=null
START [NULL] -> call named script: $scriptname, null, null, null
return null
end
.....
Another way of doing this is using an AL plugin, which can run a script on a timer. this also has the advantage of allowing you to enable/disable it via the AL Plugin menu