Scrap Mechanic

Scrap Mechanic

Not enough ratings
Game Hook
By wingcomstriker405
In this guide you will learn how to use the Game Hook library.
   
Award
Favorite
Favorited
Unfavorite
Disclaimer
This mod allows you to access the Global Environment of the game and the Game / World script instances without modifying the files.
Besides that you can also register your own Commands or add Hook for specific functions.
Please keep in mind that this method has its Limitations and requires a little Setup.
The hole library is located under sm.hook. The game and world sections are in the subdomain sm.hook.game and sm.hook.world.
Some data is stored in sm.hook.data but its not recommended to mess with that data.

If you find bugs let me know so that i can try to fix them :)

NOTE: This library is not finished so the API might change in the future!
If the devs decide change the script so that it can't be hooked (which hope they won't do) this library will be broken!
Setup
In this section I want to show you how to setup the hook.
Add this dofile to the top of the file.
dofile("$CONTENT_d398b515-50b9-4366-869e-2757e0cc4d48/Scripts/Interface.lua")
Add the createHook call at the start of the client_onCreate or server_onCreate functions.
function MyClass.server_onCreate(self) sm.hook.createHook() ... end
function MyClass.client_onCreate(self) sm.hook.createHook() ... end

If you pass self as an argument to the createHook function you will receive a callback once the hook is finished.
For this you need to add the client_onHooked or server_onHooked callback functions to you script.
function MyClass.server_onHooked(self) ... end
function MyClass.client_onHooked(self) ... end
NOTE: For the callback the library will add a callback_onHooked function to your script!
Global Environment
In this section I want to show you explain the utility and independent functions that the library provides.

sm.hook.getGlobalEnvironment Returns: _G (table) - global environment of the game script

sm.hook.isHooked Returns: finished (boolean) - if the hook is finished

sm.hook.isHooking Returns: hooking (boolean) - if the hook is not finished
Game Instance
Game Instance
sm.hook.game.getInstance Returns: instance (table) - the game instance
sm.hook.game.addFunction Parameters: name (string) - the name of the function callback (function) - the function callback
NOTE: If the function exists it will result in an error!

sm.hook.game.addFunctionGraceful Parameters: name (string) - the name of the function callback (function) - the function callback
NOTE: If the function exists it will do nothing!

sm.hook.game.addFunctionOverride Parameters: name (string) - the name of the function callback (function) - the function callback
NOTE: If the function exists it will override it!

sm.hook.game.removeFunction Parameters: name (string) - the name of the function Returns: removed (boolean) - if the function has been removed
sm.hook.game.addHook Parameters: name (string) - the name of the hooked function callback (function) - the function callback Returns: id (number) - the hook id
sm.hook.game.removeHook Parameters: id (number) - the hook id Returns: removed (boolean) - if the hook has been removed
sm.hook.game.addServerCommand Parameters: name (string) - name of the command parameters (table) - parameters of the command callback (function) - the function callback description (string) - the description of the command (optional)
NOTE: The called function will be executed in the game environment on the server side!

sm.hook.game.addClientCommand Parameters: name (string) - name of the command parameters (table) - parameters of the command callback (function) - the function callback description (string) - the description of the command (optional)
NOTE: The called function will be executed in the game environment on the client side!

sm.hook.game.addServerCommandGraceful Parameters: name (string) - name of the command parameters (table) - parameters of the command callback (function) - the function callback description (string) - the description of the command (optional)
NOTE: If the command exists it will do nothing!

sm.hook.game.addClientCommandGraceful Parameters: name (string) - name of the command parameters (table) - parameters of the command callback (function) - the function callback description (string) - the description of the command (optional)
NOTE: If the command exists it will do nothing!

sm.hook.game.removeCommand Parameters: Returns:
NOTE: Not implemented!
World Instance
sm.hook.world.getInstance Returns: instance (table) - the world instance
sm.hook.world.addFunction Parameters: name (string) - the name of the function callback (function) - the function callback
NOTE: If the function exists it will result in an error!

sm.hook.world.addFunctionGraceful Parameters: name (string) - the name of the function callback (function) - the function callback
NOTE: If the function exists it will do nothing!

sm.hook.world.addFunctionOverride Parameters: name (string) - the name of the function callback (function) - the function callback
NOTE: If the function exists it will override it!

sm.hook.world.removeFunction Parameters: name (string) - the name of the function Returns: removed (boolean) - if the function has been removed
sm.hook.world.addHook Parameters: name (string) - the name of the hooked function callback (function) - the function callback Returns: id (number) - the hook id
sm.hook.world.removeHook Parameters: id (number) - the hook id Returns: removed (boolean) - if the hook has been removed
Limitations
As mentioned earlier this library has some limitations.
One limitation is that certain functions like client_onFixedUpdate can't be added using this API because the game does not recognize the callback.
Another limitation is that the barrier between game and part script only allow to trigger more advanced functions of a part using the sm.event.sendToInteractable which can only be executed on the server.
Plans
Here are some plans for the future!
  • Add host only commands
  • Add client specific commands
Example
-- include the library dofile("$CONTENT_d398b515-50b9-4366-869e-2757e0cc4d48/Scripts/Interface.lua") MyClass = class() function MyClass.server_onCreate(self) -- create the hook and pass self to be notified sm.hook.createHook(self) end -- callback for when the hook is complete function MyClass.server_onHooked(self) -- add a function called server_directClear sm.hook.game.addFunction("server_directClear", function(self, params, player) -- calls the clear function of the world sm.event.sendToWorld(player.character:getWorld(), "sv_e_clear") end) -- adds the /clw command sm.hook.game.addServerCommand("/clw", {}, "server_directClear") -- notifies the players in the chat sm.gui.chatMessage("/clw is now available") end
4 Comments
ZIOTO 30 Dec, 2021 @ 10:50am 
ya
wingcomstriker405  [author] 30 Dec, 2021 @ 1:13am 
:D
Vajdani 29 Dec, 2021 @ 6:56pm 
yes
44TNTKITTEN 29 Aug, 2021 @ 7:00am 
no
:greenslime: