Changing thing spawns based on player class?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!
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
scalliano
Posts: 2871
Joined: Tue Jun 21, 2005 1:16 pm
Location: Ireland

Changing thing spawns based on player class?

Post by scalliano »

I know this is possible, but as a complete ZScript (and ACS for that matter) novice I'm struggling to get my head around how to implement it myself. Ever since the migration to ZScript, the wiki is somewhat light on examples I can learn from (unless I'm looking in the wrong place, which is entirely possible).

Basically, I'm looking to rebuild Shuffle from the ground up so that it resembles something that isn't a complete mess. This involves incorporating all of the separate player classes into the main .pk3. Currently, each player .pk3 has their own set of spawners which override the main one to prevent any weapons/ammo/items they can't use from spawning. What I'm looking to do is set up a script that performs this override on map load based on the class the player is using.

I did try this a few years back - I thought of giving each class a unique dummy inventory item that identifies the class, and while what I tried at the time almost worked for items and weapons, it still spawned everything else (albeit invisibly) and I never got around to figuring out how to implement it for monsters before I gave up.

I would really like to try this again, but I fear a bit of hand-holding may be in order. Any help would be appreciated, as always.
User avatar
stainedofmind
Posts: 250
Joined: Sun Sep 01, 2019 10:59 am

Re: Changing thing spawns based on player class?

Post by stainedofmind »

Threw together a quick example pk3 for you that hopefully does what you want. I didn't feel like fake coding classes, so I used Final Doomer+ (viewtopic.php?t=55061) as a vehicle to execute the code.

I made a couple of wonky Medikits that use Megasphere and Soulsphere graphics instead, and if you select Plutonia Guy or TNT Guy as classes, they'll spawn instead of normal medikits.

Hopefully this helps! Please do note, this is just one way to go about doing the replacements, so I'm sure there may be better ways, and I'm not the absolute best coder, but this should be a place to start. Lemme know if there's anything more you need to know!

Also I'd like to say a big thank you, as your shuffle mod was my first "go-to" doom mod back in the day when I started to get into dooming, so I'm looking forward to a modern version!

Edit: Removed download due to bug. Reposted below.
Last edited by stainedofmind on Wed Nov 19, 2025 4:53 pm, edited 1 time in total.
User avatar
scalliano
Posts: 2871
Joined: Tue Jun 21, 2005 1:16 pm
Location: Ireland

Re: Changing thing spawns based on player class?

Post by scalliano »

This looks like precisely what I need, and a lot easier to decipher than some of the code from other mods I've looked at. Much appreciated!

Glad you've enjoyed Shuffle in the past - suffice to say that a full rebuild is a lot of work given the sheer amount of content to migrate over, so the new version will be a ways off still, but hopefully I'll have more to show soon enough!
User avatar
salahmander2
Posts: 140
Joined: Wed Nov 19, 2014 7:20 pm

Re: Changing thing spawns based on player class?

Post by salahmander2 »

stainedofmind wrote: Fri Jul 04, 2025 4:18 pm Threw together a quick example pk3 for you that hopefully does what you want. I didn't feel like fake coding classes, so I used Final Doomer+ (viewtopic.php?t=55061) as a vehicle to execute the code.

I made a couple of wonky Medikits that use Megasphere and Soulsphere graphics instead, and if you select Plutonia Guy or TNT Guy as classes, they'll spawn instead of normal medikits.

Hopefully this helps! Please do note, this is just one way to go about doing the replacements, so I'm sure there may be better ways, and I'm not the absolute best coder, but this should be a place to start. Lemme know if there's anything more you need to know!

Also I'd like to say a big thank you, as your shuffle mod was my first "go-to" doom mod back in the day when I started to get into dooming, so I'm looking forward to a modern version!
I tried your code, while it does work, it crashes some maps. Such as MAP06 on Doom II.
User avatar
stainedofmind
Posts: 250
Joined: Sun Sep 01, 2019 10:59 am

Re: Changing thing spawns based on player class?

Post by stainedofmind »

salahmander2 wrote: Tue Nov 18, 2025 4:47 pm
stainedofmind wrote: Fri Jul 04, 2025 4:18 pm Threw together a quick example pk3 for you that hopefully does what you want. I didn't feel like fake coding classes, so I used Final Doomer+ (viewtopic.php?t=55061) as a vehicle to execute the code.

I made a couple of wonky Medikits that use Megasphere and Soulsphere graphics instead, and if you select Plutonia Guy or TNT Guy as classes, they'll spawn instead of normal medikits.

Hopefully this helps! Please do note, this is just one way to go about doing the replacements, so I'm sure there may be better ways, and I'm not the absolute best coder, but this should be a place to start. Lemme know if there's anything more you need to know!

Also I'd like to say a big thank you, as your shuffle mod was my first "go-to" doom mod back in the day when I started to get into dooming, so I'm looking forward to a modern version!
I tried your code, while it does work, it crashes some maps. Such as MAP06 on Doom II.
That's odd, since all it's doing is a standard actor replacement, unless it's spawning already replaced actors, causing an infinite loop. How are using the code exactly?
User avatar
salahmander2
Posts: 140
Joined: Wed Nov 19, 2014 7:20 pm

Re: Changing thing spawns based on player class?

Post by salahmander2 »

stainedofmind wrote: Tue Nov 18, 2025 8:02 pm
salahmander2 wrote: Tue Nov 18, 2025 4:47 pm
stainedofmind wrote: Fri Jul 04, 2025 4:18 pm Threw together a quick example pk3 for you that hopefully does what you want. I didn't feel like fake coding classes, so I used Final Doomer+ (viewtopic.php?t=55061) as a vehicle to execute the code.

I made a couple of wonky Medikits that use Megasphere and Soulsphere graphics instead, and if you select Plutonia Guy or TNT Guy as classes, they'll spawn instead of normal medikits.

Hopefully this helps! Please do note, this is just one way to go about doing the replacements, so I'm sure there may be better ways, and I'm not the absolute best coder, but this should be a place to start. Lemme know if there's anything more you need to know!

Also I'd like to say a big thank you, as your shuffle mod was my first "go-to" doom mod back in the day when I started to get into dooming, so I'm looking forward to a modern version!
I tried your code, while it does work, it crashes some maps. Such as MAP06 on Doom II.
That's odd, since all it's doing is a standard actor replacement, unless it's spawning already replaced actors, causing an infinite loop. How are using the code exactly?
Exactly how it is in the PK3 with FinalDoomer+. The error thrown up is trying to read from address zero at line 8.
User avatar
stainedofmind
Posts: 250
Joined: Sun Sep 01, 2019 10:59 am

Re: Changing thing spawns based on player class?

Post by stainedofmind »

Welp, that certainly is interesting... Seems on some maps, the playerpawn just doesn't exist at the time the replacements are being done. Checked all of Doom 2, and it also happens on Map32. Happened on a different map when I checked Freedoom Phase 2, Map13 I think. I have absolutely no idea what would be causing that.
User avatar
stainedofmind
Posts: 250
Joined: Sun Sep 01, 2019 10:59 am

Re: Changing thing spawns based on player class?

Post by stainedofmind »

Okay, I think I fixed it. Added fixed download. Has to use 'players[consoleplayer].Cls.GetClassName()' instead of reading the class right from the PlayerPawn, cause again, sometimes it doesn't exist yet. Not sure if there'd be any issues using this method, as the Wiki states it's used internally, but it SEEMS to be functioning.
scalliano wrote: Sat Jul 05, 2025 4:29 pm This looks like precisely what I need, and a lot easier to decipher than some of the code from other mods I've looked at. Much appreciated!

Glad you've enjoyed Shuffle in the past - suffice to say that a full rebuild is a lot of work given the sheer amount of content to migrate over, so the new version will be a ways off still, but hopefully I'll have more to show soon enough!
Hopefully you're not too deep into recoding, but I would take a look at the new version and replace your code with the changes I noted above.

Edit: Removed code example. Tis bad. Better examples down in other posts.
Last edited by stainedofmind on Thu Nov 20, 2025 7:12 pm, edited 1 time in total.
User avatar
salahmander2
Posts: 140
Joined: Wed Nov 19, 2014 7:20 pm

Re: Changing thing spawns based on player class?

Post by salahmander2 »

stainedofmind wrote: Wed Nov 19, 2025 4:57 pm Okay, I think I fixed it. Added fixed download. Has to use 'players[consoleplayer].Cls.GetClassName()' instead of reading the class right from the PlayerPawn, cause again, sometimes it doesn't exist yet. Not sure if there'd be any issues using this method, as the Wiki states it's used internally, but it SEEMS to be functioning.
scalliano wrote: Sat Jul 05, 2025 4:29 pm This looks like precisely what I need, and a lot easier to decipher than some of the code from other mods I've looked at. Much appreciated!

Glad you've enjoyed Shuffle in the past - suffice to say that a full rebuild is a lot of work given the sheer amount of content to migrate over, so the new version will be a ways off still, but hopefully I'll have more to show soon enough!
Hopefully you're not too deep into recoding, but I would take a look at the new version and replace your code with the changes I noted above.
Still crashing on MAP32 surprisingly. Been looking around on Google, and it mentioned something about a null checker.
User avatar
stainedofmind
Posts: 250
Joined: Sun Sep 01, 2019 10:59 am

Re: Changing thing spawns based on player class?

Post by stainedofmind »

salahmander2 wrote: Wed Nov 19, 2025 5:24 pm
stainedofmind wrote: Wed Nov 19, 2025 4:57 pm Okay, I think I fixed it. Added fixed download. Has to use 'players[consoleplayer].Cls.GetClassName()' instead of reading the class right from the PlayerPawn, cause again, sometimes it doesn't exist yet. Not sure if there'd be any issues using this method, as the Wiki states it's used internally, but it SEEMS to be functioning.
scalliano wrote: Sat Jul 05, 2025 4:29 pm This looks like precisely what I need, and a lot easier to decipher than some of the code from other mods I've looked at. Much appreciated!

Glad you've enjoyed Shuffle in the past - suffice to say that a full rebuild is a lot of work given the sheer amount of content to migrate over, so the new version will be a ways off still, but hopefully I'll have more to show soon enough!
Hopefully you're not too deep into recoding, but I would take a look at the new version and replace your code with the changes I noted above.
Still crashing on MAP32 surprisingly. Been looking around on Google, and it mentioned something about a null checker.
Works for me on Map32, and I ran it though Freedoom 2 again, and no crash. No sure whats going on with your setup. Regardless, the code provided isn't really meant to run for a full playthrough or anything, as it is just a prototype/example/proof-of-concept. Null checks are always good, but in this case, just bailing out on the replacement will cause the items to not be replaced, so a different method would have to be devised for detecting the player class. My best idea would be to record the player class to a persistent variable, then use that going forward instead. Not sure how this would work if the player class somehow changed during the run though.
User avatar
fakemai
Posts: 411
Joined: Mon Feb 12, 2018 12:26 am
Location: Australia

Re: Changing thing spawns based on player class?

Post by fakemai »

First, consoleplayer should not be used to affect the playsim, you need to check the global players array. What about defining your items as normal but have the actual replacement be a custom spawner that uses PostBeginPlay to check the players in-game and which classes exist? Then it can spit out items appropriately in a circle, or if only one player class exists, just on the spawner. You may find the RandomSpawner class useful to look at, and you could even use DropItem for storing the item lists so they're only defined once in memory.

Admittedly multiplayer is a trouble if the classes have different pick-ups unlike something like DRLA, having six different medikits appearing in a cluster would look dumb. You may have to make compromises if you want the mod to work in multiplayer like making the health items generic and just having the weapons in a cluster, with appropriate class restrictions set.
User avatar
salahmander2
Posts: 140
Joined: Wed Nov 19, 2014 7:20 pm

Re: Changing thing spawns based on player class?

Post by salahmander2 »

fakemai wrote: Thu Nov 20, 2025 3:53 am First, consoleplayer should not be used to affect the playsim, you need to check the global players array. What about defining your items as normal but have the actual replacement be a custom spawner that uses PostBeginPlay to check the players in-game and which classes exist? Then it can spit out items appropriately in a circle, or if only one player class exists, just on the spawner. You may find the RandomSpawner class useful to look at, and you could even use DropItem for storing the item lists so they're only defined once in memory.

Admittedly multiplayer is a trouble if the classes have different pick-ups unlike something like DRLA, having six different medikits appearing in a cluster would look dumb. You may have to make compromises if you want the mod to work in multiplayer like making the health items generic and just having the weapons in a cluster, with appropriate class restrictions set.
That does sound like it's a lot easier. so basically you're saying it can pick from a pool from stuff in the item list, making it easier? Is there any examples of it BTW? Since I'm helping out with Super Shuffle, I can use the example as a base and hopefully build it from there.
User avatar
fakemai
Posts: 411
Joined: Mon Feb 12, 2018 12:26 am
Location: Australia

Re: Changing thing spawns based on player class?

Post by fakemai »

Rough mock-up to show what I'm getting at that you can try in Hexen where Ettins turn into the player's third weapons, but it does miss some important details that again, check RandomSpawner to see what it does for spawning. The biggest ones are specials since Hexen and UDMF can attach actions to picking up items, and monster death actions if you do anything with monsters.

Code: Select all

class ClassGiver : Actor abstract {
	static const string classlist[] = {
		"FighterPlayer",
		"ClericPlayer",
		"MagePlayer"
	};

	override void PostBeginPlay() {
		DropItem itm = self.GetDropItems();
		for(int i = 0 ; i < classlist.Size() ; i++ ) {
			for(int i2=0 ; i2 < players.Size() ; i2++) {
				if(PlayerInGame[i2] && players[i2].mo is classlist[i]) {
					Actor m = Spawn(itm.Name, pos);
					m.vel.z = 10;
				}
			}
			itm = itm.Next;
		}
		Destroy();
	}
}

class TestEttin : ClassGiver replaces Ettin {
	Default {
		DropItem "MWeapLightning";
		DropItem "CWeapFlame";
		DropItem "FWeapHammer";
	}


}
Also you may want to check Wrath of Cronos, which has to spawn different weapons for classes, but that's ACS/DECORATE I think. Still useful if you were intending to target that for compatibility.

EDIT: And yes, GetDropItems does resolve in reverse so you may prefer to put the class names list in reverse instead.
User avatar
stainedofmind
Posts: 250
Joined: Sun Sep 01, 2019 10:59 am

Re: Changing thing spawns based on player class?

Post by stainedofmind »

fakemai wrote: Thu Nov 20, 2025 3:53 am First, consoleplayer should not be used to affect the playsim, you need to check the global players array.
Out of curiosity, why is using consoleplayer an issue? Multiplayer I'm assuming. That being the case, how are you supposed to determine the current client player number? In defense of the code I posted, it is just an example, as I keep saying, and I had assumed classes in Shuffle weren't meant to be used in a multiplayer capacity.
User avatar
fakemai
Posts: 411
Joined: Mon Feb 12, 2018 12:26 am
Location: Australia

Re: Changing thing spawns based on player class?

Post by fakemai »

The important part. If you were to use consoleplayer in my example what'll happen is each player's copy of the game will spawn different items (if different classes at least) and it WILL desync. Even if you don't care about multiplayer there's no sense in doing it that way, it's potential to break as other bad scope interactions have in later versions of GZDoom. It is fine to use consoleplayer in some circumstances, like reading from play to render to the HUD.

Also my example's just as rough and has bugs but it's not my intention to do all the work, only to point to a solution to the problem, and one that at least has the possibility of multiplayer if that's desirable.
Post Reply

Return to “Scripting”