Player Class Based Weapon Replacement

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!)
Post Reply
User avatar
Airehnr66
Posts: 8
Joined: Sun Nov 27, 2022 6:04 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): macOS 13.5 Ventura
Graphics Processor: Apple M1
Contact:

Player Class Based Weapon Replacement

Post by Airehnr66 »

Hello everyone,

So I've searched the forums and wiki but can't quite seem to find what I'm looking for. I'm thinking of trying my hand in some gameplay mods that give you a new character to play through with your favorite map packs, but I'm not sure what the best approach would be. I'm wanting to replace particular base weapons with custom made weapons when the custom player class is selected at the start of the game, but if another class that doesn't modify them (like the default Doom Marine) is selected those weapons are unchanged. In the past I've successfully coded simple custom weapons that replace base weapons in the map, as well as a quick new player class that can have a custom weapon load out. Now I'm hoping to combine what I know so far to make new character (player class) mods that make this happen.

My question is... What would be the best route to take as far as coding/archiving goes? All my previous experiments were using DECORATE so that would be my first instinct, but I wasn't sure if it was possible to have the choice of player class determine whether a weapon was swapped out in a map or not using DECORATE alone. Would I need to involve some sort of ACS? Is this only possible in ZScript? Perhaps a combination of all three? And will this have to be made a .pk3 or will .WAD suffice? I hope I've provided enough detail as to what I'm trying to achieve but I can do my best to answer more if need be. As you can tell I'm only just getting started in modding of any sort, so any help is appreciated!
Jarewill
 
 
Posts: 1768
Joined: Sun Jul 21, 2019 8:54 am

Re: Player Class Based Weapon Replacement

Post by Jarewill »

If you don't want to replace and redefine all the weapons (which would be required to make custom spawners) you can use ZScript event handlers, specifically CheckReplacement, like this:

Code: Select all

Class ClassSpecificHandler : EventHandler{
	Override void CheckReplacement(ReplaceEvent e){
		If(players[0].mo is "CustomPlayerClass"){
			If(e.Replacee is "Shotgun"){
				e.Replacement = "CustomShotgun";
			}Else If(e.Replacee is "Chaingun"){
				e.Replacement = "CustomChaingun";
			}//So on with all other weapons
		}
	}
}
Then you will need to add this into your MAPINFO:

Code: Select all

GameInfo{
	AddEventHandlers = "ClassSpecificHandler"
}
However this method will break in multiplayer as it will only check the class of the first player who entered.
User avatar
Airehnr66
Posts: 8
Joined: Sun Nov 27, 2022 6:04 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): macOS 13.5 Ventura
Graphics Processor: Apple M1
Contact:

Re: Player Class Based Weapon Replacement

Post by Airehnr66 »

Thank you for this. I had to take some time to learn ZScript and the particular aspects of your code but its now making more sense. I've managed to implement it in my WAD and it's doing exactly what I need! While I'm only focused on getting my character mods to work in single player I do intend to have multiplayer compatibility at some point. I don't think that will be too difficult once I get there.

I'm curious though, if there are other ways to get the same or similar results. One of my early iterations of getting this done was using custom actors that inherited from RandomSpawners that placed my custom weapons in map using DropItem. Worked like a charm until I needed to figure out a way to check the player class and replace weapons based on the result. I'm open to keeping this discussion going if there are other methods that could be shared. It may be useful to other projects as well!
User avatar
Virathas
Posts: 249
Joined: Thu Aug 10, 2017 9:38 am

Re: Player Class Based Weapon Replacement

Post by Virathas »

RandomSpawners are indeed a valid option for that - I use them myself in my Player/Monster replacer mod. The exact spawners used there use the version from this topic. It is compatible with both single player and cooperative gameplay.

But it does require what Jarewill mentioned - redefining all original weapons, to add class requirements to them.
User avatar
Airehnr66
Posts: 8
Joined: Sun Nov 27, 2022 6:04 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): macOS 13.5 Ventura
Graphics Processor: Apple M1
Contact:

Re: Player Class Based Weapon Replacement

Post by Airehnr66 »

Very interesting. I never figured out the class check when using RandomSpawners, but I used DropItem in a similar way.
Post Reply

Return to “Scripting”