Trying to access Editor Key in switch statement gives type mismatch

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
TheSartremaster
Posts: 20
Joined: Thu Jun 15, 2023 1:29 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows10 / Ubuntu 22.04.3
Graphics Processor: ATI/AMD (Modern GZDoom)

Trying to access Editor Key in switch statement gives type mismatch

Post by TheSartremaster »

Hi,

I am trying to clean up my assets a bit, by making the same editor object spawn different versions of a sprite.
I want to control the choice of which version to take via the Editor Keys
Here is one version of this:

Code: Select all

class Villager: Actor
{
	Default
	{
		//$Sprite VILFA1
		
		//$Arg0 "Type"
		//$Arg0Type 11
		//$Arg0Enum {0="Version 1"; 1="Version 2"; 2="Version 3";}
	}
	States
	{
	Spawn:
		VILF A 0 {
			switch(args[0]) {
				case 0:
					return ResolveState("Type1");
				break;
				case 1:
					return ResolveState("Type2");
				break;
				case 2:
					return ResolveState("Type3");
				break;
				default:
					return ResolveState("Type1");
				break;
			}
		}
		Stop;
	Type1:
		VILF A -1;
	Type2:
		VILF B -1;
	Type3:
		VILF C -1;
	}
}
This throws the following error:

Code: Select all

Script warning, "WN.pk3:acs/npcs.zs" line 87:
Incorrect number of return values. Got 0, but expected 1
Script error, "WN.pk3:acs/npcs.zs" line 87:
Return type mismatch
(Line 87 being the line "VILF A 0 {" in the code snipped above)

I was following this wiki page: https://zdoom.org/wiki/Switch/Case and examples discussed in this thread: viewtopic.php?t=76337

Does anyone know what is going wrong here?
From my testing (see also below), it seems like the switch statement (or the generic function) cannot deal with returning ResolveState, but I am not sure how to work around that.

Thanks so much in advance!

*edit: changed class inheritance to "actor" to make it more easily reproducible.
Last edited by TheSartremaster on Sat Jun 07, 2025 4:41 am, edited 2 times in total.
TheSartremaster
Posts: 20
Joined: Thu Jun 15, 2023 1:29 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows10 / Ubuntu 22.04.3
Graphics Processor: ATI/AMD (Modern GZDoom)

Re: Trying to access Editor Key in switch statement gives type mismatch

Post by TheSartremaster »

Okay, brief addendum. I also tried the following:

Code: Select all

class Villagers : Actor
{
	Default
	{
		//$Sprite VILFA1
		
		//$Arg0 "Type"
		//$Arg0Type 11
		//$Arg0Enum {0="Version 1"; 1="Version 2"; 2="Version 3";}
	}
	States
	{
	Spawn:
		VILF A 0 {
		switch(args[0]) {
				case 0:
					A_Jump(256, "Type1");
				break;
				case 1:
					A_Jump(256, "Type2");
				break;
				case 2:
					A_Jump(256, "Type3");
				break;				
		}}
	Type1:
		VILF A -1;
	Type2:
		VILF B -1;
	Type3:
		VILF C -1;
	}
}
This compiles, but always just runs through to the Type1 state.

Making A_Jump work here would actually be pretty great, because I would like to implement a "random" case for some assets.
I.e:

Code: Select all

class BloodPuddles : Actor
{
	Default
	{
		//$Sprite BLSPA0
		
		//$Arg0 Gore Type
		//$Arg0Type 11
		//$Arg0Enum {0="Random"; 1="Version 1"; 2="Version 2"; 3="Version 3";}
		
		Radius 8;
		Height 1;
		+FLATSPRITE;
		+NOGRAVITY;
	}
	States
	{
	Spawn:
		BLSP A 0 {
		switch(args[0])
		{
			case 0:
				A_Jump (256, "Type1", "Type2", "Type3");
				break;
			case 1:
				A_Jump(256, "Type1");
				break;
			case 2:
				A_Jump(256, "Type2");
				break;
			case 3:
				A_Jump(256, "Type3");
				break;
			}
		}
	Type1:
		BLSP A -1;
	Type2:
		BLSP B -1;
	Type3:
		BLSP C -1;
	}
}
TheSartremaster
Posts: 20
Joined: Thu Jun 15, 2023 1:29 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows10 / Ubuntu 22.04.3
Graphics Processor: ATI/AMD (Modern GZDoom)

Re: Trying to access Editor Key in switch statement gives type mismatch

Post by TheSartremaster »

Solution:

Okay, I managed to figure it out. Turns out, the issue was neither args[0], nor ReturnState, but my lack of understanding of Anonymous Functions.
The following code works. TL:DR: I moved the return out of the switch statement.

Code: Select all

class BloodPuddles : DecoGore
{
	Default
	{
		//$Sprite BLSPA0
		
		//$Arg0 Gore Type
		//$Arg0Type 11
		//$Arg0Enum {0="Random"; 1="Version 1"; 2="Version 2"; 3="Version 3";}
		
		Radius 8;
		Height 1;
		+FLATSPRITE;
		+NOGRAVITY;
	}
	States
	{
	Spawn:		
		BLSP A 0 NoDelay
		{
			string stateName;
			int choice;
			if(args[0] == 0) {choice = random(1,3);}
			else{choice = args[0];}
			
			switch(choice)
			{
				case 1:
					stateName = "Type1";
					break;
				case 2:
					stateName = "Type2";
					break;
				case 3:
					stateName = "Type3";
					break;
			}
			
			return FindStateByString(stateName);
		}
	Type1:
		BLSP A -1;
	Type2:
		BLSP B -1;
	Type3:
		BLSP C -1;
	}
}
Not sure yet if I am going to stick with this, as I am not happy with the Bloodsplatters being different every time I start the game, but it works for now.
User avatar
inkoalawetrust
Posts: 91
Joined: Mon Aug 26, 2019 9:18 pm
Graphics Processor: nVidia with Vulkan support
Contact:

Re: Trying to access Editor Key in switch statement gives type mismatch

Post by inkoalawetrust »

Your previous code would've worked to, but for anonymous functions you just need it to still end in SOME kind of state return. usually that would just be return State(Null);. Which is no return and makes the actor just go to whatever the next state is.
Post Reply

Return to “Scripting”