Selecting a random character

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Grimfan
Posts: 369
Joined: Wed Jan 02, 2013 7:48 am

Selecting a random character

Post 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. ;)
User avatar
mahric
Posts: 192
Joined: Sun Nov 04, 2012 3:05 pm

Re: Selecting a random character

Post 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?
Did you visit the Wine Merchant's Basement? And heard about the Awakening of Taarnab?
Grimfan
Posts: 369
Joined: Wed Jan 02, 2013 7:48 am

Re: Selecting a random character

Post 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)
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: Selecting a random character

Post 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
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
Grimfan
Posts: 369
Joined: Wed Jan 02, 2013 7:48 am

Re: Selecting a random character

Post by Grimfan »

Thanks for the additional help John. :)

I'm going to try a modified script with your ideas and see how it works.
User avatar
Xanathar
Posts: 629
Joined: Sun Apr 15, 2012 10:19 am
Location: Torino, Italy
Contact:

Re: Selecting a random character

Post by Xanathar »

Shameless plug :mrgreen:

if you put grimq in your project, it is:

Code: Select all

local randomChar = grimq.fromAliveChampions():random()
Waking Violet (Steam, PS4, PSVita, Switch) : http://www.wakingviolet.com

The Sunset Gate [MOD]: viewtopic.php?f=14&t=5563

My preciousss: http://www.moonsharp.org
Grimfan
Posts: 369
Joined: Wed Jan 02, 2013 7:48 am

Re: Selecting a random character

Post by Grimfan »

Isn't grimq included with JKos's framework? I've got that so I should be able to use your method.
User avatar
Xanathar
Posts: 629
Joined: Sun Apr 15, 2012 10:19 am
Location: Torino, Italy
Contact:

Re: Selecting a random character

Post by Xanathar »

Yes, it is :)
Waking Violet (Steam, PS4, PSVita, Switch) : http://www.wakingviolet.com

The Sunset Gate [MOD]: viewtopic.php?f=14&t=5563

My preciousss: http://www.moonsharp.org
Grimfan
Posts: 369
Joined: Wed Jan 02, 2013 7:48 am

Re: Selecting a random character

Post 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.
User avatar
Xanathar
Posts: 629
Joined: Sun Apr 15, 2012 10:19 am
Location: Torino, Italy
Contact:

Re: Selecting a random character

Post 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 ?
Waking Violet (Steam, PS4, PSVita, Switch) : http://www.wakingviolet.com

The Sunset Gate [MOD]: viewtopic.php?f=14&t=5563

My preciousss: http://www.moonsharp.org
Post Reply