How do I bind keys to custom (named) weapon states?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
User avatar
SpancherZD
Posts: 6
Joined: Sun Oct 22, 2017 11:49 pm
Location: Behind you

How do I bind keys to custom (named) weapon states?

Post by SpancherZD »

Hi. How does one bind keys to activate a custom weapon state that has a name? By that I mean a weapon state that isn't called User#.
Say, for example, I wanted a state called "Reload", and to activate it I'd press R. How would I do this? I've only been able to activate them by calling them User1, User2, etc and having this in my KEYCONF lump;

Code: Select all

	AddMenuKey "Reload" reload
	Alias reload +user1
	DefaultBind R reload
(Yes, I know that "Reload" is already a state that can be used with WRF_ALLOWRELOAD, I was simply using this as an example)
It's obviously possible as nearly every gun mod I've looked at has used custom weapon states with actual names.
Probably a noob question... After all, I am a noob lol (I only started modding a few days ago).
ZippeyKeys12
Posts: 111
Joined: Wed Jun 15, 2016 2:49 pm

Re: How do I bind keys to custom (named) weapon states?

Post by ZippeyKeys12 »

For user states it's WRF_ALLOWUSER# and # is 1 thru 4.
If you mean any generic state then I would personally have a keyconf for example "+customthingy".Then I'd setup up an EventHandler which I'd setup an InputProcess and compare e.KeyScan to KeyBindings.GetKeysForCommand(+customthingy) and set a variable in the player. Finally inside the weapon check the variable in the player and jump to state if it is true.

Sry if it doesn't make sense, writing this while walking in between classes.
User avatar
SpancherZD
Posts: 6
Joined: Sun Oct 22, 2017 11:49 pm
Location: Behind you

Re: How do I bind keys to custom (named) weapon states?

Post by SpancherZD »

Ah, thanks Zippey, I thought I tried that second thing originally but I must've done something wrong, it works now.

EDIT: No idea what I changed, but suddenly it doesn't work ;(
ZippeyKeys12
Posts: 111
Joined: Wed Jun 15, 2016 2:49 pm

Re: How do I bind keys to custom (named) weapon states?

Post by ZippeyKeys12 »

If your project isn't too secretive, you could post some of the relevant code to see what's wrong.
User avatar
SpancherZD
Posts: 6
Joined: Sun Oct 22, 2017 11:49 pm
Location: Behind you

Re: How do I bind keys to custom (named) weapon states?

Post by SpancherZD »

Alright, basically what I want is for a "weapon" animation/sound to play on-screen when I press F.
In the weapon decorate marker thingy I want it to play on, I've made a custom weapon state called User1 that contains the animation + sound.

Code: Select all

User1:
	TNT1 A 10
	OKAY A 1
	OKAY B 1 A_PlaySound("YEAH", 2)
	OKAY CD 1 A_AlertMonsters
	OKAY E 15
	OKAY DCBA 1
	TNT1 A 10
	Goto Ready
And, in KEYCONF, I've binded F to User1.

Code: Select all

AddKeySection "Custom Mod Controls" CustomModControls

	AddMenuKey "Signal OKAY" Okay
	Alias Okay +User1
	DefaultBind F Okay
It works, however I have two problems;
-When the animation completes, it replays the animation infinitely until I switch weapon. Switching back to the weapon will continue playing that animation, the only way to stop it is to die.
-I don't want the state to be called User1, I want it to be called "Okay".

How would I fix/accomplish these? I've read heaps of stuff about it and can't seem to figure out why it's doing that/how to call the state something else and have it still work.
User avatar
ramon.dexter
Posts: 1562
Joined: Tue Oct 20, 2015 12:50 pm
Graphics Processor: nVidia with Vulkan support
Location: Kozolupy, Bohemia

Re: How do I bind keys to custom (named) weapon states?

Post by ramon.dexter »

Sorry to ask, but is it really big problem, that it will be "user1", instead of "okay"? Just, think: What would happen, when you dont have this weapon in inventory? What would happen, when you have the weapon in inventory, but not selected? I think it would cause a crash, beacuse with the key bound to a specififc state of specific weapon. Or nothing would ahppen at all. gzDoom has the weapon states, because, basically, it is a weapon state of the one weapon. The user states are added so user can have four other actions, besides the fire, altfire and reaload. So you have four actions, that you can bind to any key. And that's it.

Also, when you want some special action for each of the weapon you have, you have to manually specify the action for each weapon individually. That's just how gzDoom works - many things have to done manually, instead of automatics like in modern engines.
User avatar
SpancherZD
Posts: 6
Joined: Sun Oct 22, 2017 11:49 pm
Location: Behind you

Re: How do I bind keys to custom (named) weapon states?

Post by SpancherZD »

Yeah haha, I can't use User1-4 because I'm having more than 4 custom states, and I can't use AltFire or Reload as they're already being used as well. Like I said, it's definitely possible as I've seen it in heaps of mods, I mean look at Brutal Doom; every weapon has way more than 4 custom states, all with different names, and they even have bindings in its KEYCONF.

Also, it hasn't seemed to crash yet, and I've tried the state in every weapon I've made so far.

... Speaking of Brutal Doom, if it helps, the "okay" state I want is similar to the "offend" state in BD, except the frames/sprites are different obviously.
Blue Shadow
Posts: 5032
Joined: Sun Nov 14, 2010 12:59 am

Re: How do I bind keys to custom (named) weapon states?

Post by Blue Shadow »

I'd imagine it's done by using inventory: when you press a key, an item is given to you, for which the weapon checks, jumping to the desired state.

Or something to that effect...
User avatar
ramon.dexter
Posts: 1562
Joined: Tue Oct 20, 2015 12:50 pm
Graphics Processor: nVidia with Vulkan support
Location: Kozolupy, Bohemia

Re: How do I bind keys to custom (named) weapon states?

Post by ramon.dexter »

Hey, that's no a bad idea at all...

This way you can bind scripts, that will give you these items. Weapon will have just A_JumpIfInventory checks, that will do the rest.
ZippeyKeys12
Posts: 111
Joined: Wed Jun 15, 2016 2:49 pm

Re: How do I bind keys to custom (named) weapon states?

Post by ZippeyKeys12 »

Blue Shadow wrote:I'd imagine it's done by using inventory: when you press a key, an item is given to you, for which the weapon checks, jumping to the desired state.
That's what I was recommending except using inputprocess and a player variable instead for cleanliness.

EDIT: I'll try and write an example tonight if you don't figure it out by then
ZippeyKeys12
Posts: 111
Joined: Wed Jun 15, 2016 2:49 pm

Re: How do I bind keys to custom (named) weapon states?

Post by ZippeyKeys12 »

Something like this, haven't tested it but it has got the basic idea.

Code: Select all

version "2.5"
class SampleInput:EventHandler {
	KeyBindings myBindings;
	override void OnRegister(){
		SetOrder(666);
	}
	override void NetworkProcess(ConsoleEvent e){
		if(e.Name=="oksig"){
			let playah = CustomPlayer(players[e.Player].mo);
			playah.okaysig=true;
		}
	}
}

class CustomPlayer:DoomPlayer{
	bool okaysig;
	Default{
		Player.WeaponSlot 1, "CustomWeap";
	}
}

class CustomWeap:Chainsaw{
	Default{
		Weapon.SlotNumber 1;
	}
	States{
		Ready:
			TNT1 A 1{
				if(CustomPlayer(invoker).okaysig)
					return ResolveState("UniqueState");
				return ResolveState(null);
			}
			//EDIT:Insert Weapon functionality
		UniqueState:
			TNT1 A 0{
				console.printf("state");
				return ResolveState("Ready");
			}
	}
}

Code: Select all

AddKeySection "Custom Mod Controls" CustomModControls
AddMenuKey "Signal OKAY" Okay
Alias Okay "netevent oksig"
DefaultBind F Okay

Code: Select all

gameinfo{
	PlayerClasses = "CustomPlayer"
	AddEventHandlers="SampleInput"
}
Last edited by ZippeyKeys12 on Wed Nov 01, 2017 10:08 pm, edited 1 time in total.
User avatar
SpancherZD
Posts: 6
Joined: Sun Oct 22, 2017 11:49 pm
Location: Behind you

Re: How do I bind keys to custom (named) weapon states?

Post by SpancherZD »

It worked, thanks everyone :D
ZippeyKeys12
Posts: 111
Joined: Wed Jun 15, 2016 2:49 pm

Re: How do I bind keys to custom (named) weapon states?

Post by ZippeyKeys12 »

Glad you worked it out! :D

Return to “Scripting”