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 (16,798 points)

The code of the child:

extends Panel

var Cost = 0
var Turns = 0

func _ready():

func Payment():
var Cost = randi() %10 + 5

var Turns = randi() %8 + 1

get_node('Dur').text = str(Turns) + ' ' + 'Turns'

get_node("CashLabel").text = 'Income: ' + str(Cost)

func onNo_pressed():
visible = false

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

func onNextpressed():
Turn = Turn + 1
Money = Money + (Income * 2)

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 Frequently asked questions and 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 [email protected] with your username.