If Champ alive if Champ dead

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
User avatar
Eleven Warrior
Posts: 752
Joined: Thu Apr 18, 2013 2:32 pm
Location: Australia

If Champ alive if Champ dead

Post by Eleven Warrior »

Hi all I need two different Scripts please. I want to be able to see if a Champ is Dead or Alive eg...

1 - Champs 1-4 check if they are dead or alive, then something happens

Script 1

if champ 1 is alive then
hudPrint("I am here")
elseif champ 1 is dead then

-- Another random champs says Boris is dead -- The random must be alive lol --

hudPrint('Boris is dead.")
end

Script 2

1 - Champs 1-4 check if they are dead or alive, then something happens.

if champ 1 is alive then
hudPrint("I can see the writing.")
elseif champ 1 is dead then
hudPrint('")
end

I need these scripts badly so any help would be most appreciated thxs :)
User avatar
Frenchie
Posts: 219
Joined: Wed Oct 16, 2013 2:50 am

Re: If Champ alive if Champ dead

Post by Frenchie »

I think this will suffice for script 1:

Code: Select all

for i=1,4 do local ch = party.party:getChampion( i ) 
if ch:isAlive() do hudprint( ch:getName().." says he is here" ) 
else hudprint( ch:getName()..' is dead" ) end end end
I think the random option is possible but it would complicate things. To get a random number use this:

Code: Select all

math.random(1,4)
Edit : I was still trying to improve my script till minmay came to the rescue (I already tested it that %5 didn't work)

I thought I wanted to put the names of all the living and dead into 2-dimentional strings (living[1] to living[4] and dead[1] to dead[4]). Then use counters li and de to see how many of each I had. This is how far I got :

Code: Select all

living = { } dead = { } li = 0 de = 0
for i=1,4 do local ch = party.party:getChampion( i )
if ch:isAlive living[ i ] = ch:getName() li = li +1
else dead[ i ] = getName() de = de +1
end end
j = math.random ( 1, li )
- - if all dead this won't work
for i=1,li do local ch = party.party:getChampion( j )
hudprint ...
Last edited by Frenchie on Thu Feb 05, 2015 9:35 am, edited 8 times in total.
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: If Champ alive if Champ dead

Post by minmay »

that won't work: 5 % 5 is equal to 0, not 1
also, you presumably didn't mean to make x a permanent field
also, you have a typo in there that will prevent it from running at all

here's a quick solution:
script 1:

Code: Select all

do
  local ch = party.party:getChampion(1)
  if ch:isAlive() then
    hudPrint(ch:getName()..": I am here.")
  else
    local alive = {}
    for i = 2,4 do if party.party:getChampion(i):isAlive() then table.insert(alive,i) end end -- skip champion 1, we already know they're dead
    hudPrint(party.party:getChampion(alive[math.random(1,#alive)])..": "..ch:getName().." is dead.")
  end
end
script 2:

Code: Select all

do
  local ch = party.party:getChampion(1)
  if ch:isAlive() then
    hudPrint(ch:getName()..": I can see the writing.")
  else
    hudPrint("")
  end
end
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
Eleven Warrior
Posts: 752
Joined: Thu Apr 18, 2013 2:32 pm
Location: Australia

Re: If Champ alive if Champ dead

Post by Eleven Warrior »

Thank you very much guys most appreciated :)

EDIT: In fact this script(s) are awesome :)
User avatar
Frenchie
Posts: 219
Joined: Wed Oct 16, 2013 2:50 am

Re: If Champ alive if Champ dead

Post by Frenchie »

A long long time ago (25 yrs or so) when I learned diagrams (psd and pss) to make a program work flows with arrows, I would first solve any problems before actually writing the code. Then you would see which approach would work and which not.

For example if you picked a random number between 1 and 4, then checked if that champion was alive it would be the speaker reporting the status of all other champions including himself. If he was dead what then? Do another random or go to the next champion. With another random the loop might take a long time. With the next champion you would first need to make sure that after the 4th champion you need to start again at the first and then check if he's alive. But you also must stop after 4 checks.

You probably will make a few flows on paper or in your head that don't work after testing and then think about which variables to use. Then after coding experience will let you optimize it. Maybe someone should pick up the Lua Scripting Lesson again and optimize it for LoG2.
User avatar
msyblade
Posts: 792
Joined: Fri Oct 12, 2012 4:40 am
Location: New Mexico, USA
Contact:

Re: If Champ alive if Champ dead

Post by msyblade »

minmay, you impress me more and more each and every day. Just. . .wow man.
Currently conspiring with many modders on the "Legends of the Northern Realms"project.

"You have been captured by a psychopathic diety who needs a new plaything to torture."
Hotel Hades
Post Reply