Referring to characters in a script?

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
Duncan_B
Posts: 31
Joined: Wed Dec 19, 2012 8:43 pm
Location: Santa Cruz, Calif.-- USA

Referring to characters in a script?

Post by Duncan_B »

Something simple I'd like to be able to do, but that I haven't figured out with the Dungeon Editor yet, is how I could single out a character in the player's party at the beginning of the game so that I can I refer to that particular character (rather than a character slot) in later scripts. I'd also like to do something similar to reference a character uses a particular item. Any help?
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Referring to characters in a script?

Post by Komag »

The big friendly warden does this somewhat, calling you by name depending on your character names and positions
Finished Dungeons - complete mods to play
User avatar
Grimwold
Posts: 511
Joined: Thu Sep 13, 2012 11:45 pm
Location: A Dungeon somewhere in the UK

Re: Referring to characters in a script?

Post by Grimwold »

The character ordinal value is what you need... when the party is generated each character is given an ordinal value from 1 to 4 (different to the slot value)... this value never changes, even if you move the characters around.

so you could randomly generate an ordinal value at the start of the game and whenever you need to refer to that champion, use the following script to find the champion entity based on that ordinal.

Code: Select all

function findChampionByOrdinal(ord)
  for ch = 1,4 do
    if party:getChampion(ch):getOrdinal() == ord then
      return party:getChampion(ch)
    end
  end
end

regarding your item use question you can use party:getChampion(slot):getOrdinal() to find the ordinal value of the champion in a particular slot at the time... or if it's part of an onUse() item hook you can do

Code: Select all

onUse() = function(item,champion)
  local item_user_ordinal = champion:getOrdinal()

DO OTHER STUFF
end
User avatar
Duncan_B
Posts: 31
Joined: Wed Dec 19, 2012 8:43 pm
Location: Santa Cruz, Calif.-- USA

Re: Referring to characters in a script?

Post by Duncan_B »

Thanks, guys! I'll get these worked into my game when I get a minute and report back if I have any trouble.
User avatar
Duncan_B
Posts: 31
Joined: Wed Dec 19, 2012 8:43 pm
Location: Santa Cruz, Calif.-- USA

Re: Referring to characters in a script?

Post by Duncan_B »

I had a go at this today. I'm having difficulty referring to a character who has used an item in a later script. How is this done with a local variable?

A friend advised I try defining that character's ordinal position as a global variable called "servOrd", but this doesn't seem to work. When I attempt to reference "servOrd" in the second script, nothing happens. If I try to get it to hudPrint an ordinal referring to servOrd, I end up with an error message.

onUseItem hook:
SpoilerShow

Code: Select all

onUseItem = function(self, champion)
	party:shakeCamera(0.6, 2)
	playSound("game_over")
	champion:addSkillPoints(5)
	servOrd = champion:getOrdinal()
	hudPrint(servOrd.." is servOrd")
	return true
end,
Function attempting to reference the same ordinal character later:
SpoilerShow

Code: Select all

function nauseate()
--hudPrint(servOrd.." is servOrd") <-- attempt to concatenate global 'servOrd' (a nil value)
	for ch = 1,4 do
	    	if party:getChampion(ch):getOrdinal() == 1 then
		     party:getChampion(ch):setCondition("diseased", 90001)
		end
		if party:getChampion(ch):getOrdinal() == servOrd then
			party:getChampion(ch):setCondition("diseased", 90001)
		end
	end
end
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: Referring to characters in a script?

Post by Komag »

You can't save stuff in the external lua files (I think). If you want to define a variable to use later, put the info in a script in the dungeon. If you need it to happen from a hook, then just reference that script to do a function that sets the variable:

in your items.lua file:

Code: Select all

onUseItem = function(self, champion)
   party:shakeCamera(0.6, 2)
   playSound("game_over")
   champion:addSkillPoints(5)
   myItemScript.setServOrd(champion)
   hudPrint(servOrd.." is servOrd")
   return true
end,
in a script entity in the dungeon called myItemScript:

Code: Select all

servOrd = 0
function setServOrd(champion)
   servOrd = champion:getOrdinal()
end
in your other script and function:

Code: Select all

function nauseate()
--hudPrint(myItemScript.servOrd.." is servOrd") <-- attempt to concatenate global 'servOrd' (a nil value)
   for ch = 1,4 do
          if party:getChampion(ch):getOrdinal() == 1 then
           party:getChampion(ch):setCondition("diseased", 90001)
      end
      if party:getChampion(ch):getOrdinal() == servOrd then
         party:getChampion(ch):setCondition("diseased", 90001)
      end
   end
end
You could put the function right near your nauseate function, and then would not need the script name first (so it would look like you originally had it), but you would then need to change the hook reference to the correct script name.
Finished Dungeons - complete mods to play
Post Reply