"How do I ZScript?"

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
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

Re: "How do I ZScript?"

Post by Matt »

Why frandom? They're ints.
(I'm not noticing any performance difference between random and frandom either, whether applied to ints or to doubles)
User avatar
ZZYZX
 
 
Posts: 1384
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

Re: "How do I ZScript?"

Post by ZZYZX »

It's not a question of performance, it's a question of remembering for the rest of your life that if you need a double random from 0 to 1 (or -1 to 1) you should use frandom :D
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49252
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: "How do I ZScript?"

Post by Graf Zahl »

Vaecrius wrote:Why frandom? They're ints.
(I'm not noticing any performance difference between random and frandom either, whether applied to ints or to doubles)

They are doing very different things so it's not a matter or performance at all but a matter of the result you want.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49252
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: "How do I ZScript?"

Post by Graf Zahl »

To bring some info to that weird discussion about function calls - yes, they cost a lot more than reading a local variable.
A local variable can be read with a single VM instruction that translates to approximately 10 native assembly instructions.

To call a native function with two parameters, both of these parameters need to get pushed to the stack (at least 30 native instructions, then the function needs to be looked up and prepared (at least 50(!) native instructions, and then the actual function needs to do its thing. To summarize, a native function call costs at least as much as accessing 10-20 local variables, a scripted function call even more. The precise amount depends on the types of parameters, their amount and of course the return values.

And that's where caching of function results becomes important. Calling GetPlayerInput 8 times in a function that gets called once per tic is harmless. Doing the same in a function which gets called repeatedly by multiple actors in the game each time can quickly become a drag. Tests on my own system show that the VM can approximately do 20000 function calls per millisecond.
User avatar
Major Cooke
Posts: 8218
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: GZBoomer Town

Re: "How do I ZScript?"

Post by Major Cooke »

Eel Vaecrius wrote:Apparently it is a lot faster!
And there you have it folks. Rather than repeating a function over and over, store it in a variable and pull it from there. Especially in ZScript. :mrgreen:
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

Re: "How do I ZScript?"

Post by Matt »

Why can't I get the length of this array?

Code: Select all

	//seeing if you're standing on a liquid texture
	static const String lq[]={
		"MFLR8_4","MFLR8_2",
		"SFLR6_1","SFLR6_4",
		"SFLR7_1","SFLR7_4",
		"FWATER1","FWATER2","FWATER3","FWATER4",
		"BLOOD1","BLOOD2","BLOOD3",
		"SLIME1","SLIME2","SLIME3","SLIME4",
		"SLIME5","SLIME6","SLIME7","SLIME8"
	};
	bool standingonliquid;
	bool CheckLiquidTexture(){
		int lqlength=21; //lq.length();
		for (int i=0; i<lqlength; i++){
			TextureID tx = TexMan.CheckForTexture(lq[i], TexMan.Type_Flat);
			if (tx && floorpic == tx){
				return true;
			}
		}
		return false;
	}
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49252
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: "How do I ZScript?"

Post by Graf Zahl »

Because apparently something is missing in the compiler.
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

Re: "How do I ZScript?"

Post by Matt »

Is there a way to set the attack origin on LineAttack to be the actor's exact position?
User avatar
ZZYZX
 
 
Posts: 1384
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

Re: "How do I ZScript?"

Post by ZZYZX »

Because IIRC for arrays it's Size(), not Length().
User avatar
Ed the Bat
Posts: 3060
Joined: Thu May 03, 2012 1:18 pm
Graphics Processor: nVidia with Vulkan support
Location: Maryland, US

Re: "How do I ZScript?"

Post by Ed the Bat »

Yes, Length() is for how many characters in a string. Size() is for how many items in an array.
User avatar
multiguy18
Posts: 5
Joined: Tue Dec 13, 2016 12:05 pm
Location: Between France, Germany, Italy and Austria

Re: "How do I ZScript?"

Post by multiguy18 »

I'm trying to figure out since two days why the following code (and many other variants i tried) does not work correctly.

Code: Select all

class HordeEventHandler : EventHandler
{
	override void WorldThingSpawned(WorldEvent e)
	{
		if (e.Thing.bIsMonster && !e.Thing.CountInv("X10Clone") && !e.Thing.CountInv("X10Master"))
		{
			e.Thing.GiveInventory("X10Master", 1);
		}
	}
}

class X10Master : CustomInventory
{
	Default
	{
		Inventory.MaxAmount 1;
		+INVENTORY.UNDROPPABLE
		+INVENTORY.UNTOSSABLE
		+INVENTORY.AUTOACTIVATE
	}

	States
	{
	Use:
		TNT1 A 0;
		Fail;
	Pickup:
		TNT1 A 1;
		TNT1 A 1
		{
			A_LogInt(CountInv("X10Clone"));
			if (!CountInv("X10Clone")) //Double Check
			{
				Actor spawner = Spawn("Monsterspawner", pos, NO_REPLACE);
				if (spawner != null) 
				{
					Monsterspawner(spawner).classref = self;
				}
			}
			return true;
		}
		Stop;
	}
}

class X10Clone : CustomInventory
{
	Default
	{
		Inventory.Amount 0;
		Inventory.MaxAmount 1;
		+INVENTORY.UNDROPPABLE
		+INVENTORY.UNTOSSABLE
	}
}

class Monsterspawner : Actor
{
	Default
	{
		+NOINTERACTION
	}
	
	Actor classref;
	
	States
	{
	Spawn:
		TNT1 A 9;
		TNT1 A 1
		{
			Actor clone = Spawn(classref.GetClassName(), pos + (0, 0, 60), NO_REPLACE);
			if (clone != null)
			{
				clone.GiveInventory("X10Clone", 1);
			}
			return true;
		}
		Goto Death;
	Death:
		TNT1 A -1;
		Stop;
	}
}
For some reason, CountInventory always returns 0, which leads to a endless spawning of new monsters. Did i miss something?
Blue Shadow
Posts: 5046
Joined: Sun Nov 14, 2010 12:59 am

Re: "How do I ZScript?"

Post by Blue Shadow »

My only guess is that those CustomInventory items just execute their pickup state when gotten and not stay in the inventory.
User avatar
multiguy18
Posts: 5
Joined: Tue Dec 13, 2016 12:05 pm
Location: Between France, Germany, Italy and Austria

Re: "How do I ZScript?"

Post by multiguy18 »

Thanks Blue Shadow. Replacing CustomInventory with Inventory did the trick.
User avatar
Major Cooke
Posts: 8218
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: GZBoomer Town

Re: "How do I ZScript?"

Post by Major Cooke »

How many ways can a Color variable be accessed besides strings and hexadecimals? Can I get examples of them too, please? I'd like to know if something like:

Code: Select all

Color randomrgb = Color(random(0,255), random(0,255), random(0,255));
is possible. And are there any other parameters?
User avatar
AFADoomer
Posts: 1345
Joined: Tue Jul 15, 2003 4:18 pm

Re: "How do I ZScript?"

Post by AFADoomer »

Unless I'm mis-remembering, straight decimal of the hex value works, too (e.g., 0x004400 is the same as 17408)... A random decimal number between 0 and 16777215 (0xFFFFFF) should give you a random color...

Return to “Scripting”