system
March 15, 2020, 2:21pm
1
Attention
Topic was automatically imported from the old Question2Answer platform.
Asked By
Samo3000
I can’t figer out how to use a variable from a parent node.
I have a variable called Income in the parent node and need to use it in its child.
So I wrote get_parent().Income but I still cant use it. Can I even do that?
system
March 15, 2020, 2:29pm
2
Reply From:
jgodfrey
Yes, you can do that. I’d guess there’s something a little off with how you’ve set things up, but it’s difficult to know without more details.
How about some screen shots. Maybe 1) the scene tree, 2) the script on the parent , and 3) the script on the child ?
For the above to work…
The parent node must have a script with an Income
variable
A direct child of the parent must have the script containing theget_parent().Income = 25
(for example)…
The code of the child:
extends Panel
var Cost = 0
var Turns = 0
func _ready():
Payment()
func Payment():
var Cost = randi() %10 + 5
randomize()
var Turns = randi() %8 + 1
randomize()
get_node('Dur').text = str(Turns) + ' ' + 'Turns'
get_node("CashLabel").text = 'Income: ' + str(Cost)
func _on_No_pressed():
visible = false
func _on_Yes_pressed():
visible = false
get_parent().Income = Cost
Code of the parent:
extends Panel
var Money = 500
var Soldiers = 0
var Rented = 0
var Income = 0
var Turn = 1
var MaxRent = 0
var TurnRent = 0
func _ready():
update_ui()
func _on_Next_pressed():
Turn = Turn + 1
Money = Money + (Income * 2)
update_ui()
end()
Samo3000 | 2020-03-15 15:03
Nevermind I figured it out.
Samo3000 | 2020-03-15 15:16