Guest User

Dialog Box Script

a guest
Feb 18th, 2021
6,423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends ColorRect
  2.  
  3. export var dialogPath = ""
  4. export(float) var textSpeed = 0.05
  5.  
  6. var dialog
  7.  
  8. var phraseNum = 0
  9. var finished = false
  10.  
  11. func _ready():
  12.     $Timer.wait_time = textSpeed
  13.     dialog = getDialog()
  14.     assert(dialog, "Dialog not found")
  15.     nextPhrase()
  16.  
  17. func _process(_delta):
  18.     $Indicator.visible = finished
  19.     if Input.is_action_just_pressed("ui_accept"):
  20.         if finished:
  21.             nextPhrase()
  22.         else:
  23.             $Text.visible_characters = len($Text.text)
  24.  
  25. func getDialog() -> Array:
  26.     var f = File.new()
  27.     assert(f.file_exists(dialogPath), "File path does not exist")
  28.    
  29.     f.open(dialogPath, File.READ)
  30.     var json = f.get_as_text()
  31.    
  32.     var output = parse_json(json)
  33.    
  34.     if typeof(output) == TYPE_ARRAY:
  35.         return output
  36.     else:
  37.         return []
  38.  
  39. func nextPhrase() -> void:
  40.     if phraseNum >= len(dialog):
  41.         queue_free()
  42.         return
  43.    
  44.     finished = false
  45.    
  46.     $Name.bbcode_text = dialog[phraseNum]["Name"]
  47.     $Text.bbcode_text = dialog[phraseNum]["Text"]
  48.    
  49.     $Text.visible_characters = 0
  50.    
  51.     var f = File.new()
  52.     var img = dialog[phraseNum]["Name"] + dialog[phraseNum]["Emotion"] + ".png"
  53.     if f.file_exists(img):
  54.         $Portrait.texture = load(img)
  55.     else: $Portrait.texture = null
  56.    
  57.     while $Text.visible_characters < len($Text.text):
  58.         $Text.visible_characters += 1
  59.        
  60.         $Timer.start()
  61.         yield($Timer, "timeout")
  62.    
  63.     finished = true
  64.     phraseNum += 1
  65.     return
Advertisement
Add Comment
Please, Sign In to add comment