NJB's Codings
NJB's Codings

Reputation: 27

how can i access functions from another scripts in godot?

I'm trying to make a top down shooter in Godot but I'm stuck ! I've made two scenes first bullet and second player. And we can change the rotation and direction of the bullet accordingly to the direction of the player. But I can't access the direction variable from player because we can't extend / inherit more than one class / script.

#Bullet script

extends KinematicBody2D

var direction = "down"

func physics_process(delta):
    #Something which can access the direction variable from player script.
#Player script
extends KinematicBody2D

var direction = "down"

func _physics_process(delta):
    if Input.is_action_pressed("ui_left"):
        direction = "left"
    # And so on...

Pleas help me !

Upvotes: 0

Views: 13169

Answers (1)

rafantastico
rafantastico

Reputation: 61

You can use two methods that I think about:

  1. If the bullet is in your scene you can get it with get_node() link

  2. For me the more elegant solution: use signals. In fact, in the previous link I sent you an example of get_node() to use signals.

If you let me comment that I would recommend using a RigidBody2D for the bullet instead of a KinematicBody2D. Here a link about this.

Upvotes: 1

Related Questions