Code: Select all
-- dialog_script object
function dialog(id, actors)
local c = {} --c.conv_1 c.conv_2 c.conv_3 ...... c.conv_50...
--conversation table. (The "conv_#" names can actually be anything you like; they are manually called.)
c.conv_1 = { -- Number entries are the intended speaker's given position in the "converse" function's actors array.
2, "Want hear new recipe "..actors[1].."?",
1, "What kind?",
2, "Rat surprise.",
1, "Sound good. How me make?",
2, "Catch rat. Careful no squish too much.",
1, "How no squish? Every time I club them, they pop. Very messy.",
2, "You want hear recipe or not? Catch rat your problem.",
1, "Me sorry.",
2, "Like said, catch rat. Hang by tail on wall.",
"Let hang til tail fall off. It ready then. Eat quickly",
"if let sit too long, the surprise crawl out and fly away.",
1, "Sound yummy.",
3, "That sounds disgusting!",
0, actors[1].." and "..actors[2].." both scream with fright!"
}
-- c.conv_2 = {}
-- c.conv_3 = {}
--validity test
for keys in pairs(c) do
if keys == id then
return c[id]
end
end
--default
print("Invalid conversation id given to "..self.id, ">>function dialog(id)", "Key does not exist.")
return "Error: See Console Output"
end
-- counts the existing PCs
function number_of_PCs()
local count = 0
for x = 1, 4 do
if party:getChampion(x):getName() then
count = count + 1
end
end
return count
end
number_of_PCs = number_of_PCs()
-- accepts ordinal, returns the correct champion
function getChampionByOrdinal(ordinal)
for x = 1, number_of_PCs do
if party:getChampion(x):getOrdinal() == ordinal then
return party:getChampion(x)
end
end
end
-- setup delayed calls to displayText()
function delayedHudPrint(text, delay)
local ID = "timer"..getStatistic("play_time").."@"..text
if not findEntity(ID) then
spawn("timer",party.level,0,0,0, ID):setTimerInterval(delay):addConnector("activate",self.id,"displayText"):addConnector("activate",ID,"destroy"):activate()
end
end
function converse(id, ...) -- "..." becomes the actors array, and holds either the PC ordinals, or the names of the speakers.
-- For specific party positions, or the names of random PC's, you must supply their names. EG. converse( "conv_1", "Jack", "Jill", party:getChampion(4):getName() )
local actors = {...}
--process actors
--convert names & ordinals to id-numbers
for x = 1, #actors do
if type(actors[x]) == "number" then
actors[x] = getChampionByOrdinal(actors[x]):getName()
end
end
local dialog = dialog(id, actors)
local line = 1
-- display dialog
local speaker = ''
local time_delay = 4 -- Uniform delay (in seconds) between each line of dialog
for t = 1, #dialog, 2 do
speaker = string.rep(" ", #speaker) --once aligned printed console output
line = t
if type(dialog[line]) == "number" and dialog[line] >0 and dialog[line] <= #actors then --Who is speaking; indicated by a number before the dialog
speaker = actors[dialog[line]]..': '
line = line + 1
end
local text = speaker..'"'..dialog[line]..'"'
if dialog[line] == 0 then text = dialog[line+1] end --narrative option (removes quotes, and speaker name)
delayedHudPrint(text, t*(time_delay/2))
end
end
-- wrapper for the hudPrint function; called by a timer.
function displayText(caller) playSound("click_down")
hudPrint("") hudPrint("")
hudPrint(tostring(caller.id:sub(caller.id:find("@")+1)))
hudPrint("") hudPrint("")
end
--Example usage:
converse("conv_1", 2, party:getChampion(1):getName(), "A nearby ghost")
-- Example usage input description [for the above]:
-- "conv_1" The conversation to be printed out
-- 2 The ordinal of the first PC. [Will always be that PC, no matter what slot they occupy in the party]
-- party:getChampion(1):getName() The name of the PC who is in the #1 party position
-- "A nearby ghost" A forced name for a 3rd speaker.
The scripting reference for Grimrock has most of the functions that you might need for this; see the Champion section.