0 votes

I'm stumped, how do I fix this? I cant find a solution anywhere else.

extends Position2D

export (PackedScene) var spawnScene
onready var spawnReference = load(spawnScene.get_path())
onready var sprite = get_tree().get_root().get_node("World/Player/PlayerSprite")
onready var sound = get_parent().get_node("World/Player/BulletSpawn/Bullet/Sound")#/Sound").get("Shoot")

#var bullet = self
var velocity = Vector2()
var speed = 1.25
var ammo = 12
var canShoot = true

export var BulletSpeed = 1500.0
export var Smoothness = 0.2

func _ready():
    set_fixed_process(true)
    set_process_input(true)


#func _fixed_process(delta):

func _input(event):
    var spawnInstance = spawnReference.instance()
    var direction = 3.0

    if event.type == InputEvent.KEY:
        var key = event.scancode
        var pressed = event.is_pressed()
        var echo = event.is_echo()

        if key == KEY_Q and pressed and not echo:
            print("Fired")
            spawnInstance.set_pos()
            get_parent().add_child(spawnInstance)
            sprite.set_flip_h(true)
            ammo -= 1

        elif key == KEY_E and pressed and not echo:
            print("Fired")
            spawnInstance.set_pos()
            get_parent().add_child(spawnInstance)
            sprite.set_flip_h(false)
            ammo -= 1
in Engine by (162 points)

1 Answer

+1 vote

You didn't say where exactly in that code the error is happening.

One thing I notice: you use spawnInstance.set_pos(), but set_pos() requires an argument - you have to set the position to something.

http://docs.godotengine.org/en/stable/classes/class_node2d.html#class-node2d-set-pos

Also, this:

func _input(event):
    var spawnInstance = spawnReference.instance()

Is not a good thing. You're spawning an instance of that scene every time the input function is called. But that happens on any input event, including mouse movement, etc.

In general, it's a better idea to use the input map and create input actions for any input you want to process. That way it's easier to change input keys, transition to gamepad or touch, etc.

by (18,443 points)

Whoops, its at line 36 and 43...

So it's what I answered: you have to supply a Vector2 for set_pos() to set the position of a node.

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 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 webmaster@godotengine.org with your username.

Categories