0 votes

The script is supposed to spawn a node once "Q" or "E" is pressed, but it doesn't do that. What is the problem?

There are two scripts to this, one that might work better than the other.

Gunscript V1:

extends Position2D

export (PackedScene) var spawnScene
onready var spawnReference = load(spawnScene.get_path())
onready var sprite = get_tree().get_root().get_node("World/Player/CollisionShape2D/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)
    #self.add_collision_exception_with(PhysicsBody)


func _fixed_process(delta):
        var spawnInstance = spawnReference.instance()
        var shootleft = Input.is_action_pressed("ui_shootleft")
        var shootright = Input.is_action_pressed("ui_shootright")

        if shootleft and not pressed:
            get_parent().add_child(spawnInstance)
            sprite.set_flip_h(true)
            ammo -= 1


        elif shootleft and not pressed:
            get_parent().add_child(spawnInstance)
            sprite.set_flip_h(false)
            ammo -= 1

        pressed = shootleft && shootright

Gunscript V2:

extends Position2D

export (PackedScene) var spawnScene
onready var spawnReference = load(spawnScene.get_path())
onready var sprite = get_tree().get_root().get_node("World/Player/CollisionShape2D/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)
    #self.add_collision_exception_with(PhysicsBody)


#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:
            get_parent().add_child(spawnInstance)
            sprite.set_flip_h(true)
            ammo -= 1

        elif key == KEY_E and pressed and not echo:
            get_parent().add_child(spawnInstance)
            sprite.set_flip_h(false)
            ammo -= 1
asked Nov 4, 2017 in Engine by HarryCourt (162 points)

4 Answers

0 votes

I have 2 comments:

1.) Gunscript V1

if shootleft and not pressed:
    (...)
elif shootleft and not pressed:
    (...)

The two conditions are exactly the same.

2.) Gunscript V2
As I see you frogot the set_process_input(true)

answered Nov 4, 2017 by bruteforce (646 points)

I fixed the errors, but to no avial.

0 votes

have you tried to reposition the bullet before spawning it? by what i see it will place itself at 0,0 making it fly outside of the screen of in the very top

EDIT: you can reposition it before adding it as a child by using the instance_node.set_pos() if you are on godot 2.1.4 or instance_node.position = ... if you are on godot 3

answered Nov 5, 2017 by rustyStriker (1,073 points)
+1 vote

You didn't set the bullet's position when it was created so there's a possibility that it's spawning at 0, 0 (top left) and not from the gun.

You need to do something like:

bullet = BulletScene.instance()
bullet.set_pos(Vector2(gun.get_pos().x, gun.get_pos().y)) // v2.1.4
bullet.position = Vector2(gun.position.x, gun.position.y)) // v3+
root_node.add_child(bullet)
answered Nov 5, 2017 by gonzo191 (118 points)
bullet.set_pos(Vector2(gun.get_pos().x, gun.get_pos().y)) // v2.1.4
bullet.position = Vector2(gun.position.x, gun.position.y)) // v3+

This is a giant waste. get_pos() returns a Vector2. set_pos() takes a Vector2. No need to muck about breaking it into x an y only to construct another Vector2:

bullet.set_pos(gun.get_pos()) // v2.1.4
bullet.position = gun.position // v3+
0 votes

Have you defined the action ui_shootleft somewhere?

pressed is "left AND right", to be true, both need to be true, also is setted at the end of the code meaning it will be true if in the previous frame both actions were pressed.

Read line by line how the values change, follow the flow of the logic.


For the second one, you have never enabled input processing (set_process_input) and you can use is_action on events so there is no need for type and scancode checks if you have the option of actions try to use that instead of scancodes and things like that.


Also get used to debugging, the remote inspector will show a lot of details of the live tree and you can easily check if your nodes are correctly placed on the scene

answered Nov 10, 2017 by eons (7,764 points)
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.