RandomPick

Moderator: GZDoom Developers

User avatar
Major Cooke
Posts: 8170
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

RandomPick

Post by Major Cooke »

A long time ago, I suggested something like this for ZDoom. I've finally programmed it.
Clamps down and returns one of the two numbers entered, and nothing else. To get more results, pick inside of pick can happen:

Code: Select all

randompick(1,2,9,12)
...to get a result of 1, 2, 9, or 12. Lots of possibilities with this, that's for sure.

Here's the pull request.
Last edited by Major Cooke on Sat Dec 27, 2014 2:54 pm, edited 3 times in total.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49056
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: RClamp

Post by Graf Zahl »

I think a better implementation would be to allow an infinite amount of parameters and then randomly select one instead of nesting these calls.
Gez
 
 
Posts: 17833
Joined: Fri Jul 06, 2007 3:22 pm

Re: RClamp (Random Clamp)

Post by Gez »

The name is misleading, that's not a clamp. (Thread title had me scratching my head, what's a random clamp and how could it be useful?)

It's something that randomly picks one of the numbers given, right? Then why not call it pick?

And what Graf said. pick(x, ...)
User avatar
Major Cooke
Posts: 8170
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: RClamp (Random Clamp)

Post by Major Cooke »

Name change: Done. Now Pick.

And I have no idea how I'm going to allow unlimited parameters, truthfully. Mind giving me a hand?
User avatar
Nightfall
Posts: 555
Joined: Thu Aug 06, 2009 4:00 am
Location: Finland

Re: Pick

Post by Nightfall »

After scanning the initial '(', read in variables until ')' is encountered. Store these in a TArray<FxExpression*> and pass that to FxPick which then processes this further with the casting.

I think 'choose' would make a better name.
User avatar
Major Cooke
Posts: 8170
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: Pick

Post by Major Cooke »

The question then comes to mind, what if we have something like pick(1,2,(user_somenumber*82))? Wouldn't that throw it off?
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49056
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Pick

Post by Graf Zahl »

Not if parsed properly. The ParseExpression functions parse an entire expression and store it as an FxExpression object. This object can be anything, ranging from a constant to some complex constructs that requires several levels of recursion to be evaluated.
User avatar
Major Cooke
Posts: 8170
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: Pick

Post by Major Cooke »

I'm not sure what to do about resolving with the array though. Any ideas?

Here's my code so far. Again, this is... very alien territory for me so it shouldn't be a surprise if I completely screwed the code up entirely. :?

Currently, it doesn't work. Access violation occurs in the Resolve call.
Spoiler:
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49056
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Pick

Post by Graf Zahl »

You try to get the size of your array by calling min.Max() when you should call min.Size(). Max() returns the amount of reserved memory, not the amount of elements in use.
User avatar
Major Cooke
Posts: 8170
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: Pick

Post by Major Cooke »

That did the trick! Thanks Graf! :D

The pull request has been updated.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49056
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Pick

Post by Graf Zahl »

Added, but I had to plug a memory leak. You forgot to delete the array's contents.
User avatar
Major Cooke
Posts: 8170
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: Pick

Post by Major Cooke »

Aw hell. Sorry. Yeah, I still have a lot to learn.

Thanks for that though!
D2JK
Posts: 543
Joined: Sat Aug 30, 2014 8:21 am

Re: Pick

Post by D2JK »

Perhaps a dumb question, but is this supposed to work in decorate? For me, using it in an action function makes ZDoom shut down in the startup, without any error message.
User avatar
Major Cooke
Posts: 8170
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: Pick

Post by Major Cooke »

Copy and paste your code, and I'll take a look at it. I only really designed this for DECORATE usage though. Whether it works in ACS or not was something I didn't have in mind for this to begin with. This is mainly because I don't want to make Graf clean up after my abysmal mess again. :oops:

If the latter, this is code I used to test it.

Code: Select all

Actor MM
{
	var int user_t;
	+NOINTERACTION
	States
	{
	Spawn:
		TNT1 A 0 NoDelay A_SetUserVar("user_t",pick(1,4,12,16))
		TNT1 A 0 A_JumpIf(user_t == 1,"s1")
		TNT1 A 0 A_JumpIf(user_t == 4,"s2")
		TNT1 A 0 A_JumpIf(user_t == 12,"s3")
		TNT1 A 0 A_JumpIf(user_t == 16,"s4")
	Err:
		TNT1 A 35 A_PrintBold("Apparently it didnt work.")
		Goto Spawn
	s1:
		TNT1 A 35 A_PrintBold("One")
		Goto Spawn
	s2:
		TNT1 A 35 A_PrintBold("Four")
		Goto Spawn
	s3:
		TNT1 A 35 A_PrintBold("Twelve")
		Goto Spawn
	s4:
		TNT1 A 35 A_PrintBold("Sixteen")
		Goto Spawn
	}
}
D2JK
Posts: 543
Joined: Sat Aug 30, 2014 8:21 am

Re: Pick

Post by D2JK »

Decorate.

I used the latest (Dec 16) x64 build in here.

Actually there is an error message, the window just closed so fast I couldn't see it first: "Call to an unknown function Pick".

For example, this code can cause the error:
Spoiler:
Post Reply

Return to “Closed Feature Suggestions [GZDoom]”