tModLoader Accessing a boolean value from a ModWeapon class to use in a ModProjectile class

POCKETS

Terrarian
Alright, so this one has me stumped.

I'm attempting to use a boolean value from my ModWeapon's class to create an event in my ModProjectile's class.

Here's the breakdown -

My ModWeapon has dual use functionality (normal and right-click). This weapon has a damage increase over time. Once the damage hits a certain threshold, a new projectile spawns, and the boolean variable is set to false. This projectile is simply an indicator letting the player know that the right-click functionality is available for use. Once I use the right-click function, the bool is then set to true.

Alright, so here's what I need to do -

I need to access that bool value to perform an event inside my projectile's class (the same indicator projectile). Basically, I need to projectile.Kill() the indicator projectile once the bool value has been set to true.

How can I do this without having to resort to using a public static bool variable (which does happen to work), which I've been told is a no-no in C#?

OR, perhaps there's an easier way to handle this?
 
Well, the static approach might seem to work, but in multiplayer it will most likely break. Which highlights a big issue with statics. There is always only one of them. (By definition.) This makes them pretty inflexible, you can't just decide you need to make another instance if your thing.

In this particular case, I'd recommend you do a bit of dependency inversion. Instead of having your projectile depend on your weapon, by having it ask the weapon about it's state, you should have your weapon manage the lifetime of the projectile. Keep track of the projectile your weapon creates, and tell it die when it needs to.
 
Well, the static approach might seem to work, but in multiplayer it will most likely break. Which highlights a big issue with statics. There is always only one of them. (By definition.) This makes them pretty inflexible, you can't just decide you need to make another instance if your thing.

In this particular case, I'd recommend you do a bit of dependency inversion. Instead of having your projectile depend on your weapon, by having it ask the weapon about it's state, you should have your weapon manage the lifetime of the projectile. Keep track of the projectile your weapon creates, and tell it die when it needs to.


That sounds like a decent approach. I managed to find a solution (perhaps this is a similar approach?) that was given to me by the tModLoader Discord, although I'm not quite sure how well this will work in multiplayer.

Someone suggested using this check - if (player.ownedProjectileCounts[ModContent.ProjectileType<ModProjectile>()] > 0).

This does solve my issue. If this projectile spawns (which means there's more than 0 of them), it kills the "indicator" projectile. Well, not by itself, but by using projectile.Kill() as the code for the if statement. However, I don't know if this works in multiplayer. It does mention player.ownedProjectileCounts, so perhaps this keeps track of how many projectiles of ProjectileType that that particular player has? Not sure on that one.

Thank you for the advice, though. I think the approach you mentioned, and this one, sound as if they're very similar in nature :)
 
Back
Top Bottom