• Print

Author Topic: Executing ULX commands on server startup  (Read 5625 times)

0 Members and 1 Guest are viewing this topic.

Offline Splitzle

  • Newbie
  • *
  • Posts: 6
  • Karma: 1
Executing ULX commands on server startup
« on: July 22, 2017, 12:01:18 pm »
How would I go about doing this? If I put "+ulx command" in the server batch file, it will not recognize it as a command since it is executed before ulx/ulib are able to load. I read that I could put a lua file in the lua/autorun/server directory and I tried my best to make one, but I am not very good at lua to begin with.

Code: [Select]
local function RandomMap ( changeTo, ply ) --No idea if these are correct or needed

RunConsoleCommand("ulx", "randommap")

end

I'm trying to do the "ulx randommap" command on startup. No errors were shown in console when I started it, so now I need help. Here is the RandomMap module if that helps.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Executing ULX commands on server startup
« Reply #1 on: July 22, 2017, 01:10:01 pm »
You could use an GM:Initialize hook inside a ULX modules folder for example,
Code: [Select]
addons/your_custom_name/ulx/modules/sv/randommap.lua to make sure that ULX is initialized before it is loaded.

Code: Lua
  1. local function randomMap() -- Arguments are not required unless you want to call this function directly from a ULX command
  2.     RunConsoleCommand( "ulx", "randommap" )
  3. end
  4. hook.Add( "Initialize", "Random Map on Startup", randomMap )
  5.  
So you had it correct, just without something to tell it to run (the hook). Anything in 'autorun' directory is loaded automatically, but you need something to tell it to actually run.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline Splitzle

  • Newbie
  • *
  • Posts: 6
  • Karma: 1
Re: Executing ULX commands on server startup
« Reply #2 on: July 22, 2017, 03:18:23 pm »
I made the file like you said, but now this appears in the server console -

Code: [Select]
Warning: Player issued command but is now vanished (Command was ""randommap"")
Is it confusing the command with a player issued command?

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Executing ULX commands on server startup
« Reply #3 on: July 24, 2017, 11:38:26 am »
It is tricky to detect when the server starts up without the help of external scripts. There are no hooks, variables or functions that tell us this kind of information.

The only thing I found that seems to persists across sessions is the connection ID. This ID starts at 2 and increments by 1 every time a player (or bot) joins the server. This ID resets when the server shuts down but not when the server changes maps.

This can be used to create a function that gets called every time the server starts its first session:
Code: Lua
  1. -- lua/ulx/modules/startup.lua
  2.  
  3. local function onFirstSession()
  4.     RunConsoleCommand( "ulx", "randommap" )
  5. end
  6.  
  7. hook.Add( "Think", "check-first-session", function()
  8.     local bot = player.CreateNextBot( "Bot" )
  9.  
  10.     if IsValid( bot ) then
  11.         hook.Add( ULib.HOOK_UCLAUTH, "auth-startup-bot", function()
  12.             if bot:UserID() == 2 then onFirstSession() end
  13.             bot:Kick()
  14.             hook.Remove( ULib.HOOK_UCLAUTH, "auth-startup-bot" )
  15.         end )
  16.     end
  17.  
  18.     hook.Remove( "Think", "check-first-session" )
  19. end )

Update: Much better and simpler approach:
Code: Lua
  1. -- lua/ulx/modules/startup.lua
  2.  
  3. hook.Add( ulx.HOOK_ULXDONELOADING, "random_map_on_first_session", function()
  4.     if game.GetGlobalCounter( "ulx_randommap" ) == 0 then
  5.         game.SetGlobalCounter( "ulx_randommap", 1 )
  6.         RunConsoleCommand( "ulx", "randommap" )
  7.     end
  8.     hook.Remove( "Think", "random_map_on_first_session" )
  9. end )
« Last Edit: August 24, 2017, 04:47:10 am by Timmy »

Offline Splitzle

  • Newbie
  • *
  • Posts: 6
  • Karma: 1
Re: Executing ULX commands on server startup
« Reply #4 on: July 24, 2017, 03:48:07 pm »
Thank you Timmy, this worked perfectly. Also thanks Viscosity since now I now the actual purpose of those arguments, lol

  • Print