• 2D
  • How might one change the group of a hitbox (Area2D)?

So I worked out a way to have my player's sword be a sprite attached to him so that I can plug in different textures for different upgrades he may get so I don't need to have a ton of sprites the character drags around. I've set up my hitbox collisions to run their code "if area.is_in_group()" and the have different groups set up like "enemy", "bullet", "sword" and so on. An example of what I'm trying to do is this: when the player picks up a sword his hitbox is in group sword, when an enemy collides with that hitbox it checks if it's in group "sword" it will do 2 hp damage. but then he'll pick up a lightsaber and I'd like to change the hitbox's group to "saber" and then the enemy will have an if condition checking for that group and modify it's damage variable accordingly

Maybe this is a wonky way to achieve my goal here. But it will work if I can get the group to change. Perhaps when I learn more about using global variables I'll develop a better method

Thank you so much for that bit of knowledge. I actually tried that and it didn't accomplish what I was trying to do, though. It seems that while add_to_group did change the group of my hit box, for some reason the collision with the enemy produced the same result of the previous group it was in. Some other flaw in my code I am not seeing I guess!

Since posting this question I learned about using collision events to call a function in the body collided with and to assign variables as arguments that can be used in the other node's functions, thus eliminating the need for this wonky is_in_group business! So now I just have a damage level variable that changes with power ups and can easily be plugged in to my collisions as an argument.

As I'm writing this reply, it just dawned on me that perhaps the reason for this is that while my weapon's hitbox was added to the new group, it also remained in the old group since I didn't try remove_from_group yet. Thus the collision with the enemy would default to the first group since the engine would return that value first during the collision. Light bulb moment! Even though I already worked out a better way to accomplish my goal here, I may experiment with this purely for learning and understanding purposes!

Just got done experimenting and that did indeed solve the problem! Now when the player picks up a power up, his sword will be removed from group "sword level 1" and added to "sword level 2" and deal out the appropriate amount of hp damage as set up in the enemy's script. Now that I got it working, I'm not sure which method I like better- handling upgrade status through groups, or plugging in variable arguments into functions. Which method do you feel is better?

Whatever gets the job done in the simplest fashion without being too expensive or prone to bugs.