Toggable object spawn

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!)
Post Reply
User avatar
nekostuffing
Posts: 60
Joined: Sat Aug 31, 2019 8:08 pm
Graphics Processor: ATI/AMD with Vulkan/Metal Support

Toggable object spawn

Post by nekostuffing »

Ive seen many mods that have options to toggle certain things to spawn or not, like smoke, bullet casings and other actors, but i haven't figured out how to do this, could someone help me understand how that's done, preferably using acs with decorate.
User avatar
R4L
Global Moderator
Posts: 426
Joined: Fri Mar 03, 2017 9:53 am
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 11 Pro
Graphics Processor: ATI/AMD with Vulkan/Metal Support
Contact:

Re: Toggable object spawn

Post by R4L »

You can do this with CVars and CVARINFO: https://zdoom.org/wiki/CVARINFO

For an example, say I have a Pistol that spawns a smoke actor named BerettaSmokeSpawn in the fire state. You can define a CVar in a CVARINFO lump like so:

Code: Select all

user bool mymod_disablesmoke = false; //Turn on smoke effects by default
Then in the Fire state, say I had this for code:

Code: Select all

Fire:
	BRTT E 1 A_FireProjectile("BerettaSmokeSpawn",0,0,6,8) //Spawn smoke when firing
	BRTT F 1 A_FireBullets(4,5,-1,6,"BulletPuff",1)
	BRTT GHIJKLGO 1 A_WeaponReady
	Goto Ready
Just add your CVar check before the first line:

Code: Select all

Fire:
	TNT1 A 0 A_JumpIf(GetCVar("mymod_disablesmoke"), 2) //Don't spawn smoke
	BRTT E 1 A_FireProjectile("BerettaSmokeSpawn",0,0,6,8)
	BRTT F 1 A_FireBullets(4,5,-1,6,"BulletPuff",1)
	BRTT GHIJKLGO 1 A_WeaponReady
	Goto Ready
A_JumpIf just evaluates if the CVar is true or false (in this case, false) and jumps/skips over A_FireProjectile to A_FireBullets. For more examples of A_Jump, see here: https://zdoom.org/wiki/A_Jump
User avatar
nekostuffing
Posts: 60
Joined: Sat Aug 31, 2019 8:08 pm
Graphics Processor: ATI/AMD with Vulkan/Metal Support

Re: Toggable object spawn

Post by nekostuffing »

R4L wrote: Mon Oct 03, 2022 12:38 pm You can do this with CVars and CVARINFO
This is exactly what i was looking for, thank you!
User avatar
Player701
 
 
Posts: 1710
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support
Contact:

Re: Toggable object spawn

Post by Player701 »

NB: CVARs used for controlling object spawns should be marked server, otherwise they can and will break multiplayer games! Even if the object in question is just a decoration, its own behavior (such as calls to random(...) may cause a loss of network consistency due to the RNGs on each client going out of sync with each other. And even if this is avoided, by default any actor being spawned will trigger an RNG that is marked important to network sync, which will also eventually break the game if the actor is not spawned for some of the clients. There is actually a way to avoid triggering it, but I'd strongly advise against it because it looks somewhat like a hack and may eventually change at any time (and still doesn't guraantee stable behavior unless your actor is marked +NOINTERACTION, probably not even then).
User avatar
nekostuffing
Posts: 60
Joined: Sat Aug 31, 2019 8:08 pm
Graphics Processor: ATI/AMD with Vulkan/Metal Support

Re: Toggable object spawn

Post by nekostuffing »

Player701 wrote: Tue Oct 04, 2022 3:34 am NB: CVARs used for controlling object spawns should be marked server
I was using user bool but it wasn't even working in single player, changed it to server bool and now the option for turning the effect on and off works, will using server bool cause problems with multiplayer? im not sure if server int is what you meant.
User avatar
Player701
 
 
Posts: 1710
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support
Contact:

Re: Toggable object spawn

Post by Player701 »

You can use any type (bool or int) depending on the application. For a simple toggle, bool is sufficient. It's the scope (server vs user) that matters. Server CVARs are always synchronized between players, and only the settings controller (i.e. the player who's hosting the game) can change them.
User avatar
nekostuffing
Posts: 60
Joined: Sat Aug 31, 2019 8:08 pm
Graphics Processor: ATI/AMD with Vulkan/Metal Support

Re: Toggable object spawn

Post by nekostuffing »

Alright thanks, appreciate it.
User avatar
R4L
Global Moderator
Posts: 426
Joined: Fri Mar 03, 2017 9:53 am
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 11 Pro
Graphics Processor: ATI/AMD with Vulkan/Metal Support
Contact:

Re: Toggable object spawn

Post by R4L »

Heh, my bad for suggesting a user CVar. I didn't really think about multiplayer when I suggested it.
Post Reply

Return to “Scripting”