Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node
- var panelNode
- var story = { }
- var events = { }
- var choices = { }
- var currDialogue
- var inDialogue = false
- var currChoice
- var buttonPressed
- var isChoice
- var isChoiceDialogue
- var oldChoices = { }
- var oldWasChoice
- var dialogueOptions = []
- func load_file_as_JSON(path, target):
- var file = File.new()
- file.open(path, file.READ)
- var content = (file.get_as_text())
- target.parse_json(content)
- file.close()
- func _ready():
- set_process_input(true)
- set_fixed_process(true)
- load_file_as_JSON("Narrative/cycle1.json", story)
- load_file_as_JSON("Narrative/events.json", events)
- load_file_as_JSON("Narrative/choices.json", choices)
- panelNode = get_node("../CanvasLayer/Panel")
- var initButton = panelNode.get_node("Button")
- initButton.connect("pressed", self, "_on_button_pressed", [initButton])
- opening()
- func _fixed_process(delta):
- if(inDialogue == false):
- panelNode.hide()
- func opening():
- inDialogue = true
- panelNode.show()
- currDialogue = story["awakening"]["content"]
- panelNode.get_node("Label").set_text(currDialogue[0])
- func load_next():
- if currDialogue[1].has("divert"):
- oldWasChoice == false # used for update_choices, make sure to clear each iteration
- currDialogue = story[currDialogue[1]["divert"]]["content"]
- else:
- oldWasChoice = true
- oldChoices = currDialogue.size() # used for update_choices, make sure to clear each iteration
- currDialogue = story[currDialogue[get_user_choice(currChoice)]["linkPath"]]["content"]
- print("succesfully transitioned to choice " + str(currDialogue))
- func _on_Button_pressed():
- load_next()
- panelNode.get_node("Label").set_text(currDialogue[0]) #prints next line to screen
- update_choices(currDialogue)
- func _on_button_pressed_target(target):
- currChoice = target
- load_next()
- panelNode.get_node("Label").set_text(currDialogue[0]) #prints next line to screen
- update_choices(currDialogue)
- func get_user_choice(target):
- var buttonName = target.get_name()
- var id = buttonName.to_int()
- return id
- func update_choices(dialogue): # takes dialogue and makes enough buttons and labels for the corresponding choice
- # must be called after load_next()
- if (oldWasChoice == true): #if previous dialogue was a choice
- for i in range (1, oldChoices):
- panelNode.get_node("ChoiceButton" + str(i)).queue_free()
- print("queued freed button " + str(i))
- for item in currDialogue:
- if(typeof(item) != TYPE_STRING and item.has("linkPath")):
- dialogueOptions.append(item)
- for i in range(1, dialogue.size()):
- var choiceButton = Button.new()
- choiceButton.set_name("ChoiceButton" + str(i))
- panelNode.add_child(choiceButton)
- choiceButton.set_pos(Vector2(10, (20*i) + 72))
- choiceButton.set_size(Vector2(512,20))
- choiceButton.connect("pressed", self, "_on_button_pressed_target", [choiceButton])
- var choiceLabel = Label.new()
- choiceLabel.set_name("Choice Label" + str(i))
- panelNode.get_node("ChoiceButton" + str(i)).add_child(choiceLabel)
- choiceLabel.set_pos(Vector2(10, 5))
- choiceLabel.set_size(Vector2(512, 20))
- choiceLabel.set_autowrap(true)
- choiceLabel.set_text(dialogueOptions[i-1]["option"])
- choiceLabel.show()
- print("____________")
- print(str(i))
- print(choiceLabel.get_text())
- print(choiceLabel.get_visible_characters())
- print(choiceLabel.get_lines_skipped())
- print(choiceLabel.get_max_lines_visible())
- print(choiceLabel.is_visible())
- print(choiceLabel.is_hidden())
- oldWasChoice = null
- oldChoices = null
- dialogueOptions.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement