Page 1 of 1
Referring to characters in a script?
Posted: Thu Jan 03, 2013 2:26 am
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?
Re: Referring to characters in a script?
Posted: Thu Jan 03, 2013 2:44 pm
by Komag
The big friendly warden does this somewhat, calling you by name depending on your character names and positions
Re: Referring to characters in a script?
Posted: Thu Jan 03, 2013 3:35 pm
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
Re: Referring to characters in a script?
Posted: Fri Jan 04, 2013 4:25 am
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.
Re: Referring to characters in a script?
Posted: Mon Jan 07, 2013 3:30 am
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:
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:
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
Re: Referring to characters in a script?
Posted: Mon Jan 07, 2013 3:44 am
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.