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?

asked Mar 15 in Engine by Samo3000 (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)...
answered Mar 15 by jgodfrey (6,576 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 How to use this Q&A? before posting your first questions.