Jump to content

How do I grab a random value from an array?


Recommended Posts

Posted

Maybe not the best place to ask this, and maybe it's not doable, but if I've created an array full of NPCs, what is the best way to randomly assign one of the values in the array to a ref variable? I'm experimenting with different ways for the Tryout scanner to pick companions for sex, and I can't figure out how to script this.

Posted

I assume you mean: how to pick a random NPC ref from a bunch held in an array?

 

try this:

 

int iKey

int iSize

ref rNPC

 

let iSize := ar_size MyArray

set iKey to rand 0, iSize

let rNPC := MyArray[iKey]

Posted

I gave this a shot - it says the "let" command resulted in an invalid operation. It's not clear which "let" statement it's squawking about, but it has to be one of those two as they are the only ones in the script.

 

But, it looks like this is also going to use more scripting to initialize the array and populate it than the other way I was planning on doing it. If I can get it working, there's a bunch of other applications for this, though, like partnering up people from two different arrays.

Posted

Could be something as simple as a typo. The only possible reasons for an error in those lines are

1. The array isn't initialized

2. It is, but there's nothing under that key

3. There is something under that key but it's not a form/ref

 

Try debugging by printing out the value of iKey once it's been calculated, and dumping the contents of the array with ar_dump.

Posted

Could be something as simple as a typo.

 

Yep. It fucking works! Thanks!

 

It's a little more scripting than the other method I was trying to use (some trickery with formlists and tokens), but it runs faster and is far more reliable!

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...