Something random inside a scene?

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
User avatar
Hipshot
Posts: 59
Joined: Wed Feb 03, 2016 1:45 pm
Location: Sweden

Something random inside a scene?

Post by Hipshot »

Is it possible to have something like "TNT1 A 0 A_StartSound("alert", CHAN_VOICE);" happen by random in a scene. I understand that I can just do a jump randomly to another scene, but that requires me to do another scene and jump back, if I keep doing that I end up with tons of small scenes everywhere.
Blue Shadow
Posts: 5043
Joined: Sun Nov 14, 2010 12:59 am

Re: Something random inside a scene?

Post by Blue Shadow »

Yes.

Code: Select all

TNT1 A 0
{
	if (random() < 32)
	{
		A_StartSound("alert", CHAN_VOICE);
	}
}
User avatar
Hipshot
Posts: 59
Joined: Wed Feb 03, 2016 1:45 pm
Location: Sweden

Re: Something random inside a scene?

Post by Hipshot »

Nice, is the number 32, 32 out of 255 here where 255 is 100%/1.0?
Blue Shadow
Posts: 5043
Joined: Sun Nov 14, 2010 12:59 am

Re: Something random inside a scene?

Post by Blue Shadow »

Yes. random(), without passing any args to it, returns a value that ranges from 0 to 255.
User avatar
Hipshot
Posts: 59
Joined: Wed Feb 03, 2016 1:45 pm
Location: Sweden

Re: Something random inside a scene?

Post by Hipshot »

Ok, I see.

I noted that this can't be "outside" a spritename (not sure what it's called really), but it has to be inside like in your example TNT1?

So this wont work, throws me an error:

TNT1 A 0 A_StartSound("alert", CHAN_VOICE); GUAA A 3 A_FaceTarget; { if (random() < 32) { GUAA F 4 Bright; } } GUAA B 3 ThrustThing(angle*256/360+192, 3, 0, 0); GUAA ABCD 4 A_Wander();

Because it assumes that the entire section there is suppose to be inside the a_facetarget sprite group?
Blue Shadow
Posts: 5043
Joined: Sun Nov 14, 2010 12:59 am

Re: Something random inside a scene?

Post by Blue Shadow »

No, it can't. The block of code in-between the curly braces (the outer ones, that is) is basically a function, and you can't have more than one function attached to a single state. So what you do is execute A_FaceTarget from within the anonymous function, like this:

Code: Select all

GUAA A 3
{
	A_FaceTarget();

	if (random() < 32)
	{
		// ...
	}
}
User avatar
Hipshot
Posts: 59
Joined: Wed Feb 03, 2016 1:45 pm
Location: Sweden

Re: Something random inside a scene?

Post by Hipshot »

Ok, good to know. Had been even goodlier otherwise =)
Post Reply

Return to “Scripting”