Hooks in S&Box

I assume that the hook system from GMod is being completely dumped and replaced, since it got really messy and caused a bunch of compatibility problems between addons - but how will we achieve the same possibilities in S&Box?

If we’re playing by the C# rulebook, I imagine async void events would be used for a pub/sub style event system, where your addon could subscribe to particular events like player input or the render tick to draw stuff etc, but what about the old hooks where you could return a value to influence behaviour?

Some examples of this previously would be to modify the view state (CalcView) of the player (custom third-person view or head bobbing effect for example) or to prevent player physgunning objects (PhysgunPickup) under certain conditions (maybe an admin mod). How would these work in S&Box?

5 Likes

Gleaming from previous code examples that garry posted it seems like most “hooks” will be moved out to abstract/virtual methods in the base Game/Entity/PlayerController/Etc. classes.

Not sure how that will translate to an addon wanting to override a specific action and/or simply subscribe to it, maybe @garry can shed some light on that.

EDIT:

Though judging by the rather generous use of annotations to reduce boilerplate in the aforementioned examples, hooks will probably be handled the same way. I imagine something like that:

[GameHook(method = Game.OnMapLoad)]
public static void MyOnDeath() { ... }

[PlayerHook(method = Player.OnDeath)]
public static void MyOnDeath(Player player) { ... }
2 Likes

I was also curious about this. C# does have a built-in event listening system, however I’m not a big fan of it (over-complicated imo), nor is it used very widely in the industry.

You can achieve something similar to a hook by calling the base method before/after you add your own code, however this can’t account for multiple developers using method without having to worry about other developers overriding their functionality.

1 Like

We haven’t done anything about this yet. I don’t even know how necessary it will be.

9 Likes

I’m fine with anything as long as I don’t have to do this monstrosity just to override hooks:

local mEnt = FindMetaTable("Entity")

mEnt.NewSetModel = mEnt.NewSetModel || mEnt.SetModel

function mEnt:SetModel(model)

	self.NewSetModel(self, model)

	if(self:IsPlayer()) then

		hook.Run("SPM_ModelChange", self)

	end

end
3 Likes

I’m going to assume addons will have their own class extending some kind of BaseAddon and virtual methods would be overridden or implemented if some addon interface was used.

1 Like

Clearly, they have to find a better way than that, it’s so terrible. On Garry’s Mod, it’s almost impossible to ask developers to add hooks, because there are so many other requests waiting, or it is considered a non-priority (even though it’s essential), sometimes you have to do some shit that work partially, it’s a shame.

1 Like

Please no. If you wanted any form of custom hooks, that just won’t be scalable.

2 Likes

Please no hooks in S&Box. Hooks are a stupid concept.

Garry doesnt like C# events either, for some reason…

3 Likes

This message was paid for by delegate gang.

2 Likes