Texture Pack Need help with custom texture modding on certrain conditions. (SOLVED WITH SOLUTION IF YOU NEED IT)

CheeseyFries482

Official Terrarian
Hi, I'm wanting to make a mod that changes a vanilla town npcs texture based on what name they have, but I can't figure out how to do this. I figured it would be in global NPCs but I don't know the code. I also tried looking on example mod but couldn't find anything about changing vanilla textures. Any help would be appreciated. Thanks! :)
(Edit)
I figured out the correct if statement, just need the code for the texture change.
 
Last edited:
Hi, I'm wanting to make a mod that changes a vanilla town npcs texture based on what name they have, but I can't figure out how to do this. I figured it would be in global NPCs but I don't know the code. I also tried looking on example mod but couldn't find anything about changing vanilla textures. Any help would be appreciated. Thanks! :)
(Edit)
I figured out the correct if statement, just need the code for the texture change.
Ok, now I've got a different problem. I'm not getting any code errors but when I launch a world it says that it can't load the file. The code is below but I'm 99% sure it is fine.

if (npc.type == NPCID.Guide && npc.GivenName == "Andrew")
Mod.Assets.Request<Texture2D>("UniqueTownNPCs/Global/GlobalNPCs/Guide/Andrew");
Mod.Assets.Request<Texture2D>("UniqueTownNPCs/Global/GlobalNPCs/Guide/Andrew_Head");
 
Found out the Asset error but still not working. I'm not getting any kind of code errors so I'm not sure whats wrong. Please help.
 
Ok, I've got it figured out. If your wanting to do this put in this code

public override bool PreDraw(NPC npc, SpriteBatch spriteBatch, Vector2 screenPos, Color drawColor)
{
if (npc.type == NPCID.Guide && npc.GivenName == "Andrew") //Where it says "Andrew" is the name, change this to whatever the name of your NPC is. (You have to keep the " " around it)
{
Texture2D texture = Mod.Assets.Request<Texture2D>("Assets/NPCs/Guide/Andrew").Value; //"Assets/NPCs/Guide/Andrew" is the path of your image/spritesheet.
Vector2 position = npc.position - Main.screenPosition + new Vector2(-12, -6); //-12, -6 is the offset that the npc will be drawn. For me I had to use those numbers, but depending on the width and height of the frame you may need different ones.

// Get the current frame of the NPC's animation
Rectangle sourceRectangle = npc.frame;

// Modify the source rectangle to only draw a specific portion of the texture, this again depends on how big each frame is. Be sure to keep extra room in the source rectangle for things like attacking or when the npc bobs up in the animation.
sourceRectangle.X += 2;
sourceRectangle.Y += 8;
sourceRectangle.Width -= 0;
sourceRectangle.Height -= 10;

SpriteEffects spriteEffects = npc.direction == 1 ? SpriteEffects.FlipHorizontally : SpriteEffects.None;

// Draw the NPC using the modified source rectangle
spriteBatch.Draw(texture, position, sourceRectangle, drawColor, 0f, Vector2.Zero, 1f, spriteEffects, 0f);


return false;
}
return true;
}
 
Back
Top Bottom