Godot Version
Godot 4.3
Question
`Hi, I was wondering if anyone knew how I could have a value in an array that is a reference to a variable, as opposed to the value of the variable, I have this line of code here,
dialogReplies.append(["Is my weapon drawn?", DS_VAR_CHECK, player.itemDrawn , 3, 4])
And I have a script that checks to see if the value of the variable in the [2] position of the array is true or not, however, on creation, the place in the array is set to the value of the variable, and so stays at false, and never changes, how can I make it so the position is a reference to the variable instead of the value of the variable`
No but maybe you can change your dialog system to accept indexable strings, like how tween_property
works?
Given an Object and a String (NodePath) you could use player.get_indexed("itemDrawn")
for example.
I’m currently trying to store the variable as a string, then using the get() function to recall the string as the variable. This is returning null however and I don’t know what to do.
Could you show your code parsing this array? And the error message?
So the NPC create the dialogue line in their ready function,
dialogReplies.append(["Is my weapon drawn?", DS_VAR_CHECK, "player.itemDrawn" , 3, 4])
This includes the text, the function, the variable to check, the dialogue step to go to if the variable is true, and the dialogue step to go to if the variable is false.
Then the dialogue window has this function for selecting dialogue options,
elif dialog3Reply[Dialog.target.DIALOG_SCRIPT] == Dialog.DS_VAR_CHECK:
Dialog.var_check(dialog3Reply[dialogVarCheck],dialog3Reply[dialogGotoVal],dialog3Reply[dialogGotoVal2])
Which then goes to the global dialogue manager which runs this function,
func var_check(varCheck,trueNum,falseNum):
print(get(varCheck))
if get(varCheck):
dialogStep = trueNum
else:
dialogStep = falseNum
There’s no error code, however the get(varCheck)
function is returning null, even though if I remove the get()
function, varCheck is correctly stored as the player.itemDrawn
string.
Thank you! The function get
does not index, it only looks at local variables and does not get properties within that local variable, get("player.itemDrawn")
will look for a single variable named player.itemDrawn
, which is impossible since the dot operator means it must be a variable within an object, a single variable cannot be declared var player.itemDrawn
. get_indexed
can fix this, but it requires different formatting, and player
must exist on the script calling get_indexed
.
Does your global dialogue manager have a player
variable? You may need to also add the object to the array, if you intend to operate on non-player objects. Seems like you will need to change the dialogue reader dialogVarCheck
into two parts, maybe dialogVarCheckObject
and dialogVarCheckIndex
dialogReplies.append(["Is my weapon drawn?", DS_VAR_CHECK, player, "itemDrawn" , 3, 4])
func var_check(check_object: Object, check_index: NodePath, trueNum, falseNum) -> void:
if check_object.get_indexed(check_index):
dialogStep = trueNum
else:
dialogStep = falseNum
Hi, I fixed it by adding an extra part in the dialogReplies array that checks the object the variable belongs to, and it’s working now. Thank you for your help!