Home Tutorials Download Beta Store Forum Documentation KnowledgeBase Wiki Blog

ShiVa3D

Return to User contributions

dialogue example

dialogue example

Postby Cheramu » 27 May 2012, 22:14

This is what i 'm using currently for dialogue, huds text and translation in my game prototypes.
There is a game example on http://layers.ichange.nl (click preview or download the android example).

I currently have a developer preview of polyglot-dialogue editor tool that can generate the XML look under forum external tools.
If you have time please have a look and give feedback in that topic.

How it works
It loads a hash table with key values with you're data, you register by the AI_DIALOGUE a handler that get a event when there
is dialogue data to display. To create a event you send onShowDialog with start or next dialogue id.

Overview
XML format example (minium needed "dialog" fields to work)
Code: Select all
<items>
  <item type="text" id="T(0)" field="TITLE_EXAMPLE" value="hoi" />
  <item type="text" id="T(0)" field="TITLE_EXAMPLE-ja-JP" value="konichiwa" />
  <item type="text" id="T(0)" field="SUBTITLE_EXAMPLE" value="gaat het goed" />
  <item type="text" id="T(0)" field="SUBTITLE_EXAMPLE-ja-JP" value="genki des ka" />

  <item type="actor" id="A(narrator)" field="TAG" value="A(narrator)" />
  <item type="actor" id="A(narrator)" field="NAME" value="narrator" />

  <item type="conversation" id="D(0)" field="ID" value="D(0)" />

  <item type="dialog" id="D(0)" field="ID" value="D(0)" />
  <item type="dialog" id="D(0)" field="TAG" value="" />
  <item type="dialog" id="D(0)" field="ACTOR_TAG" value="A(narrator)" />
  <item type="dialog" id="D(0)" field="TEXT" value="a polyglot-dialogue example" />
  <item type="dialog" id="D(0)" field="TEXT-ja-JP" value="" />
  <item type="dialog" id="D(0)" field="NEXT" value="D(1)" />
  <item type="dialog" id="D(0)" field="CONDITION" value="" />

  <item type="dialog" id="D(1)" field="ID" value="D(1)" />
  <item type="dialog" id="D(1)" field="TAG" value="" />
  <item type="dialog" id="D(1)" field="ACTOR_TAG" value="A(narrator)" />
  <item type="dialog" id="D(1)" field="TEXT" value="Hello World" />
  <item type="dialog" id="D(1)" field="TEXT-ja-JP" value="Konichiwa sekai" />
  <item type="dialog" id="D(1)" field="NEXT" value="" />
  <item type="dialog" id="D(1)" field="CONDITION" value="" />
</items>


You can also load the data directly in code example, there is a xml loading example code add the bottom of the post.
hashtable.add ( this.htDialogs ( ), "D(1).DIALOG_TEXT", "Hello World" )

AI_DIALOGUE handlers
function AI_DIALOGUE.onDialog ( sINPUT_DIALOG_ID )
function AI_DIALOGUE.onRefresh ( )
function AI_DIALOGUE.onRegister ( sAIModel ,sCallbackEvent )
function AI_DIALOGUE.onSetDialog ( sINPUT_DIALOG_ID )
function AI_DIALOGUE.onSetLanguage ( sLANGUAGE_ID )

AI_DIALOGUE variables
local sLangcode = application.getCurrentUserAIVariable ( "AI_DIALOGUE", "sLangcode" )
-- contains the current language
local htActors = application.getCurrentUserAIVariable ( "AI_DIALOGUE", "htActors")
-- contains actor data
local htText = application.getCurrentUserAIVariable ( "AI_DIALOGUE", "htText" )
--contains the dialogs that are to be showed to the user
local htShowDialogList = application.getCurrentUserAIVariable ( "AI_DIALOGUE", "htShowDialogList" )
--on dialogue start it will check these dialog id with condiation and loads the first that passes
local tConversations = application.getCurrentUserAIVariable ( "AI_DIALOGUE", "tConversations")
--contains all the dialogdata
local htDialogs = application.getCurrentUserAIVariable ( "AI_DIALOGUE", "htDialogs")
--contains the dialog types
local tDialogTypes = application.getCurrentUserAIVariable ( "AI_DIALOGUE", "tDialogTypes")

--Game hud text implemenation example
Code: Select all
local htText = application.getCurrentUserAIVariable ( "AI_DIALOGUE", "htText" )
local sLangcode = application.getCurrentUserAIVariable ( "AI_DIALOGUE", "sLangcode" )

local text = hashtable.get ( htText, sTextId )
if ( hashtable.contains ( htText, sTextId .. "-" .. sLangcode ) )
then
   text = hashtable.get ( htText, sTextId .. "-" .. sLangcode )
end

if ( text == nil )
then
   text = "?%" .. sTextId .."%?";
   log.warning ( "Cannot translate text: " .. sComponentTag .. "." .. sTextId )
end


AI_DIALOGUE implementation example.

--example ai named pdController

-- handler onInit
Code: Select all
--local Variable htLangCodes
hashtable.add ( this.htLangCodes ( ), "LANGUAGE-EN", "" )
hashtable.add ( this.htLangCodes ( ), "LANGUAGE-JP", "JP" )
hashtable.add ( this.htLangCodes ( ), "LANGUAGE-NL", "NL" )

-- load xml data
local hXmlContent = xml.newInstance ( )
xml.createFromResource ( hXmlContent, "pd.xml" )
loadPDXML ( hXmlContent )

user.sendEvent ( this.getUser ( ), "AI_DIALOGUE", "onSetLanguage", sNewLangCodeValue )

--register listener for dialogue events
user.sendEvent ( this.getUser ( ), "AI_DIALOGUE", "onRegister", "pdController", "onDisplayDialogue")


-- handler onDisplayDialogue
Code: Select all
-- display you're dialog in hud
local htDialogs = application.getCurrentUserAIVariable ( "AI_DIALOGUE", "tShowDialogList" )   
log.message ( "Show: " .. table.getSize ( htDialogs ) )
local nDialogCount = table.getSize ( htDialogs )
if ( nDialogCount == 0 )
then
-- conversattion ended
elseif ( nDialogCount == 1 )
then
-- dialog text
else
-- dialogs options
end


-- handler onClickDialogue ( sDialogId )
Code: Select all
-- proces youre dialog hud with the dialog id to continue
log.message ( "show next dialog: " ..  sDialogId )
user.sendEvent ( this.getUser ( ), "AI_DIALOGUE", "onDialog", sDialogId )


-- method onStartGame
Code: Select all
-- start a dialogue
user.sendEvent ( this.getUser ( ), "AI_DIALOGUE", "onDialog","start" )


--method loadPDXML
Code: Select all
--load dialogue data
local hRoot = xml.getRootElement ( hXML )
local hChild = xml.getElementFirstChild ( hRoot )

while ( hChild )
do
   local sType  = xml.getAttributeValue ( xml.getElementAttributeWithName ( hChild, "type" ) )
   local sId    = xml.getAttributeValue ( xml.getElementAttributeWithName ( hChild, "id" ) )
   local sField = xml.getAttributeValue ( xml.getElementAttributeWithName ( hChild, "field" ) )
   local sValue = xml.getAttributeValue ( xml.getElementAttributeWithName ( hChild, "value" ) )

   if ( sType == "conversation" )
   then
      local tConversations = application.getCurrentUserAIVariable ( "AI_DIALOGUE", "tConversations")
      table.add ( tConversations, sId )
      log.message ( "added conversation field: " .. sId )
   elseif ( sType == "dialog" )
   then
      local htDialogs = application.getCurrentUserAIVariable ( "AI_DIALOGUE", "htDialogs")
      if ( hashtable.contains ( htDialogs, sId .. "." ..  sField ) )
      then
         hashtable.set ( htDialogs, sId .. "." .. sField , sValue )
      else   
         hashtable.add ( htDialogs, sId .. "." .. sField , sValue )           
      end
      log.message ( "added dialog field: " .. sId .. "." .. sField .. " ".. sValue)
   elseif ( sType == "actor" )
   then
      local htActors = application.getCurrentUserAIVariable ( "AI_DIALOGUE", "htActors")
      hashtable.add ( htActors, sId .. "." .. sField , sValue )
      log.message ( "added actor field: " .. sId .. "." .. sField .. " ".. sValue) 
   elseif ( sType == "dialog-type" )
   then
      local tDialogTypes = application.getCurrentUserAIVariable ( "AI_DIALOGUE", "tDialogTypes")
      table.add ( tDialogTypes, sValue )
      log.message ( "added dialog type: " .. sId .. "." .. sField .. " ".. sValue)     
   elseif ( sType == "text" )
   then
      local htText = application.getCurrentUserAIVariable ( "AI_DIALOGUE", "htText")
      hashtable.add ( htText, sField ,sValue )
      --log.warning ( "added text field: " .. sId .. "." .. sField .. " ".. sValue)     
   end
   
   hChild = xml.getElementNextSibling ( hChild )
end
log.message ( "XML loading compleet" )
Cheramu
Senior Boarder
Senior Boarder
 
Posts: 60

Return to User contributions