0 votes

Hello,

So I'm new to Godot (and programming) and I'm trying to make a very light football game prototype. Right now, what I'm trying to do is to wait for a given amount of time, then trigger a function to move the ball from point A to point B. Nothing fancy.

My issue is that the Tween animation of the ball doesn't work and nothing happens.

My code is structured that way:

I have a Clock Node which contains this script:

extends Node

var second = 0
var minute = 0
var time_mult = 250.0
var paused = false

var Match = load("res://Match.gd").new()

func _ready():
    set_process(true)

func _process(delta):
    if not paused:
        second += delta * time_mult
        minute = int(second / 60)
        get_node("MarginContainer/Label").set_text(str(minute))

    if minute == 3:
        Match.kick_off()

My Match.gd is supposed to centralize every action and call the appropriate functions. Here's what it looks like for now:

extends Control

var Ball = load("res://Ball.gd").new()

# Called when the node enters the scene tree for the first time.
func _ready():
    print("Match started!")


func kick_off():
    Ball.move_to()

And so the idea is that after 3 minutes, the kick_off is triggered, which triggers the Ball Node to move to a specific point.
Here is my Ball.gd:

extends Node2D

onready var TweenNode = get_node("Tween")

func _ready():
    var x = get_viewport().size.x/2
    var y = get_viewport().size.y/2

    self.position = Vector2(x,y)

func move_to():
    TweenNode.interpolate_property(self, "position", get_position(), Vector2(125,78), 1.0, Tween.TRANS_LINEAR, Tween.EASE_IN)
    TweenNode.start()
    print("Moved")

What I don't understand is that the console does print "Moved" so the function is indeed triggered correctly. But it gives me this error:

Invalid call. Nonexistent function 'interpolate_property' in base 'Nil'.

I figure this has something to do with me not understanding correctly the relations between the different scripts but I can't seem to find the answer.

Godot version 3.5
in Engine by (15 points)

1 Answer

0 votes
Best answer

Hey, two problems with your code, you have written var Ball = load("res://Ball.gd").new()

The problem here is the Ball.gd. when you call the .new() function on a script, what you get is an instance of the class but it doesn't add any of that instances nodes to the scene. So you need to first change that reference from Ball.gd to Ball.tscn. This will actually load the whole scene, not just the script.

Once that's done you'll need to add the Ball object to the Match scene tree using the add_child() function. So in your Match.gd inside your ready function you need to add add_child(Ball). This will basically add your Ball scene to the Match scene and when the Ball scene loads, so too will the Tween node. That should fix your problem.

by (70 points)
selected by

Thank you very much, that works!

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.