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,
}
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,
}
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
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,
}
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