[Solved] Replace starting weapon without using playerclass

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
Jekyll Grim Payne
 
 
Posts: 1076
Joined: Mon Jul 21, 2008 4:08 am
Preferred Pronouns: He/Him
Graphics Processor: nVidia (Modern GZDoom)
Contact:

[Solved] Replace starting weapon without using playerclass

Post by Jekyll Grim Payne »

Is there an effective way to do that? I tried overriding readyweapon from WorldTick but it produced pretty weird results.
Last edited by Jekyll Grim Payne on Thu Apr 18, 2019 2:03 pm, edited 1 time in total.
User avatar
Void Weaver
Posts: 724
Joined: Thu Dec 18, 2014 7:15 am
Contact:

Re: Replace starting weapon without using custom playerclass

Post by Void Weaver »

What about script which will execute function sequence like as TakeActorInventory(tid,"weapon_to_replace") => GiveActorInventory(tid,"custom_weapon") => SetWeapon("custom_weapon")?
User avatar
Jekyll Grim Payne
 
 
Posts: 1076
Joined: Mon Jul 21, 2008 4:08 am
Preferred Pronouns: He/Him
Graphics Processor: nVidia (Modern GZDoom)
Contact:

Re: Replace starting weapon without using custom playerclass

Post by Jekyll Grim Payne »

Void Weaver wrote:What about script which will execute function sequence like as TakeActorInventory(tid,"weapon_to_replace") => GiveActorInventory(tid,"custom_weapon") => SetWeapon("custom_weapon")?
Sorry, I'm not looking for ACS methods. Also, I can take and give weapons from a zscript event handler, of course, but it's not the same as replacing the weapon — the player will still start the level with default Pistol.
User avatar
Matt
Posts: 9696
Joined: Sun Jan 04, 2004 5:37 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Debian Bullseye
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Contact:

Re: Replace starting weapon without using custom playerclass

Post by Matt »

I think this is the sort of thing event handlers are for, so I don't see any way to do this without them...
User avatar
Jekyll Grim Payne
 
 
Posts: 1076
Joined: Mon Jul 21, 2008 4:08 am
Preferred Pronouns: He/Him
Graphics Processor: nVidia (Modern GZDoom)
Contact:

Re: Replace starting weapon without using custom playerclass

Post by Jekyll Grim Payne »

Found it by looking through A_SetWeapon code: check for readyweapon and set pendingweapon to the required one.
User avatar
Apeirogon
Posts: 1605
Joined: Mon Jun 12, 2017 12:57 am

Re: [Solved] Replace starting weapon without using playercla

Post by Apeirogon »

Pro tip : post you solution here so that in distant future our descendants would not necrobump this topic with words "same problem" or similar :wink:
User avatar
Jekyll Grim Payne
 
 
Posts: 1076
Joined: Mon Jul 21, 2008 4:08 am
Preferred Pronouns: He/Him
Graphics Processor: nVidia (Modern GZDoom)
Contact:

Re: [Solved] Replace starting weapon without using playercla

Post by Jekyll Grim Payne »

Apeirogon wrote:Pro tip : post you solution here so that in distant future our descendants would not necrobump this topic with words "same problem" or similar :wink:
Well, I did describe it above, but here's the code I use in Beautiful Doom, initial version suggested by Zombie:

Code: Select all

class WeaponOverrideHandler : EventHandler
{
override void WorldTick() {		
	for (int pn = 0; pn < MAXPLAYERS; pn++) {
		if (!playerInGame[pn])
			continue;

		PlayerInfo player	= players[pn];
		PlayerPawn mo		= player.mo;
		
		let oldweap = player.pendingWeapon.GetClassName(); //get the weapon that is being selected
		let oldweapr = player.readyweapon.GetClassName(); //get the currently selected weapons
		if (bdoom_replaceweapons) { //check if the replace weapons CVAR is true
			switch (oldweap) { //if game attempts to SELECT a vanilla weapon, take it away and select a modded weapon (and give it, if it's missing)
				case 'Fist' 			: player.pendingWeapon = GetWeapon(mo, "BD_Fist"); 			mo.TakeInventory(oldweap,1); break;
				case 'Chainsaw' 		: player.pendingWeapon = GetWeapon(mo, "BD_Chainsaw"); 		mo.TakeInventory(oldweap,1); break;
				case 'Pistol' 			: player.pendingWeapon = GetWeapon(mo, "BD_Pistol"); 		mo.TakeInventory(oldweap,1); break;
				case 'Shotgun'			: player.pendingWeapon = GetWeapon(mo, "BD_Shotgun"); 		mo.TakeInventory(oldweap,1); break;
				case 'SuperShotgun' 	: player.pendingWeapon = GetWeapon(mo, "BD_SuperShotgun"); 	mo.TakeInventory(oldweap,1); break;
				case 'Chaingun' 		: player.pendingWeapon = GetWeapon(mo, "BD_Chaingun"); 		mo.TakeInventory(oldweap,1); break;
				case 'RocketLauncher' 	: player.pendingWeapon = GetWeapon(mo, "BD_RocketLauncher");mo.TakeInventory(oldweap,1); break;
				case 'PlasmaRifle' 		: player.pendingWeapon = GetWeapon(mo, "BD_PlasmaRifle"); 	mo.TakeInventory(oldweap,1); break;
				case 'BFG9000' 			: player.pendingWeapon = GetWeapon(mo, "BD_BFG9000"); 		mo.TakeInventory(oldweap,1); break;
				}
			switch (oldweapr) { //if a vanilla weapon is READY (such as Pistol at vanilla map start), we also take it away and select the modded counterpart
				case 'Fist' 			: player.pendingWeapon = GetWeapon(mo, "BD_Fist"); 			mo.TakeInventory(oldweapr,1); break;
				case 'Chainsaw' 		: player.pendingWeapon = GetWeapon(mo, "BD_Chainsaw"); 		mo.TakeInventory(oldweapr,1); break;
				case 'Pistol' 			: player.pendingWeapon = GetWeapon(mo, "BD_Pistol"); 		mo.TakeInventory(oldweapr,1); break;
				case 'Shotgun'			: player.pendingWeapon = GetWeapon(mo, "BD_Shotgun"); 		mo.TakeInventory(oldweapr,1); break;
				case 'SuperShotgun' 	: player.pendingWeapon = GetWeapon(mo, "BD_SuperShotgun"); 	mo.TakeInventory(oldweapr,1); break;
				case 'Chaingun' 		: player.pendingWeapon = GetWeapon(mo, "BD_Chaingun"); 		mo.TakeInventory(oldweapr,1); break;
				case 'RocketLauncher' 	: player.pendingWeapon = GetWeapon(mo, "BD_RocketLauncher");mo.TakeInventory(oldweapr,1); break;
				case 'PlasmaRifle' 		: player.pendingWeapon = GetWeapon(mo, "BD_PlasmaRifle"); 	mo.TakeInventory(oldweapr,1); break;
				case 'BFG9000' 			: player.pendingWeapon = GetWeapon(mo, "BD_BFG9000"); 		mo.TakeInventory(oldweapr,1); break;
				}
			}
		}
	}
private Weapon GetWeapon(PlayerPawn mo, Class<Weapon> type)
	{
		let weap = Weapon(mo.FindInventory(type));
		return weap ? weap : Weapon(mo.GiveInventoryType(type));
	}
}
Post Reply

Return to “Scripting”