0 votes

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?

in Engine by (12 points)

1 Answer

0 votes

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)...
by (10,932 points)

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 onNo_pressed():
visible = false

func onYespressed():
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 onNextpressed():
Turn = Turn + 1
Money = Money + (Income * 2)
update
ui()
end()

Nevermind I figured it out.

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.