Skip to content
This repository has been archived by the owner on Aug 15, 2021. It is now read-only.

Creating a key binding for the entity

Alessio Malato edited this page Dec 5, 2015 · 2 revisions

We all know that bindings in gmod are pretty annoying, you have to create the command, and then you have to TELL the user to use the console to bind the specific command to a key.

Also, since commands can either be clientside or serverside, makes them pretty annoying to work with prediction in our case.

That's why the entity base comes with a way to assign an arbitrary key on the user's keyboard/mouse/gamepad to trigger a specific action.

The way it works is to trigger a specific IN_ button whenever that user button is pressed ( which is basically what the +moveleft and etc bindings do anyway ), which then you will check in your entity.

In most cases you would want the IN button to be one that nobody has a binding for, for instance, IN_GRENADE1, which would usually require the user to bind +grenade1 to a button.

So let's look at some code:

if SERVER then
	self:SetInButton( 2 ^ 26 )	--after IN_ATTACK3 ( which is 2 ^ 25 )
	self:SetKey( KEY_G )
end

And then when we want to fire the grappling hook:

function ENT:PredictedSetupMove( owner , mv , usercmd )
	if self:IsKeyDown( mv ) then
		if self:GetNextFire() <= CurTime() then
			self:FireHook()
		end
	end
end

This code was taken from the grappling hook entity, and as you can see we're setting both InButton and Key. The reason we're not using the already defined IN_ buttons is simply because we just want to be special.

The user packet will network up to 2 ^ 31, and so as you can see we've got some space for what we can use, since they will be transmitted but not checked for in the slightest.

If you don't like doing it this way you can simply use IN_GRENADE1 or 2.

When the user wants to modify his own button to activate the grappling hook, all he has to do is to use the edit menu.

Of course, the edit menu is only defined in sandbox for now, but with a bit of copypaste it can also work in other gamemodes.

Clone this wiki locally