Skip to content

Instantly share code, notes, and snippets.

@baughj
Created February 12, 2020 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baughj/884b980ef6f97a87a038e0d3f813dd67 to your computer and use it in GitHub Desktop.
Save baughj/884b980ef6f97a87a038e0d3f813dd67 to your computer and use it in GitHub Desktop.
Asynchronous dialog (Lua) test for Hybrasyl
function OnLoad()
-- Something that should only run the very first time the script is instantiated,
-- rather than every time it is evaluated.
asynctest_send = {"Hello! This is a test of the asynchronous dialog system.",
"In the next dialog, please enter the name of an Aisling in the world. They (should) receive a dialog.",
"I have sent the dialog request. Isn't this fun?"}
asynctest_send_seq = world.NewDialogSequence("asynctest_send",
world.NewDialog(asynctest_send[1]),
world.NewDialog(asynctest_send[2]),
world.NewTextDialog("Enter the name of a victim. No, that's not right, I mean tester...",
"The name of the person", "To receive the test", 16, "", "async_handler()"),
world.NewDialog(asynctest_send[3]))
asynctest_recv = {"Hello! This is a test of the asynchronous dialog system.",
"You've received this dialog because {{invoker}} sent it to you.",
"Hopefully you're seeing it!"}
options = world.NewDialogOptions()
options.AddOption("Stuff", "async_target_response()")
options.AddOption("Things", "async_target_response()")
asynctest_recv_seq = world.NewDialogSequence("asynctest_recv",
world.NewDialog(asynctest_recv[1]),
world.NewDialog(asynctest_recv[2]),
world.NewDialog(asynctest_recv[3]),
world.NewOptionsDialog("Select an option. The user will receive a notification of \z
what you selected.",options))
-- this_script is a global that holds the name of the currently executing script
-- Your recipient script doesn't necessarily need an associate, but, in order to do
-- anything even vaguely interesting, it needs to be able to process callbacks / etc
asynctest_recv_seq.AssociateWithScript(this_script)
asynctest_recv_seq.SetDisplayName("Async Receive Test")
asynctest_recv_seq.SetNpcDisplaySprite(3)
asynctest_send_seq.SetDisplayName("Async Send Test")
asynctest_send_seq.AssociateWithScript(this_script)
asynctest_send_seq.SetNpcDisplaySprite(3)
world.RegisterGlobalSequence(asynctest_recv_seq)
world.RegisterGlobalSequence(asynctest_send_seq)
end
function OnUse()
invoker.SystemMessage("async test beginning")
invoker.StartSequence("asynctest_send")
end
-- This is the function that is called by the dialog handler
function async_handler()
-- player_response contains the player's text response
target = world.GetUser(player_response)
if target == nil then
invoker.SystemMessage("That Aisling is nowhere to be found.")
invoker.EndDialog()
return
else
-- User is logged in.
-- This function returns true/false, it's possible for it to fail under various
-- circumstances (player is busy, in combat, etc).
invoker.SystemMessage("I will ask " .. target.Name .. " about it...")
-- This returns a boolean and it's your job to handle it
result = target.RequestDialog("asynctest_recv", invoker.Name)
if (not result) then
-- Could also start a "Oh well" dialog sequence here, or do a lot of other things
invoker.EndDialog()
end
end
end
-- This is the function that will be called when the target selects a response
function async_target_response()
-- player_response contains the target's response
-- source is the object who sent the dialog (NPC or user)
-- invoker is the person (HybrasylUser) responding to the dialog
-- (in this case, a user casting this spell, but also, potentially a npc taking some action)
source.SystemMessage("They have selected " .. player_response .. ".")
invoker.SystemMessage("I've let them know what you wanted.")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment