Why doesn't my gun script shoot?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By HarryCourt
:warning: Old Version Published before Godot 3 was released.

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
:bust_in_silhouette: Reply From: bruteforce

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)

I fixed the errors, but to no avial.

HarryCourt | 2017-11-05 04:26

:bust_in_silhouette: Reply From: rustyStriker

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

:bust_in_silhouette: Reply From: gonzo191

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)
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+

kidscancode | 2017-11-06 01:57

:bust_in_silhouette: Reply From: eons

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