tModLoader v2024.10
A mod to make and play Terraria mods
ModProjectile Class Reference

This class serves as a place for you to place all your properties and hooks for each projectile.
To use it, simply create a new class deriving from this one. Implementations will be registered automatically. The Basic Projectile Guideteaches the basics of making a modded projectile. More...

Inherits ModType< Projectile, ModProjectile >, and ILocalizedModType.

Public Member Functions

virtual void AI ()
 Allows you to determine how this projectile behaves. This will only be called if PreAI returns true.
The Basic Projectile Guideteaches the basics of writing a custom AI, such as timers, gravity, rotation, etc.
 
virtual void AutoStaticDefaults ()
 Automatically sets certain static defaults. Override this if you do not want the properties to be set for you.
 
virtual ? bool CanCutTiles ()
 Return true or false to specify if the projectile can cut tiles like vines, pots, and Queen Bee larva. Return null for vanilla decision.
 
virtual ? bool CanDamage ()
 Whether or not this projectile is capable of killing tiles (such as grass) and damaging NPCs/players. Return false to prevent it from doing any sort of damage. Return true if you want the projectile to do damage regardless of the default blacklist. Return null to let the projectile follow vanilla can-damage-anything rules. This is what happens by default.
 
virtual ? bool CanHitNPC (NPC target)
 Allows you to determine whether this projectile can hit the given NPC. Return true to allow hitting the target, return false to block this projectile from hitting the target, and return null to use the vanilla code for whether the target can be hit. Returns null by default. More...
 
virtual bool CanHitPlayer (Player target)
 Allows you to determine whether this hostile projectile can hit the given player. Return false to block this projectile from hitting the target. Returns true by default. More...
 
virtual bool CanHitPvp (Player target)
 Allows you to determine whether this projectile can hit the given opponent player. Return false to block this projectile from hitting the target. Returns true by default. More...
 
virtual ? bool CanUseGrapple (Player player)
 This code is called whenever the player uses a grappling hook that shoots this type of projectile. Use it to change what kind of hook is fired (for example, the Dual Hook does this), to kill old hook projectiles, etc.
 
virtual ? bool Colliding (Rectangle projHitbox, Rectangle targetHitbox)
 Allows you to use custom collision detection between this projectile and a player or NPC that this projectile can damage. Useful for things like diagonal lasers, projectiles that leave a trail behind them, etc. More...
 
virtual void CutTiles ()
 Code ran when the projectile cuts tiles. Only runs if CanCutTiles() returns true. Useful when programming lasers and such.
 
virtual void DrawBehind (int index, List< int > behindNPCsAndTiles, List< int > behindNPCs, List< int > behindProjectiles, List< int > overPlayers, List< int > overWiresUI)
 When used in conjunction with Projectile.hide = true (Projectile.hide), allows you to specify that this projectile should be drawn behind certain elements. Add the index to one and only one of the lists. For example, the Nebula Arcanum projectile draws behind NPCs and tiles.
 
virtual void EmitEnchantmentVisualsAt (Vector2 boxPosition, int boxWidth, int boxHeight)
 Called when Projectile.EmitEnchantmentVisualsAt(Vector2, int, int) is called. Typically used to spawn visual effects (Dust) indicating weapon enchantments such as flasks, frost armor, or magma stone effects. This is similar to how items spawn visual effects in CombinedHooks.MeleeEffects(Player, Item, Rectangle), but for projectiles instead. A typical weapon enchantment would likely include similar code in both to support weapon enchantment visuals for both items and projectiles. Projectiles can use Projectile.noEnchantments to indicate that a projectile should not be considered for enchantment visuals, so check that field if relevant.
 
virtual ? Color GetAlpha (Color lightColor)
 Allows you to determine the color and transparency in which this projectile is drawn. Return null to use the default color (normally light and buff color). Returns null by default.
 
virtual ? bool GrappleCanLatchOnTo (Player player, int x, int y)
 Whether or not the grappling hook can latch onto the given position in tile coordinates.
This position may be air or an actuated tile!
Return true to make it latch, false to prevent it, or null to apply vanilla conditions. Returns null by default.
 
virtual void GrapplePullSpeed (Player player, ref float speed)
 The speed at which the grapple pulls the player after hitting something. Defaults to 11, but the Bat Hook uses 16.
 
virtual float GrappleRange ()
 How far away this grappling hook can travel away from its player before it retracts.
 
virtual void GrappleRetreatSpeed (Player player, ref float speed)
 The speed at which the grapple retreats back to the player after not hitting anything. Defaults to 11, but vanilla hooks go up to 24.
 
virtual void GrappleTargetPoint (Player player, ref float grappleX, ref float grappleY)
 The location that the grappling hook pulls the player to. Defaults to the center of the hook projectile.
 
virtual void Kill (int timeLeft)
 
virtual bool MinionContactDamage ()
 Whether or not this minion can damage NPCs by touching them. Returns false by default. Note that this will only be used if this projectile is considered a pet (Main.projPet).
 
virtual void ModifyDamageHitbox (ref Rectangle hitbox)
 Allows you to change the hitbox used by this projectile for damaging players and NPCs. More...
 
virtual void ModifyFishingLine (ref Vector2 lineOriginOffset, ref Color lineColor)
 
virtual void ModifyHitNPC (NPC target, ref NPC.HitModifiers modifiers)
 Allows you to modify the damage, knockback, etc., that this projectile does to an NPC. This method is only called for the owner of the projectile, meaning that in multi-player, projectiles owned by a player call this method on that client, and projectiles owned by the server such as enemy projectiles call this method on the server. More...
 
virtual void ModifyHitPlayer (Player target, ref Player.HurtModifiers modifiers)
 Allows you to modify the damage, etc., that this hostile projectile does to a player. More...
 
virtual void NumGrappleHooks (Player player, ref int numHooks)
 How many of this type of grappling hook the given player can latch onto blocks before the hooks start disappearing. Change the numHooks parameter to determine this; by default it will be 3.
 
virtual void OnHitNPC (NPC target, NPC.HitInfo hit, int damageDone)
 Allows you to create special effects when this projectile hits an NPC (for example, inflicting debuffs). This method is only called for the owner of the projectile, meaning that in multi-player, projectiles owned by a player call this method on that client, and projectiles owned by the server such as enemy projectiles call this method on the server. More...
 
virtual void OnHitPlayer (Player target, Player.HurtInfo info)
 Allows you to create special effects when this hostile projectile hits a player.
Only runs on the local client in multiplayer. More...
 
virtual void OnKill (int timeLeft)
 Allows you to control what happens when this projectile is killed (for example, creating dust or making sounds). Also useful for creating retrievable ammo. Called on all clients and the server in multiplayer, so be sure to use if (Projectile.owner == Main.myPlayer) if you are spawning retrievable ammo. (As seen in ExampleJavelinProjectile)
 
virtual void OnSpawn (IEntitySource source)
 Gets called when your projectiles spawns in world.
Called on the client or server spawning the projectile via Projectile.NewProjectile.

 
virtual bool OnTileCollide (Vector2 oldVelocity)
 Allows you to determine what happens when this projectile collides with a tile. OldVelocity is the velocity before tile collision. The velocity that takes tile collision into account can be found with Projectile.velocity. Return true to allow the vanilla tile collision code to take place (which normally kills the projectile). Returns true by default. More...
 
virtual void PostAI ()
 Allows you to determine how this projectile behaves. This will be called regardless of what PreAI returns.
 
virtual void PostDraw (Color lightColor)
 Allows you to draw things in front of this projectile. Use the Main.EntitySpriteDraw method for drawing. This method is called even if PreDraw returns false. More...
 
virtual bool PreAI ()
 Allows you to determine how this projectile behaves. Return false to stop the vanilla AI and the AI hook from being run. Returns true by default. More...
 
virtual bool PreDraw (ref Color lightColor)
 Allows you to draw things behind this projectile, or to modify the way it is drawn. Use the Main.EntitySpriteDraw method for drawing. Return false to stop the vanilla projectile drawing code (useful if you're manually drawing the projectile). Returns true by default. More...
 
virtual bool PreDrawExtras ()
 Allows you to draw things behind this projectile. Use the Main.EntitySpriteDraw method for drawing. Returns false to stop the game from drawing extras textures related to the projectile (for example, the chains for grappling hooks), useful if you're manually drawing the extras. Returns true by default.
 
virtual bool PreKill (int timeLeft)
 Allows you to determine whether the vanilla code for Kill and the Kill hook will be called. Return false to stop them from being called. Returns true by default. Note that this does not stop the projectile from dying.
 
virtual void PrepareBombToBlow ()
 Used to adjust projectile properties immediately before the projectile becomes an explosion. This is called on projectiles using the ProjAIStyleID.Explosive aiStyle or projectiles that are contained in the ProjectileID.Sets.Explosive set. By defaults tileCollide is set to false and alpha is set to 255. Use this to adjust damage, knockBack, and the projectile hitbox (Projectile.Resize). Called during Projectile.PrepareBombToBlow, which is called by default during Projectile.AI_016 and during Projectile.Kill for the aforementioned projectiles.
 
virtual void ReceiveExtraAI (BinaryReader reader)
 Use this to receive information that was sent in SendExtraAI.
Called whenever MessageID.SyncProjectile is successfully received.
Can be called on both server and client, depending on who owns the projectile. More...
 
virtual void SendExtraAI (BinaryWriter writer)
 If you are storing AI information outside of the Projectile.ai array, use this to send that AI information between clients and servers, which will be handled in ReceiveExtraAI.
Called whenever MessageID.SyncProjectile is successfully sent, for example on projectile creation, or whenever Projectile.netUpdate is set to true in the update loop for that tick.
Can be called on both server and client, depending on who owns the projectile. More...
 
virtual void SetDefaults ()
 Allows you to set all your projectile's properties, such as width, damage, aiStyle, penetrate, etc.
 
sealed override void SetupContent ()
 If you make a new ModType, seal this override, and call SetStaticDefaults in it. More...
 
virtual bool ShouldUpdatePosition ()
 Whether or not this projectile should update its position based on factors such as its velocity, whether it is in liquid, etc. Return false to make its velocity have no effect on its position. Returns true by default.
 
virtual bool TileCollideStyle (ref int width, ref int height, ref bool fallThrough, ref Vector2 hitboxCenterFrac)
 Allows you to determine how this projectile interacts with tiles. Return false if you completely override or cancel this projectile's tile collision behavior. Returns true by default. More...
 
virtual void UseGrapple (Player player, ref int type)
 This code is called whenever the player uses a grappling hook that shoots this type of projectile. Use it to change what kind of hook is fired (for example, the Dual Hook does this), to kill old hook projectiles, etc.
 
- Public Member Functions inherited from ModType< Projectile, ModProjectile >
virtual TModType Clone (TEntity newEntity)
 Create a copy of this instanced global. Called when an entity is cloned. More...
 
virtual bool IsLoadingEnabled (Mod mod)
 Allows you to stop Mod.AddContent from actually adding this content. Useful for items that can be disabled by a config. More...
 
virtual void Load ()
 Allows you to perform one-time loading tasks. Beware that mod content has not finished loading here, things like ModContent lookup tables or ID Sets are not fully populated. More...
 
virtual TModType NewInstance (TEntity entity)
 Create a new instance of this ModType for a specific entity More...
 
string PrettyPrintName ()
 
virtual void SetStaticDefaults ()
 Allows you to modify the properties after initial loading has completed.
 
virtual void SetupContent ()
 If you make a new ModType, seal this override, and call SetStaticDefaults in it. More...
 
virtual void Unload ()
 Allows you to safely unload things you added in Load. More...
 
virtual bool IsLoadingEnabled (Mod mod)
 Whether or not this type should be loaded when it's told to. Returning false disables Mod.AddContent from actually loading this type. More...
 
abstract void Load (Mod mod)
 Called when loading the type. More...
 
abstract void Unload ()
 Called during unloading when needed. More...
 

Protected Member Functions

override Projectile CreateTemplateEntity ()
 
sealed override void Register ()
 If you make a new ModType, seal this override. More...
 
- Protected Member Functions inherited from ModType< Projectile, ModProjectile >
abstract TEntity CreateTemplateEntity ()
 
virtual void InitTemplateInstance ()
 Create dummy objects for instanced mod-types
 
override void InitTemplateInstance ()
 Create dummy objects for instanced mod-types
 
abstract void Register ()
 If you make a new ModType, seal this override. More...
 
virtual void ValidateType ()
 Check for the correct overrides of different hook methods and fields and properties
 

Properties

int AIType [get, set]
 Determines which type of vanilla projectile this ModProjectile will copy the behavior (AI) of. Leave as 0 to not copy any behavior. Defaults to 0.
 
int CooldownSlot = -1 [get, set]
 Determines which ImmunityCooldownID to use when this projectile damages a player. Defaults to -1 (ImmunityCooldownID.General).
 
virtual LocalizedText DisplayName [get]
 The translations for the display name of this projectile.
 
bool DrawHeldProjInFrontOfHeldItemAndArms [get, set]
 If this projectile is held by the player, determines whether it is drawn in front of or behind the player's arms. Defaults to false.
 
int DrawOffsetX [get, set]
 How far to the right of its position this projectile should be drawn. Defaults to 0.
 
float DrawOriginOffsetX [get, set]
 The horizontal origin offset from the projectile's center when it is drawn.
 
int DrawOriginOffsetY [get, set]
 The vertical origin offset from the projectile's center when it is drawn. The origin is essentially the point of rotation. This field will also determine the vertical drawing offset of the projectile.
 
virtual string GlowTexture [get]
 The file name of this projectile's glow texture file in the mod loader's file space. If it does not exist it is ignored.
 
virtual string LocalizationCategory [get]
 The category used by this modded content for use in localization keys. Localization keys follow the pattern of "Mods.{ModName}.{Category}.{ContentName}.{DataName}". The Localization wiki pageexplains how custom ModType classes can utilize this. More...
 
Projectile Projectile [get]
 The projectile object that this ModProjectile controls.
 
virtual string Texture [get]
 The file name of this type's texture file in the mod loader's file space.
The resulting Asset<Texture2D> can be retrieved using TextureAssets.Projectile indexed by Type if needed.
You can use a vanilla texture by returning $"Terraria/Images/Projectile_{ProjectileID.ProjectileNameHere}"

 
int Type [get]
 Shorthand for Projectile.type;
 
- Properties inherited from ModType< Projectile, ModProjectile >
virtual bool CloneNewInstances [get]
 Whether to create new instances of this mod type via Clone(TEntity) or via the default constructor Defaults to false (default constructor).
 
TEntity Entity [get, set]
 
string FullName [get]
 The internal name of this, including the mod it is from. More...
 
virtual bool IsCloneable [get]
 Whether or not this type is cloneable. Cloning is supported if
all reference typed fields in each sub-class which doesn't override Clone are marked with [CloneByReference]
 
Mod Mod [get, set]
 The mod this belongs to. More...
 
virtual string Name [get]
 The internal name of this. More...
 
- Properties inherited from IModType
string FullName [get]
 => $"{Mod.Name}/{Name}" More...
 
Mod Mod [get]
 The mod this belongs to. More...
 
string Name [get]
 The internal name of this instance. More...
 
- Properties inherited from ILocalizedModType
abstract string LocalizationCategory [get]
 The category used by this modded content for use in localization keys. Localization keys follow the pattern of "Mods.{ModName}.{Category}.{ContentName}.{DataName}". The Localization wiki pageexplains how custom ModType classes can utilize this. More...
 

Detailed Description

This class serves as a place for you to place all your properties and hooks for each projectile.
To use it, simply create a new class deriving from this one. Implementations will be registered automatically.

The Basic Projectile Guide

teaches the basics of making a modded projectile.

Member Function Documentation

◆ CanHitNPC()

virtual ? bool ModProjectile.CanHitNPC ( NPC  target)
virtual

Allows you to determine whether this projectile can hit the given NPC. Return true to allow hitting the target, return false to block this projectile from hitting the target, and return null to use the vanilla code for whether the target can be hit. Returns null by default.

Parameters
targetThe target.

◆ CanHitPlayer()

virtual bool ModProjectile.CanHitPlayer ( Player  target)
virtual

Allows you to determine whether this hostile projectile can hit the given player. Return false to block this projectile from hitting the target. Returns true by default.

Parameters
targetThe target.

◆ CanHitPvp()

virtual bool ModProjectile.CanHitPvp ( Player  target)
virtual

Allows you to determine whether this projectile can hit the given opponent player. Return false to block this projectile from hitting the target. Returns true by default.

Parameters
targetThe target

◆ Colliding()

virtual ? bool ModProjectile.Colliding ( Rectangle  projHitbox,
Rectangle  targetHitbox 
)
virtual

Allows you to use custom collision detection between this projectile and a player or NPC that this projectile can damage. Useful for things like diagonal lasers, projectiles that leave a trail behind them, etc.

Parameters
projHitboxThe hitbox of the projectile.
targetHitboxThe hitbox of the target.
Returns
Whether they collide or not.

◆ CreateTemplateEntity()

override Projectile ModProjectile.CreateTemplateEntity ( )
protectedvirtual

◆ ModifyDamageHitbox()

virtual void ModProjectile.ModifyDamageHitbox ( ref Rectangle  hitbox)
virtual

Allows you to change the hitbox used by this projectile for damaging players and NPCs.

Parameters
hitbox

◆ ModifyHitNPC()

virtual void ModProjectile.ModifyHitNPC ( NPC  target,
ref NPC.HitModifiers  modifiers 
)
virtual

Allows you to modify the damage, knockback, etc., that this projectile does to an NPC. This method is only called for the owner of the projectile, meaning that in multi-player, projectiles owned by a player call this method on that client, and projectiles owned by the server such as enemy projectiles call this method on the server.

Parameters
targetThe target.
modifiersThe modifiers for this strike.

◆ ModifyHitPlayer()

virtual void ModProjectile.ModifyHitPlayer ( Player  target,
ref Player.HurtModifiers  modifiers 
)
virtual

Allows you to modify the damage, etc., that this hostile projectile does to a player.

Parameters
targetThe target.
modifiers

◆ OnHitNPC()

virtual void ModProjectile.OnHitNPC ( NPC  target,
NPC.HitInfo  hit,
int  damageDone 
)
virtual

Allows you to create special effects when this projectile hits an NPC (for example, inflicting debuffs). This method is only called for the owner of the projectile, meaning that in multi-player, projectiles owned by a player call this method on that client, and projectiles owned by the server such as enemy projectiles call this method on the server.

Parameters
targetThe target.
hitThe damage.
damageDoneThe actual damage dealt to/taken by the NPC.

◆ OnHitPlayer()

virtual void ModProjectile.OnHitPlayer ( Player  target,
Player.HurtInfo  info 
)
virtual

Allows you to create special effects when this hostile projectile hits a player.
Only runs on the local client in multiplayer.

Parameters
targetThe target.
info

◆ OnTileCollide()

virtual bool ModProjectile.OnTileCollide ( Vector2  oldVelocity)
virtual

Allows you to determine what happens when this projectile collides with a tile. OldVelocity is the velocity before tile collision. The velocity that takes tile collision into account can be found with Projectile.velocity. Return true to allow the vanilla tile collision code to take place (which normally kills the projectile). Returns true by default.

Parameters
oldVelocityThe velocity of the projectile upon collision.

◆ PostDraw()

virtual void ModProjectile.PostDraw ( Color  lightColor)
virtual

Allows you to draw things in front of this projectile. Use the Main.EntitySpriteDraw method for drawing. This method is called even if PreDraw returns false.

Parameters
lightColorThe color of the light at the projectile's center, after being modified by vanilla and other mods.

◆ PreAI()

virtual bool ModProjectile.PreAI ( )
virtual

Allows you to determine how this projectile behaves. Return false to stop the vanilla AI and the AI hook from being run. Returns true by default.

Returns
Whether or not to stop other AI.

◆ PreDraw()

virtual bool ModProjectile.PreDraw ( ref Color  lightColor)
virtual

Allows you to draw things behind this projectile, or to modify the way it is drawn. Use the Main.EntitySpriteDraw method for drawing. Return false to stop the vanilla projectile drawing code (useful if you're manually drawing the projectile). Returns true by default.

Parameters
lightColorThe color of the light at the projectile's center.

◆ ReceiveExtraAI()

virtual void ModProjectile.ReceiveExtraAI ( BinaryReader  reader)
virtual

Use this to receive information that was sent in SendExtraAI.
Called whenever MessageID.SyncProjectile is successfully received.
Can be called on both server and client, depending on who owns the projectile.

Parameters
readerThe reader.

◆ Register()

sealed override void ModProjectile.Register ( )
protectedvirtual

If you make a new ModType, seal this override.

Implements ModType< Projectile, ModProjectile >.

◆ SendExtraAI()

virtual void ModProjectile.SendExtraAI ( BinaryWriter  writer)
virtual

If you are storing AI information outside of the Projectile.ai array, use this to send that AI information between clients and servers, which will be handled in ReceiveExtraAI.
Called whenever MessageID.SyncProjectile is successfully sent, for example on projectile creation, or whenever Projectile.netUpdate is set to true in the update loop for that tick.
Can be called on both server and client, depending on who owns the projectile.

Parameters
writerThe writer.

◆ SetupContent()

sealed override void ModProjectile.SetupContent ( )
virtual

If you make a new ModType, seal this override, and call SetStaticDefaults in it.

Reimplemented from ModType< Projectile, ModProjectile >.

◆ TileCollideStyle()

virtual bool ModProjectile.TileCollideStyle ( ref int  width,
ref int  height,
ref bool  fallThrough,
ref Vector2  hitboxCenterFrac 
)
virtual

Allows you to determine how this projectile interacts with tiles. Return false if you completely override or cancel this projectile's tile collision behavior. Returns true by default.

Parameters
widthThe width of the hitbox this projectile will use for tile collision. If vanilla doesn't modify it, defaults to Projectile.width.
heightThe height of the hitbox this projectile will use for tile collision. If vanilla doesn't modify it, defaults to Projectile.height.
fallThroughWhether or not this projectile falls through platforms and similar tiles.
hitboxCenterFracDetermines by how much the tile collision hitbox's position (top left corner) will be offset from this projectile's real center. If vanilla doesn't modify it, defaults to half the hitbox size (new Vector2(0.5f, 0.5f)).
Returns

Property Documentation

◆ LocalizationCategory

virtual string ModProjectile.LocalizationCategory
get

The category used by this modded content for use in localization keys. Localization keys follow the pattern of "Mods.{ModName}.{Category}.{ContentName}.{DataName}". The Localization wiki pageexplains how custom ModType classes can utilize this.

Implements ILocalizedModType.