OSP Code Optimisation Call script after X seconds

Users who are viewing this thread

Many of us faced with this problem: I want to execute a script after something hapened, after some seconds. Normally, to solve this, you would need to make a trigger that will check every second if it needs to execute that specific thing. For example, if you would want to send a string to player after 5 seconds after he joined the server, you would make a slot in wich you would store the time when the player joined, and the you would have a trigger that would check every second every player to see if the difference between the actual time and the store time is greater than 5 seconds...pretty hard to make, especially for a beginner.

So to make things little easier, I've designed a script that will automatically execute what script you put in the calling of it.
The calling syntax is this: (call_script, "script_csaxs", <deci seconds>, <script_id>, <num_params>, <param_1>, <param_2>,...).

The script with <script_id> will be called , with those parameters that you specify, after <deci seconds> that you put in. <num params> represents how many parameter the script will be called with. <deci seconds> means 10 * seconds that you want. So if you want 2 seconds, you put 20, if you want 4.1 seconds, you put 41.

script_csaxs - call script after x seconds

Here is an example of how you can use it to send a string after 5 seconds after the player joined the server:
1. make a script, call it "send_after_5_seconds" and inside it put
Code:
(store_script_param_1, ":player_id"),
(try_begin), 
   (player_is_active, ":player_id"),
   (multiplayer_send_string_to_player, ":player_id", multiplayer_event_show_server_message,"@Example string that you should see after 5 seconds of joining"),
(try_end),

2. go into the trigger ti_server_player_joined, and at the bottom of it paste this:
Code:
(call_script, "script_csaxs", 5 * 10, "script_send_after_5_seconds", 1, ":player_no"),

3. Done!

Now how to install this:
1. Open module_constants and put this somewhere inside the file
slot_call_script_after_x_seconds = 900
  Note: you can change the slot number, but make sure that the following 11 slots are not used (so 901, 902,...911 are not used)
2. Open module_scripts and in the script "game_quick_start" put this
(assign, "$call_script_after_x_seconds_last", -1),
Then paste this script at the bottom of the file, before the last ']'
("csaxs",[
    (store_script_param, ":time", 1),
    (store_script_param, ":script_id", 2),
    (store_script_param, ":script_num_params", 3),

(store_mission_timer_a_msec, ":cur_time"),
(val_mul, ":time", 100),
(val_add, ":time", ":cur_time"),

(assign, ":end_cond", "trp_relative_of_merchants_end"), #last troop in module troops
(try_for_range, ":cur_var", 0, ":end_cond"),
(troop_slot_eq, ":cur_var", slot_call_script_after_x_seconds, 0),
     
      (assign, ":end_cond", 0),#stop the loop
     
      (try_for_range, ":cur_param", 0, 10),#setting all params to 0
        (assign, ":slot", slot_call_script_after_x_seconds + 2),
        (val_add, ":slot", ":cur_param"),
        (troop_set_slot, ":cur_var", ":slot", 0),
      (try_end),
     
     
      (try_for_range, ":cur_param", 0, ":script_num_params"),
        (assign, ":slot", slot_call_script_after_x_seconds + 2),
        (val_add, ":slot", ":cur_param"),
       
        (val_add, ":cur_param", 4),
        (store_script_param, ":script_param", ":cur_param"),
       
        (troop_set_slot, ":cur_var", ":slot", ":script_param"),
      (try_end),
      (troop_set_slot, ":cur_var", slot_call_script_after_x_seconds, ":time"),
(troop_set_slot, ":cur_var", slot_call_script_after_x_seconds + 1, ":script_id"),
     
      (try_begin),
        (gt, ":cur_var", "$call_script_after_x_seconds_last"),
          (assign, "$call_script_after_x_seconds_last", ":cur_var"),
      (try_end),
(try_end),
  ]),
Note: you can rename the script to whatever you want.

3.Open module_mission_templates and paste this before 'multiplayer_server_check_belfry_movement'
call_script_after_x_seconds = (0.05,0,0,[],[
(store_mission_timer_a_msec, ":cur_time"),
  (store_add, ":end_cond", 1, "$call_script_after_x_seconds_last"),
(try_for_range, ":cur_var", 0, ":end_cond"),
    (neg|troop_slot_eq, ":cur_var", slot_call_script_after_x_seconds, 0),
 
(this_or_next|neg|troop_slot_ge, ":cur_var", slot_call_script_after_x_seconds, ":cur_time"),
    (troop_slot_eq, ":cur_var", slot_call_script_after_x_seconds, ":cur_time"),
   
(troop_set_slot, ":cur_var", slot_call_script_after_x_seconds, 0),
   
    (try_begin),
      (eq, "$call_script_after_x_seconds_last", ":cur_var"),
      (assign, ":temp_var", 0),
      (try_for_range_backwards, ":cur_trp", ":temp_var", "$call_script_after_x_seconds_last"),
        (neg|troop_slot_eq, ":cur_trp", slot_call_script_after_x_seconds, 0),
        (assign, ":temp_var", 9999), 
      (try_end),
     
      (try_begin),
        (eq, ":temp_var", 9999),
          (assign, "$call_script_after_x_seconds_last", ":cur_trp"),
      (else_try),
          (assign, "$call_script_after_x_seconds_last", -1),
      (try_end),
    (try_end),
   
   
(troop_get_slot, ":script", ":cur_var", slot_call_script_after_x_seconds + 1),
(troop_get_slot, ":param1", ":cur_var", slot_call_script_after_x_seconds + 2),
(troop_get_slot, ":param2", ":cur_var", slot_call_script_after_x_seconds + 3),
(troop_get_slot, ":param3", ":cur_var", slot_call_script_after_x_seconds + 4),
(troop_get_slot, ":param4", ":cur_var", slot_call_script_after_x_seconds + 5),
(troop_get_slot, ":param5", ":cur_var", slot_call_script_after_x_seconds + 6),
(troop_get_slot, ":param6", ":cur_var", slot_call_script_after_x_seconds + 7),
(troop_get_slot, ":param7", ":cur_var", slot_call_script_after_x_seconds + 8 ),
(troop_get_slot, ":param8", ":cur_var", slot_call_script_after_x_seconds + 9),
(troop_get_slot, ":param9", ":cur_var", slot_call_script_after_x_seconds + 10),
(troop_get_slot, ":param10", ":cur_var", slot_call_script_after_x_seconds + 11),
     
(call_script, ":script", ":param1", ":param2", ":param3", ":param4", ":param5", ":param6", ":param7", ":param8", ":param9", ":param10"),
(try_end),
])

After that, go into each multiplayer mission template and paste this
call_script_after_x_seconds,

4. Done!

Limitations: make sure that there are no more than 929 scripts that are waiting to be executed; the max value that <num_params> can have is 10.

Note: if your last troop in module_troops is not "relative_of_merchants_end", then go into module scripts into my script, and change this line
(assign, ":end_cond", "trp_relative_of_merchants_end"),

If you want to use it in your mod, don't ask for permission, just make sure that my name is in the credits.
 
Blobmania said:
Can't you just normally use a mission_template to the same effect?

So...let's say that you want to freeze an agent for 5 seconds when you hit it. Normally you would need a slot, in wich you will store the time when the agent was hit. Then each second a trigger would run and check for every agent to see if it needs to unfreeze. All this thing of time checking can be resolved using my script. With my script, when the agent is freezed, you then call my script and pass in 50 deciseconds and a script that will unfreeze that specific agent, wich you would pass as a parameter. See the differences? No other triggers needed. Also if you would want to do another thing that should fire after idk how many seconds after something hapened, you would need to make another trigger, another slot, other checks for timing.

I consider that using my code is by far more easier to do such a thing, rather than complicating with trigger and stuffs.
 
Back
Top Bottom