SRTT Advice needed on a lua file (Sandbox+)

[V] IdolNinja

Volition Staff
Sandbox+ for SRTT currently uses sr3_city.lua to create new threads that do things like monitor keyboard input and the game clock to advance tod. The problem is that this scriopt is only loaded for the host and not the client in a coop game. meaning only the host can use the keyboard shortcuts.

Any thoughts on a good alternate script to use that loads for both players in a coop game so the client would also be able to use them too?
 
Unfortunately, I don't believe there is any gameplay scripting running on the client. It looks like game_lib.lua is loaded, but no threads are ever started. Level scripts are not loaded, much less run.

If we are able to enable the console for you, you may be able to load/execute lua through that. The console may also allow you to set up some keyboard shortcuts using the internal system - the same way we set them up during development. That's going to depend on what we're able to do there.
 
Thanks for the reply, Rob.

In theory, I could possibly create my own thread in game_lib.lua though, right? That's how I approached it in sr3_city.lua; i.e. creating my own thread instead of using an existing one. I just added it in to sr3_city_main() which gets processed on save load/new game.

NOTE: The following is an edited version with just the relevant code we're discussing and one condition as an example (pushing SPRINT+INSERT.) See the actual release thread for the full script if you want to look at it.

Code:
local KEYCOMBO_THREAD_ON = 1
local Keycombo_Handle = INVALID_THREAD_HANDLE
 
function sr3_city_main()
    Keycombo_Handle = thread_new("keycombo_thread")
end
 
function keycombo_thread()
    while KEYCOMBO_THREAD_ON == 1 do
        if player_action_is_pressed("CBA_OFC_SPRINT") then -- SHIFT (sprint) - TELEPORTS
            if player_action_is_pressed(B_INSERT) and not SPRINT_PUSHED then
                --TELEPORT TO SAFEWORD
                local tposx,tposy,tposz = (-1170.99 - Dock_Posx), (-514.88 - Dock_Posy), 39.94
                teleport_to_object(LOCAL_PLAYER, "Dock_Purchase", false, false, tposx , tposz, tposy , false)
                if COOP_COMMANDS and coop_is_active() then
                    teleport_to_object(REMOTE_PLAYER, "Dock_Purchase", false, false, tposx , tposz, tposy , false)
                end
                sandboxplus_message("Teleported to Safeword")
                SPRINT_PUSHED = true
            elseif player_action_is_pressed(B_HOME) and not SPRINT_PUSHED then
...

So, is there a function in game_lib.lua that is called on save load/new game that is similar to sr3_city_main() that I could create a new thread for? If not, I'm looking forward to those possible alternatives you mentioned. :)
 
Rob is correct - for SR2/SR3 we only ran gameplay Lua scripts on the host . However...

game_lib.lua is loaded and executed during level initialization, right after the Gameplay Lua state is created and all of the custom SR C-side Lua functions are registered with that state. This happens on both the host and client, which means you can spawn a new Lua gameplay "thread" from game_lib.lua and it will run on both the host/client. In Lua, you are allowed to add "loose" instructions outside of function definitions, so just add a call to thread_new() at the very bottom of game_lib.lua (or anywhere in the file) to create the thread you want to have running on both machines.

For simple scripts this approach should work just fine. But fair warning - many of the SR specific C-side Lua functions make the assumption that they will only ever be run on the host and will not work correctly or even cause unintended side-effects/crashes when called on the client.
 
Matt, thanks for the tip. I had no idea you could actually execute commands like that outside of functions. That will be a big help for future scripting I have planned.
 
SR2 has no game_lib.lua so I attempted Matt's solution inside of system_lib.lua which resulted in a crash (although this may be down to my rusty understanding of Lua + poor copy-pasting.)
 

Attachments

SR2 has no game_lib.lua so I attempted Matt's solution inside of system_lib.lua which resulted in a crash (although this may be down to my rusty understanding of Lua + poor copy-pasting.)

You missed a closing end when you moved over all my code to the new script.

Code:
function keycombo_thread()
    while (1) do
        if player_button_just_pressed(PC_BTN_SELECT_WEAPON_1) then
                teleport("#PLAYER#", "airport_$nav-warp")
                sandbox_message("Teleported to Airport", LOCAL_PLAYER)
                reset_input_sequence()
                end
                end
^end
 
InnocentSam, Where is keycombo_thread() defined??

[Edit] I found it, and IdolNinja already pointed out the issue with the missing end.
[Edit2] Please note that system_lib.lua is likely loaded pretty early on, so many of the SR2 Lua functions may not yet be registered to the Lua state. I don't have the SR2 source code handy to look ATM.
 
Back
Top