Page 1 of 1

NPC: Multiple Lines of Dialogue (Was: Declaring Variables)

Posted: Fri Oct 19, 2012 3:48 pm
by pinnguaq
EDIT: What started as a conversation about multiple lines of dialogue through Variable decleration has turned into a useful seminar on how to create an NPC who carries on a conversation with each "attack" on him. Full Credit to Grimwold. Working script/instructions are as follows:

Create an NPC in your monsters.lua file
SpoilerShow
cloneObject{
name = "npcGoromorg",
baseObject = "goromorg",
onAttack = function(self, dir)
return false
end,
onMove = function(self, dir)
return false
end,
onDamage = function(self,dir)
conversation_script.haveConversation(self,dir)
return false

end,
}
Create a script entity called "converation.script" Put the following script inside:
SpoilerShow
i = 1
function haveConversation()
local dialogue = {"line 1","line 2","line 3","line 4","line 5"}
hudPrint(dialogue)
if i == 5 then
i = 0
end
i = i +1
end




Original Post Below:


Hello,

I just need some help with declaring variables for all you Lua experts (or even beginners, as this strikes me as something that shouldn't be too difficult)...

I'm creating an NPC based on the coding/suggestions from the Superscript Repository, however not one that is used as a vender, just someone with whom the player can have a conversation to carry on the story.

Currently the script is written so that the player "attacks" the NPC and the dialogue is spoken. I have six sentences I want the NPC to speak, so I'd like to display them one at a time. My idea is that each time the player "Attacks" the NPC, a new line of dialogue is spoken.

To do this I want to create a simple integer varibale called "i" that is equal to 1. With each attack, i = i + 1, until attack six in which case i = 1 again.

I am having no problem with anything except for actually declaring i = 1. Here is my script in my Monster.lua file. In this script I declare the variable "locally" in the cloneObject section, attaching it to the monster. However it returns an error when I load it up:

SpoilerShow
cloneObject{
name = "npcGoromorg",
local i = 1,
baseObject = "goromorg",
onAttack = function(self, dir)
return false
end,
onMove = function(self, dir)
return false
end,
onDamage = function(self, dir)
if i == 1 then
hudPrint("Hello traveler!")
i = i + 1
return false
elseif i ==2 then
hudPrint("Saying Number 2")
i = i + 1
return false
elseif i ==3 then
hudPrint("Saying number 3")
i = i + 1
return false
end
end,
}


Note: I'm at work typing this from memory, it may not be exactly as my script is in the Dungeon.. So forgive some comma errors. Also: I only did up to i = 3 in my example above, as I'm sure you get the point...

Ideal #1- I'd love to be able to declare the variable locally. Am I doing that wrong? (clearly I am, as it gives me an error message).
Ideal #2- If I declare the variable globally, where do I declare it? I tried in a few different files and it would return an error message again.

Variables...

Thanks,

Ryan

Re: [LUA] Declaring variables (Local and Global)

Posted: Fri Oct 19, 2012 4:20 pm
by Grimwold
Not a direct answer to your question, but some suggestions.

It might be better to shift the conversation code to a script entity in the dungeon as there's less chance of crashing the editor if you script something incorrectly... so in your monster file the damage hook would have simply..

Code: Select all

onDamage = function(self, damage,type)
  conversation_script.haveConversation(self,dir)
  return true
end,
Then place a script entity called conversation_script with a function haveConversation()


Now the thing with variables is that local variables do not persist outside of the function they are called in, and are not saved in save games. So you either want to declare your variable in a script entity, or use a counter instead. (also I'm pretty sure declaring a local variable in the monster.lua file will not persist into the actual game)

So an example using variables in a script entity:

Code: Select all

i = 1

function haveConversation()
  if i == 1 then
    hudPrint("line 1")
  elseif i == 2 then
    hudPrint("line 2")

-- and so on
  end
i = i +1
end
I refinement of this could be:

Code: Select all

i = 1
function haveConversation()
  local dialogue = {"line 1","line 2","line 3","line 4","line 5"}
  hudPrint(dialogue[i])
  i = i +1
end
Hope that helps somewhat.

Re: [LUA] Declaring variables (Local and Global)

Posted: Fri Oct 19, 2012 5:09 pm
by Wolfrug
Furthermore, even global variables are 'garbage collected' very quickly - I haven't tested how fast exactly, but afaik we're talking 5-10 seconds. That means that, unlike many other scripting languages, you can't rely on global variables to retain information between scripts :(

Maybe you could use a counter, and increment it after every piece of dialogue, and use that data to see which comes next?

-Wolfrug

Re: [LUA] Declaring variables (Local and Global)

Posted: Fri Oct 19, 2012 5:24 pm
by Grimwold
Wolfrug wrote:Furthermore, even global variables are 'garbage collected' very quickly - I haven't tested how fast exactly, but afaik we're talking 5-10 seconds. That means that, unlike many other scripting languages, you can't rely on global variables to retain information between scripts :(

-Wolfrug
I was not aware this was the case. I've used some variables across repeated calls to the same function in a script entity without any trouble... although I did have to re-write some scripts based on what it says about data types in the documentation - http://www.grimrock.net/modding/save-ga ... variables/

Re: [LUA] Declaring variables (Local and Global)

Posted: Fri Oct 19, 2012 6:21 pm
by Wolfrug
Grimwold wrote: I was not aware this was the case. I've used some variables across repeated calls to the same function in a script entity without any trouble... although I did have to re-write some scripts based on what it says about data types in the documentation - http://www.grimrock.net/modding/save-ga ... variables/
Ah right, yes. I assumed they were being garbagecollected, but in fact they're simply local to the script (entity) in every case! Normally what I consider a global variable would be truly global, that is to say this would be possible:

Code: Select all

(scriptEntity 1)

function test()
globalvariable = "Hello"
end

Code: Select all

(scriptEntity 2)

function test2()
print (globalvariable)
end
My mistake! I prefer to split my scripts up into readable chunks, rather than having EVERYTHING contained within one script entity, but maybe I'll change that now that I know this :)

-Wolfrug

Re: [LUA] Declaring variables (Local and Global)

Posted: Fri Oct 19, 2012 7:53 pm
by pinnguaq
Hope that helps somewhat.
It was very helpful. Thank you very much.