Page 1 of 2

Selecting a random character

Posted: Thu Mar 28, 2013 8:40 am
by Grimfan
Okay, I haven't run into this piece of coding yet, so I'd thought I'd ask.

I know how to select all the characters in a party or just a single specific character, but how do I select a single character at random?

I'm assuming it's a fairly simple bit of scripting, but rather than blindly stumble forward I thought I'd ask (I'm also assuming I can use the same sort of code for randomly placing an object on a character's body or inventory).

Thanks in advance as always. ;)

Re: Selecting a random character

Posted: Thu Mar 28, 2013 8:59 am
by mahric
You can get a chamption with getChampion(nr), where nr is the number of the champion you want to get.
If you don't use a number, but use the script below, it returns a random champion

Code: Select all

local c = party:getChampion(math.random(4))
The random function returns a random number from 1 to the maximum you define (4 in this case).
This tiny script does not check if the champion is alive (or even enabled) but I'm not sure that's a problem, or is it?

Re: Selecting a random character

Posted: Thu Mar 28, 2013 9:05 am
by Grimfan
I actually want to disable a random party member, which is what this script can do, correct (well not the actual disable part just setting up a random party member to be disabled)?

So can I do this with it?

Code: Select all

local c = party:getChampion(math.random(4)):setEnabled(false)
Oh, and thank you very much for the quick response Mahric. :D It reminds me that I should finish your latest mod (I've sort of neglected playing LoG a bit with the release of a few other games and real life)

Re: Selecting a random character

Posted: Thu Mar 28, 2013 9:09 am
by JohnWordsworth
It's a bit more work to support Toorum mode, where only 1 character is enabled, but I guess you could do the following;

Code: Select all

function getRandomChampion()
  local validIndices = {}

  for i = 1,4 do
    if ( party:getChampion(i):getEnabled() and party:getChampion(i):isAlive() ) then
      table.insert(validIndices, i);
    end
  end

  if ( validIndices > 0 ) then
    return validIndices[math.random(1, #validIndices)];
  end

  return nil;
end
Untested, so might have syntax mistakes, but this should work even with disabled characters / in toorum mode. The above also ensures the selected character is alive. Remove 'and party:getChampion(i):isAlive()' above to remove this constraint.

You could use the above to do the following...

Code: Select all

local champion = getRandomChampion();

if ( champion ~= nil ) then champion:setEnabled(false); end

Re: Selecting a random character

Posted: Thu Mar 28, 2013 9:23 am
by Grimfan
Thanks for the additional help John. :)

I'm going to try a modified script with your ideas and see how it works.

Re: Selecting a random character

Posted: Thu Mar 28, 2013 12:18 pm
by Xanathar
Shameless plug :mrgreen:

if you put grimq in your project, it is:

Code: Select all

local randomChar = grimq.fromAliveChampions():random()

Re: Selecting a random character

Posted: Thu Mar 28, 2013 12:44 pm
by Grimfan
Isn't grimq included with JKos's framework? I've got that so I should be able to use your method.

Re: Selecting a random character

Posted: Thu Mar 28, 2013 1:34 pm
by Xanathar
Yes, it is :)

Re: Selecting a random character

Posted: Fri Mar 29, 2013 1:02 am
by Grimfan
Okay, I used the above script from Xanathar and added setEnabled(false) to the end like this:

Code: Select all

local randomChar = grimq.fromAliveChampions():random():setEnabled(false)
Now when all the characters are alive this script works and disables a random character, however it won't work if there are any dead party members in the group.

How do I skip over dead party members to disable only living characters? I still want to keep the script relatively tight and use your framework Xanathar.

Re: Selecting a random character

Posted: Fri Mar 29, 2013 1:16 am
by Xanathar
I tried and tried and grimq.fromAliveChampions():random():setEnabled(false) works for me - it skips disabled and dead characters and disables one of the other chars (or gives error if noone remains to be chosen).
Can you post here (or PM) the whole script ?