|
|
|
|
Reply From: |
kubecz3k |
In 3d there is set_as_toplevel(boolean)
but in 2D I havent found anything like that… However you can still get this effect with use of additional node in hierarchy. Each child of the most simple type of node: Node
will have position and rotation in global coords.
So example hierarchy might look like this:
*PlayerShip(Area2D)
*Collision (CollisionShape2D)
*Sprite (Sprite2D)
*(...)
*bulletsHolder (Node)
*Bullet1 (Your instanced scene)
*Bullet2 (Your instanced scene)
*Bullet3 (Your instanced scene)
And here is sample code that you could use:
#inside Player:
#bulletsHolder is the most simple type: 'Node'
var bulletsHolder = get_node("bulletsHolder");
var newBullet = BulletScn.instance();
bulletsHolder.add_child(newBullet);
newBullet.set_position(get_global_pos());
I tried that:
But now the shot doesn’t appear, am I doing something wrong?
Eldirish | 2016-04-20 20:18
Helo, from the screen I’m unsure what is going on… but basically you should have a node of the most basic type Node
(muzzle is type of Node2D
). And then when you add your bullets to this Node
you will need to set their start positions manually (since the bullets are now in global world coordinates), for example:
#inside Player:
#bulletsHolder is the most simple type: 'Node'
var bulletsHolder = get_node("bulletsHolder");
var newBullet = BulletScn.instance();
bulletsHolder.add_child(newBullet);
newBullet.set_position(get_position());
kubecz3k | 2016-04-21 09:12
Hi, I tried that, but I get an error: [Invalid call, none existent function “get_pos” in base “Node”],
get_position also gives me the same error, get_pos only appears in the autocomplete of a node2d but it still gives me the same error even tough set_pos gives me no errors. Is this a bug with the engine?
Eldirish | 2016-04-21 13:30
Node
is so basic that it don’t even have it’s position, so don’t ask it for position You can get positions of concrete individual bullets that are children of this node.
kubecz3k | 2016-04-21 15:36
Ah, thanks it works now
Since it doesnt have a position tough, they’re spawing on top and in the center of my dude
how would I go about changing where the muzzle is, or changing where in relation to the muzzle the bullets spawn and in what layer(?) they live in so to not draw over other sprites?
I’m still getting used to Godot’s way so sorry for all the questions.
Thanks for helping =)
Eldirish | 2016-04-21 17:27
I haven’t done much coding in 2d, but basically each Node2D have set_z
and get_z
methods, those allow you to set rendering order (what nodes will be above/below). When it comes to ‘muzzle’ I’m not native English speaker and I cant figure out what this means But basically it seems that you will need yet another one parent inside “bullets(Node
)” that will determine origin point for your bullets.
But this is not what is usually needed to handle bullets in Godot. You should just put your bullets inside bulletsHolderNodeNode
and reset their position to whatever you wish and let the bullets move on their own (bullet movement code inside bullet scene)
If you are asking on how to set initial position of bullet to some other node (for example Position2D
) then new snipped would look like this:
#inside Player:
#bulletsHolder is the most simple type: 'Node'
#bulletsStartPos is type of Position2D, in tree it's located at the top lvl of the player scene
var initBulletGlobalPos = get_node("bulletsStartPos").get_global_pos();
var bulletsHolder = get_node("bulletsHolder");
var newBullet = BulletScn.instance();
bulletsHolder.add_child(newBullet);
newBullet.set_position(initBulletGlobalPos);
take a note that there is get_global_pos()
function
kubecz3k | 2016-04-21 18:43
That’s it, all my current issues fixed, thanks alot!
A muzzle is the tip of a gun, where the bullets come out of. So in this case the muzzle is the spot where the bullets spawn and ideally the tip of ship/where the guns are.
Eldirish | 2016-04-21 18:55
I’m glad it’s working for you now!
kubecz3k | 2016-04-21 19:05