Can't get width of a parent, parent node

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ondesic
:warning: Old Version Published before Godot 3 was released.

I have watched hours and hours of youtube videos and thought I understood the basics of Godot. However, I am getting stuck on code. Could someone please help me? Here is my setup:

Node

  • Background (sprite)
    • Water (sprite)
    • Player (node)
      • RigidBody2D
      • Sprite
      • AnimationPlayer

The Player is an instance. I have attached a script to the RigidBody2D which is trying to do the following:

  1. move the Player continuously to the right. and
  2. when the Player reaches the edge of the Background sprite, make it start over.

The problem is I can’t through code access the width of the Background sprite. I have tried using get_node(“…/Background”) and a few others with no luck.
How should I do this?

Instead of doing the classic way of checking size and positions of things you can use line or segments Areas to make limits and let the hard work to the engine :slight_smile:

eons | 2017-04-03 01:05

:bust_in_silhouette: Reply From: bruteforce
var bg = get_parent().get_parent()

or

var bg = get_node("../../")

or

var bg = get_node("../../../Background")

etc…

Thanks for responding. Even using these examples I can’t seem to get the texture width of the sprite Background.

I type “get_parent().get_parent()” then a “.” However, none of the properties of a Sprite are available in the auto complete drop down. I figured I would get the “get_rect” or “get_texture” or “get_pos” etc would be available, but none of them are. What can I do to get the width of the sprite?

Thanks

ondesic | 2017-04-03 15:06

You are doing something wrong :slight_smile:

Here is a screenshot:
enter image description here

And the code completion also works properly:
enter image description here

bruteforce | 2017-04-03 16:48

Bruteforce,

For some reason your screenshots show as broken. I can’t see the pictures.

When I print get_type on the var bk, it says “Sprite”, so I know I am in the right place, I just can’t assess the properties. Your screen shots will probably help when I can see them.

ondesic | 2017-04-03 18:17

OK, the first:

extends RigidBody2D

func _ready():
	print( get_node("../../").get_texture().get_size() )
	print( get_node("../../").get_texture().get_width() )
	print( get_node("../../").get_texture().get_height() )

The output:

(58, 94)
58
94

The second image is just a screenshot about code completion

Direct links:
1.) http://i.imgur.com/HurTNTF.jpg
2.) http://i.imgur.com/P5mH6rP.jpg

bruteforce | 2017-04-03 18:40

I have followed everything and it will not work.

I did a test and added a new node and a rigidbody2d. This time it works.
However, like I mentioned in the original post, the Player node (with the rigidbody2d) is an instance. I wonder if this has anything to do with it?

ondesic | 2017-04-03 20:02

Give us more details about the error (error message, the complete project / relevant code snippet… or something)!
You said:

“…When I print get_type on the var bk, it says Sprite…”

So you found the sprite node…

The sprite has a get_texture() member function, which returns a Texture object.
Doc: Sprite class
If this texture is not null, you can call it’s get_size(), get_width(), get_height()… functions.
Doc: Texture class

bruteforce | 2017-04-04 11:38

Bruteforce,

I completely understand that the sprite should have get_width(), get_texture() etc. It just simply isn’t there.

In the best detail I can give:

I created a scene with a node called “Player”.
I added 3 children under this node called “RigidBody2D”, “Sprite”, and “AnimationPlayer”

I saved the scene.

Next, I opened my main scene that has the background sprite in it.
I add the Player scene to the Main scene as an Instance.
I had to click on the Player node and select “Editable Children” to gain access to the RidigBody2D inside Player.
Next I added a script to the RigidBody2D.

It is in this script that no matter what I do I cannot access the Main scene’s Background sprite’s properties.

It is strange because if I add a node directly to the Main scene (not an instance), I can access the background spite’s properties. However, I can’t through the children of the Instantiated Player node. Does that make sense?

ondesic | 2017-04-04 15:51