-1

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 !

NJB's Codings
  • 17
  • 1
  • 9
  • If you are spawning new bullets, I would set things up a little different. I'd try loading the bullet script into the player one. Then you can "instance" the bullet script to make bullets. After the bullet is created in player, set the info on the bullet, then add the bullet as children of the parent with get_parent().add_child(bullet). If this sounds like the route you want to go. I can write up a more full answer. – Cory-G Jun 20 '20 at 23:00

1 Answers1

1

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.