[ZScript] Readonly errors

Archive of the old editing forum
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.
Locked
Talon1024
 
 
Posts: 376
Joined: Mon Jun 27, 2016 7:26 pm
Preferred Pronouns: He/Him
Graphics Processor: nVidia with Vulkan support
Contact:

[ZScript] Readonly errors

Post by Talon1024 »

I'm having a bit of trouble with ZScript, specifically readonly errors.

This is a decoupled shader control system I wrote for WolfenDoom: Blade of Agony, and some examples. It enables shaders when the player has 2 or more of a shader control item, and calls the SetUniforms method to set the shader uniforms.

When the player is on a map where he is given these shader control items, the shader gets enabled and controlled by the shader controller, as expected. However, if I start a new map, or I take away all of my shader control items, the shader "freezes" in place when it should be disabled altogether. This is what I've come up with so far, but it's giving me readonly errors on lines 53 and 54. I don't know what to do.

Code: Select all

/*
	Shader controller base class

	This allows mappers to control shaders using the GiveInventory/TakeInventory
	ACS functions.

	Do not use this directly! Instead, make an actor class that inherits from
	ShaderControl. Set the ShaderControl.Shader property to the name of the
	shader to control. The shader must be defined in GLDEFS.

	To enable a shader, give 2 of the ShaderControl subclass item to the player.
	For example:

	GiveInventory("ShakeShaderControl", 2);
	
	To disable a shader, take 1 of the shader control item away so that the
	player only has 1 of said item. For example:

	TakeInventory("ShakeShaderControl", 1);
	or
	SetInventory("ShakeShaderControl", 1);

	Note that the latter requires you to define the SetInventory function in
	your ACS code.
*/
class ShaderControl : Inventory {
	string ShaderToControl;
	bool known;
	property Shader: ShaderToControl;
	Default {
		Inventory.MaxAmount 0x7fffffff;
	}
	
	override void BeginPlay() {
		known = false;
	}

	virtual ui void SetUniforms(PlayerInfo p, RenderEvent e) {}
}

class CustomShaderHandler : StaticEventHandler
{
	Array<String> shaders;

	override void RenderOverlay(RenderEvent e)
	{
		PlayerInfo p = players[consoleplayer];
		ShaderControl shaderControl = ShaderControl(p.mo.FindInventory("ShaderControl", true));

		if (shaderControl != null)
		{
			if (!shaderControl.known) {
				shaders.Push(shaderControl.ShaderToControl);
				shaderControl.known = true;
			}
			//Console.Printf("Shader: %s", shaderControl.ShaderToControl);
			if (shaderControl.amount >= 2)
			{
				Shader.SetUniform1f(p, shaderControl.ShaderToControl, "timer", gametic + e.FracTic);
				shaderControl.SetUniforms(p, e);
				Shader.SetEnabled(p, shaderControl.ShaderToControl, true);
			}
			else
			{
				Shader.SetEnabled(p, shaderControl.ShaderToControl, false);
			}
		} else {
			for (int i = 0; i < shaders.Size(); i++) {
				Shader.SetEnabled(p, shaders[i], false);
			}
		}
	}
}
dpJudas
 
 
Posts: 3163
Joined: Sat May 28, 2016 1:01 pm

Re: [ZScript] Readonly errors

Post by dpJudas »

At the moment the shader state persists across map changes and even if you create a completely new game. I'm not sure what is the best behavior to have here.
User avatar
Rachael
Posts: 13922
Joined: Tue Jan 13, 2004 1:31 pm
Preferred Pronouns: She/Her
Contact:

Re: [ZScript] Readonly errors

Post by Rachael »

From my experience, it's always best to have all game state components completely reset to their default values when a new game is created, but not on a standard level change.

G_ChangeLevel() is the actual in-game level change command (you probably won't find this useful), and G_InitNew() is the new game command.
Locked

Return to “Editing (Archive)”