Topic: How to add a Playername during conversation

kalimgard on February 10, 2011, 01:05:44 AM

kalimgard

  • Administrator
  • Full Member
  • *****
  • Posts: 170
  • Karma: +3/-0
    • View Profile
The characters have names. And they talk. But can they use the names during conversation? The answer is yes.

To use a character name you have to take three steps:
1) get the player of whom you want the name
2) get his/her name
3) add it to the speech.

As an example I take the simple sentence "Good morning." spoken by the Archer to the Warrior .
scripted this would look like this:
speak("archer","Good morning.","happy")
Now to add a name:
1)local player = getSpeaker("warrior") (since you want the name of the warrior. The Archer will not wish himself good morning in this situation.)
2local name = get(player,"name")
3)speak("archer","Good morning "..name..".","happy")
or, if you want to write it all-in-one
speak("archer","Good morning "..get(getSpeaker("warrior"),"name")..".","happy")

The double dots '..' are to concatenate the textbits. "Good morning".. name .."." will work but lead to the result "Good morningWarriorname".

Unfortunately it may not be that simple. To get the name of the "warrior", a speaker like this has to exist. When initialising a dialog the game adds the players in a set of predefined speakers.
One possibility is by classes ("warrior", "archer","priest","mage"). If no player with a certain class exists, there will be no name and the script will try to concatenate a text with nothing, which is easy for humans and pretty much impossible for Lua script. Meaning the script will fail then and there and everything after (and including) the failed speak() won't be executed. This may be merely annoying, but it can also mean the death of a quest.
To avoid this you would have to make sure the name exists:
if(name ~= nil)then
 speak("archer","Good morning "..name..".", "happy")
else
 speak("archer","Good morning.", "happy")
end;

Another possibility is by prefered classes ("waropt","arcopt","priopt","magopt"). If the class exists, it will speak the sentence, if not, it will be spoken by another class. This means there will always be such a speaker, but you don't know who it is. And it means two speakers can very well be the same person. So if we replace the Archer and the Warrior by Arcopt and Waropt, speak("arcopt","Good morning "..get(getSpeaker("waropt"),"name")..".","happy") may lead to the archer greeting himself. Or, to make the confusion perfect, to the priest greeting the mage. To avoid speaking with yourself you should add a safety
local archer = getSpeaker("arcopt")
local warrior = getSpeaker("waropt")
if(archer ~= warrior)then
 speak("arcopt","Good morning "..name..".", "happy")
end
Logged
  • Read 2790 times
  • Category Scripting
Comments: 1

kalimgard on February 10, 2011, 06:48:48 PM
Reply #1

kalimgard

  • Administrator
  • Full Member
  • *****
  • Posts: 170
  • Karma: +3/-0
    • View Profile
As of now there exists another possibility to add a name. It is a function that retrieves the name and checks whether it exists or not. If the name is nonexistent, it will return a blank, so the script will never fail using it.
Here is what using the function looks like:
speak("archer","Good morning "..getName("warrior")..".","happy")

Of course there can still be trouble: If the name doesn't exist, the sentence can look like this in the worst case: "Good morning, ." If you want to avoid such things, you still have to check the name by using.
local name = getName("warrior")
if(name=="")then
speak("archer","Good morning.","happy")
else
speak("archer","Good morning "..name..".","happy")
end
Logged