#Why cant i grab position from an export_node_path variable?

28 messages · Page 1 of 1 (latest)

slate elbow
#

I am an absolute beginner to godot and programming in general. I picked up godot 4 (not the best idea since documentation for that version isn't the best right now) and decided it would be a great idea to follow a tutorial for godot 3 (if its not obvious from the code its a pong clone tutorial), again, not the best idea, and ran into the problem described in the title

extends CharacterBody2D

const SPEED = 200.0
var move_direction = Vector2(0,get_opponent_diretion())


@export_node_path(CharacterBody2D) var ball

func _physics_process(_delta):
    velocity = move_direction * SPEED
    move_and_slide()

func get_opponent_diretion():
    if abs(ball.position.y - position.y) > 25:
        if ball.position.y > position.y: return 1
        else: return -1
    else: return 0

I get an error at line 14, when trying to get the position of the ball from the @export_node_path(CharacterBody2D) var ball variable, can somebody explain why this is and how i can fix it?

#

just noticed that there is a tag for beginner, didn't see it when making the post

cosmic crescent
#

You exported a node path not a node

slate elbow
#

how would one export a node?

sullen path
#

@export var ball : CharacterBody2D

cosmic crescent
#

Yeah that

slate elbow
#

nice thank you

#

i am getting the same error still

Invalid get index 'position' (on base: 'Nil')
cosmic crescent
#

Did you set it in the inspector

slate elbow
#

yeah i did, i still get the error

ebon dune
#

What does your scene tree look like (top left panel)?

slate elbow
#

wall 1 and wall 2 are the boundaries on the top and bottom respectively

ebon dune
#

I guess depending on the sequencing of things it is conceivable ball is not "ready" when opponent is.

slate elbow
#

oh so i assume i just put the export variable inside of a ready function

ebon dune
#

Well, personally I would avoid referencing other sibling objects like that.

sullen path
#

actually you'd want to do @onready @export

ebon dune
#

It makes your opponent scene less reusable.

sullen path
#

but yeah you should generally avoid doing that at all

slate elbow
#

hmmm

sullen path
#

it looks like you're only referencing the ball to get the opponent direction, so in that case i'd suggest just getting the "World" node to tell the objects that info instead

ebon dune
#

Ideally any scene should be usable in isolation. So if you "Run Current Scene" in your "opponent", it won't "start" because it expects a sibling node.

slate elbow
#

i forgot to turn ping off

sullen path
#

you could add a new variable to the player/opponent scripts called opponent_direction, and then in the World script's _ready() function you can just assign the variables for those nodes

#

or in _physics_process() if you need it updated every frame

#

looking at the script again, since move_direction is only being assigned once, you can just set that variable directly in World's ready function

ebon dune
#

It does seem odd to me it is only ever set once. The code as-is looks like it will just go off in one direction for eternity and off the screen.