#Changing Projectile Collision Layer&Mask for the Player/Enemy

14 messages · Page 1 of 1 (latest)

cold shadow
#

Greetings! I´ve got a projectile scene which I preload&instantiate to shoot projectiles, which works fine for my player, but not for my enemies, since the projectiles are set up to only hit enemies. My Question now is, can I change the Layer&Mask of my Projectile scene depending on who uses it?

#

My guess would be instantiating it and then calling a function that changes it but it feels inefficient

full latch
#

I don't know how inefficient it would be; it's a one-time call for each projectile, so it's probably not too bad. The functions in question are set_collision_layer_value ( int layer_number, bool value ) and set_collision_mask_value ( int layer_number, bool value )

cold shadow
#

Thankuuu, I created a function though I can´t call it, any idea why that is?

#

That´s how I instanciate

#

but I can´t call any methods from it

#

This is what I get if I try to call bullet._setLayerMask(2,2) in line 17 of my first screenshot

full latch
#

in Base 'Nil' errors mean that the node wasn't found. So wherever you are assigning the hitbox variable, that code hasn't run yet, or failed to find the node.

cold shadow
#

Then I´m not sure why o how, since I can manipulate bullet´s position without any problems below those lines

full latch
#

Assuming hitbox is a child node, it's probable that it hasn't created its children yet. What I'd recommend would be setting the layers you want as variables on it, then having the projectile do the actual setting in its _ready() function.

cold shadow
#

Yup hitbox is a child note!
I did so though _ready() seems to be called the moment I call instantiate(), which means it´s too late to change the vars that will be used to assign the layer/mask

#

:s

full latch
#

No, _ready() is called when the node, and all its children, have finished entering the scene tree.

#

So when you .instantiate() a node, _ready() will not be called until you add the node as a child of some other node, thus adding it to the scene tree.