0 votes

I'm putting dialogue into my game . And at one point I ask for the player's name by using a line edit. I then store the player's name into a global var.

func _on_LineEdit_text_entered(new_text): # Storing name to global
Global.playername = new_text
print(Global.playername)
$Control.hide()
emit_signal("nametyped")

extends Node2D #Global Script

var playername

In my rich text script I have my dialogue array. And the players name is said by getting it from the global var. Problem is, the dialogue shows null for players name. I printed the global var to make sure it has the player name, and it does, so why is it showing null in the dialogue? Pulling my hair over this one, feels like I tried everything but nothing is fixing it.

var dialog = ["Oh " + str(Global.playername) + " Neat name"]
asked Jul 17 in Engine by Asthmar (267 points)

Hi,
and the "Global" is a singleton?

Yes its a singleton

Well, without the providing the full code i cant help you then.
- are you sure the dialog array is constructed after the name is entered?
- have you set a breakpoint after constructing the dialog to analyse the current state of your objects?
- a your sure the dialog does not get overwritten by any cause?
- are your sure you have not overwritten or rebuild then "Global" object?

.. you have to trace back the error, i bet its a simple code flow problem.

Okay then I think the problem may be that my dialogue is constructed before I ask for the name. I have my dialogue completely written out in one script and in another script I get the name. Im kinda clueless on how to get the name without having the dialogue written out first. i'm using hyper beast dialogue system setup.

More code :
inside my richtext label this is when the line edit shows

var dialog = [ "Hey!", " You!", " Creet, my name's creet", "Whats your's?", "" #here is page 10 and I signal to show the line edit ,  "Oh " + str(Global.playername) + " Neat name", ]

func _process(delta):
Global.page = page
if page == 8 && Global.onpage == true:
    emit_signal("showhim")
if page == 9:
    Global.onpage = true
if page == 10:  # This is where I show the line edit 
    canread = false
    emit_signal("showbox")

In parent script

func _on_LineEdit_text_entered(new_text):
Global.playername = new_text
print(Global.playername)
$Control.hide()
emit_signal("nametyped")

1 Answer

0 votes
Best answer

Based on the comments above.

In your code the dialog string is constructed when the script starts. The string wont change if you modified variables that where used for the construction.

You can construct your array after you received the players name

var dialog

func _on_LineEdit_text_entered(new_text):
Global.playername = new_text
print(Global.playername)
dialog = [ "Hey!", " You!", " Creet, my name's creet", "Whats your's?", "" #here is page 10 and I signal to show the line edit ,  "Oh " + str(Global.playername) + " Neat name", ]
$Control.hide()
emit_signal("nametyped")

or you want to use String.replace() after your receive the name

var     dialog = [ "Hey!", " You!", " Creet, my name's creet", "Whats your's?", "" #here is page 10 and I signal to show the line edit ,  "Oh #insert_name_here# Neat name", ]


func _on_LineEdit_text_entered(new_text):
Global.playername = new_text
dialog = dialog.relpace("#insert_name_here#",Global.playername)
print(Global.playername)
$Control.hide()
emit_signal("nametyped")
answered Jul 17 by klaas (2,334 points)
selected Aug 9 by Asthmar
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.