0 votes

Hello, Im new with Godot, and I`m having a problem with the Gdscript.
The problem is that Im trying to do a gun that shoots a bullet, and it aparently works except that when I instance the gun on my world scene it doesnt work as intended, and the bullet spawn in a different place inside the world(other than in the Position2D node that I made as spawn point for the bullet)
The gun works as intended in its own scene but when I instance the gun scene in the world scene it has the problems that I just said.
Im using Godot Engine 2.1.4.

Here is the code for the gun:

func spawn_bullet():
var bullet= bullet_scene.instance()
var pos= get_node("Pos_bullet").get_global_pos() 
bullet.set_pos(pos)
get_parent().add_child(bullet)

Thanks in anticipation, Im having so much trouble with this.

asked Nov 6, 2017 in Engine by ArAdev (54 points)

Did you used preload function to load bullet tscn

Something like this

onready var bullet_scene = preload(“res://bullet.tscn”)

Then make instance for that scene, then add it to scene tree then set pos normally

Thanks a lot xCode Soul, I realized that the problem was that the script of the gun was attached to the sprite of the gun, and the parent on the gun scene was a node2D ,and aparently it was causing problems, so I changed it and it now it works.

Please log in or register to answer this question.

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.