Is there a way to make an NPC spawn from a projectile? And how do I make an NPC that attacks enemies but not the player?

SouzouOokami

Terrarian
I'm trying to make an NPC spawn from a projectile, but it says that there is an error with the underlined part, is there any alternatives that do work?

The code:

using Terraria.Audio;
using Terraria.ID;
using Terraria;
using Terraria.ModLoader;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using OfTheNight.Puppet;

namespace OfTheNight.Projectiles.PuppetThrow
{
public class WoodDollProj : ModProjectile
{
public override void SetStaticDefaults()
{
DisplayName.SetDefault("Wood Doll");
}
public override void SetDefaults()
{

Projectile.damage = 0;
Projectile.width = 20;
Projectile.height = 40;
Projectile.friendly = true;
Projectile.hostile = false;
Projectile.timeLeft = 15;
Projectile.aiStyle = ProjAIStyleID.Arrow;
Projectile.penetrate = 1;
Projectile.ignoreWater = false;
Projectile.tileCollide = true;
Projectile.extraUpdates = 0;
Projectile.knockBack = 0;

}
public override void AI()
{
// Check if the projectile has hit something
if (Projectile.timeLeft == 1)
{
// Calculate the spawn location
Vector2 spawnPosition = Projectile.Center;
// Spawn the NPC
int npcType = ModContent.NPCType<WoodenDoll>(); // Replace with your NPC type
Projectile.NewNPC((int)spawnPosition.X, (int)spawnPosition.Y, npcType, 0);
}
}

}
}

__________________________________________________________________________________________________________________________________________________________________

also, for the npc that goes with this, how would I make a simple melee npc that attacks enemies but ignores the player?

Here is my current code for that:

using Microsoft.Xna.Framework;
using System;
using System.IO;
using System.Collections.Generic;
using Terraria;
using Terraria.GameContent.Bestiary;
using Terraria.GameContent.Biomes;
using Terraria.GameContent.UI;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using Terraria.ModLoader.Utilities;
using OfTheNight.Global;
namespace OfTheNight.Puppet
{
//The ExampleZombieThief is essentially the same as a regular Zombie, but it steals ExampleItems and keep them until it is killed, being saved with the world if it has enough of them.
public class WoodenDoll : ModNPC
{
public override void SetStaticDefaults()
{
Main.npcFrameCount[Type] = 3; // Number of frames for animation
}
public override void SetDefaults() {
NPC.width = 20;
NPC.height = 40;
NPC.damage = 3;
NPC.defense = 5;
NPC.lifeMax = 6;
NPC.HitSound = SoundID.NPCHit1;
NPC.DeathSound = SoundID.NPCDeath2;
NPC.value = 200f;
NPC.knockBackResist = 0.75f;
NPC.aiStyle = 3;
NPC.friendly = true;
AnimationType = NPCID.Zombie;

}
public override List<string> SetNPCNameList()
{
return new List<string>()
{
"Woodie",
"Hollo",
"Anim",
"Vudu",
"Barko",
"Fletcher",
"Nestor"
};
}
public override void AI()
{
NPC.TargetClosest(true);

}

}
}
 
Projectile.NewProjectile() is how you'd make an NPC spawn a projectile.

If you want your NPC to ignore the player, you'd have to use a completely custom AI style rather than AI style 3, have it detect other NPCs, and shoot friendly projectiles at them.
 
Back
Top Bottom