Hey, I need help. Every time I leave my world, the custom NPC I made disappears. Here is my code:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria.ID;
using Terraria;
using Terraria.ModLoader;
namespace Essentials.NPCs
{
public class Miner : ModNPC
{
public override bool Autoload(ref string name, ref string texture, ref string[] altTextures)
{
name = "Miner";
return mod.Properties.Autoload;
}
public override void SetDefaults()
{
npc.name = "David"; //the name displayed when hovering over the npc ingame.
npc.townNPC = true; //This defines if the npc is a town Npc or not
npc.friendly = true; //this defines if the npc can hur you or not()
npc.width = 18; //the npc sprite width
npc.height = 46; //the npc sprite height
npc.aiStyle = 7; //this is the npc ai style, 7 is Pasive Ai
npc.defense = 25; //the npc defense
npc.lifeMax = 250;// the npc life
npc.HitSound = SoundID.NPCHit1; //the npc sound when is hit
npc.DeathSound = SoundID.NPCDeath1; //the npc sound when he dies
npc.knockBackResist = 0.5f; //the npc knockback resistance
Main.npcFrameCount[npc.type] = 26; //this defines how many frames the npc sprite sheet has
NPCID.Sets.ExtraFramesCount[npc.type] = 10;
NPCID.Sets.AttackFrameCount[npc.type] = 5;
NPCID.Sets.DangerDetectRange[npc.type] = 150; //this defines the npc danger detect range
NPCID.Sets.AttackType[npc.type] = 1; //this is the attack type, 0 (throwing), 1 (shooting), or 2 (magic). 3 (melee)
NPCID.Sets.AttackTime[npc.type] = 30; //this defines the npc attack speed
NPCID.Sets.AttackAverageChance[npc.type] = 10;//this defines the npc atack chance
NPCID.Sets.HatOffsetY[npc.type] = 4; //this defines the party hat position
animationType = NPCID.Guide; //this copy the guide animation
}
public override bool CanTownNPCSpawn(int numTownNPCs, int money)
{
if (NPC.downedBoss1)
{
return true;
}
return false;
}
public override bool CheckConditions(int left, int right, int top, int bottom)
{
return true;
}
public override string TownNPCName()
{
switch (WorldGen.genRand.Next(6))
{
case 0:
return "Jack";
case 1:
return "James";
case 2:
return "David";
case 3:
return "Mike";
case 4:
return "Josh";
case 5:
return "Christopher";
default:
return "John";
}
}
public override void SetChatButtons(ref string button, ref string button2)
{
button = "Shop";
}
public override void OnChatButtonClicked(bool firstButton, ref bool openShop)
{
if (firstButton)
{
openShop = true;
}
}
public override void SetupShop(Chest shop, ref int nextSlot)
{
shop.item[nextSlot].SetDefaults(ItemID.CopperOre);
nextSlot++;
shop.item[nextSlot].SetDefaults(ItemID.TinOre);
nextSlot++;
shop.item[nextSlot].SetDefaults(ItemID.IronOre);
nextSlot++;
shop.item[nextSlot].SetDefaults(ItemID.LeadOre);
nextSlot++;
shop.item[nextSlot].SetDefaults(ItemID.SilverOre);
nextSlot++;
shop.item[nextSlot].SetDefaults(ItemID.TungstenOre);
nextSlot++;
shop.item[nextSlot].SetDefaults(ItemID.GoldOre);
nextSlot++;
shop.item[nextSlot].SetDefaults(ItemID.PlatinumOre);
nextSlot++;
if (NPC.downedBoss2)
{
shop.item[nextSlot].SetDefaults(ItemID.CrimtaneOre);
nextSlot++;
shop.item[nextSlot].SetDefaults(ItemID.DemoniteOre);
nextSlot++;
shop.item[nextSlot].SetDefaults(ItemID.Meteorite);
nextSlot++;
}
if (Main.hardMode)
{
shop.item[nextSlot].SetDefaults(ItemID.CobaltOre);
nextSlot++;
shop.item[nextSlot].SetDefaults(ItemID.PalladiumOre);
nextSlot++;
shop.item[nextSlot].SetDefaults(ItemID.MythrilOre);
nextSlot++;
shop.item[nextSlot].SetDefaults(ItemID.OrichalcumOre);
nextSlot++;
shop.item[nextSlot].SetDefaults(ItemID.AdamantiteOre);
nextSlot++;
shop.item[nextSlot].SetDefaults(ItemID.TitaniumOre);
nextSlot++;
}
if (NPC.downedPlantBoss)
{
shop.item[nextSlot].SetDefaults(ItemID.ChlorophyteOre);
nextSlot++;
}
if (NPC.downedMoonlord)
{
shop.item[nextSlot].SetDefaults(ItemID.LunarOre);
nextSlot++;
}
}
public override string GetChat() //Allows you to give this town NPC a chat message when a player talks to it.
{
switch (Main.rand.Next(4)) //this are the messages when you talk to the npc
{
case 0:
return "Hello, would you like to purchase any minerals?";
case 1:
return "I sell various minerals, each with a different cost.";
case 2:
return "This is a very nice house, I have one like it at home.";
case 3:
return "How has your day been going?";
default:
return "I come from the island of A Fresh Start.";
}
}
public override void DrawTownAttackGun(ref float scale, ref int item, ref int closeness)
{
scale = 1f;
item = (ItemID.WoodenBow);
closeness = 20;
}
public override void TownNPCAttackProj(ref int projType, ref int attackDelay)
{
projType = ProjectileID.WoodenArrowFriendly;
attackDelay = 1;
}
public override void TownNPCAttackProjSpeed(ref float multiplier, ref float gravityCorrection, ref float randomOffset)
{
multiplier = 7f;
randomOffset = 4f;
}
}
}