tAPI tAPI Community Resources - Development assistance for developers, by developers.

Hello TFC ! First of all I want to thanks the TAPI's devs for they great work ^^

I came here with a little question about it. With the imminent arrival of the 1.3 update I'm worrying about the mod installer. Should I uninstall it ? Will this one encounters problems with the update ?

Thanks in advance for responding me.
 
Hello TFC ! First of all I want to thanks the TAPI's devs for they great work ^^

I came here with a little question about it. With the imminent arrival of the 1.3 update I'm worrying about the mod installer. Should I uninstall it ? Will this one encounters problems with the update ?

Thanks in advance for responding me.

You shouldn't have anything to worry about. GameLauncher keeps the tAPI client and vanilla client effectively separate. I don't foresee any major problems. tAPI, of course, will probably not be updated for 1.3 immediately after its release.

Anyway, got another question for the thread. I'm looking for a way to grant a buff/debuff to the player when they are near a tile, a la heart lantern. Unfortunately, there isn't any code I can find as a reference for something like this - the closest I can find is the Butterfly Post in GRealm, which creates its own biome and grants its buff that way, but that seems awfully roundabout for my purposes. Is there a way to check if a given tile type is within (x, y) of the player?
 
Anyway, got another question for the thread. I'm looking for a way to grant a buff/debuff to the player when they are near a tile, a la heart lantern. Unfortunately, there isn't any code I can find as a reference for something like this - the closest I can find is the Butterfly Post in GRealm, which creates its own biome and grants its buff that way, but that seems awfully roundabout for my purposes. Is there a way to check if a given tile type is within (x, y) of the player?
These is a way to check all the tiles within a given range of the player. Find the players tile position, (player.position / 16) then subtract half the distance you want to check within, then use nested for loops to search through each tile, checking to see if it's your tile. If the range your checking is small, say within 3 tiles of the player that'd be about 72 tiles to check each time. (3 left of player 2 on player 3 right of player for a width of 8, 3 above player 3 on player 3 below player for a height of 9) Which may seem like a lot, but probably wouldn't create any noticeable frame loss. If you did that for within a distance of 20 tiles, that'd be 1806 tiles to check, which could start to become problematic.

If the tile should have an effect as soon as it's onscreen, like the Heart Lantern, that's actually extremely easy to do. Every game update the game counts every tile while applying lighting effects and stores the number in an array. This array is used to determine which zone you're in and also whether the Camp Fire and Heart Lantern buffs should be applied or not. Stone has a Tile ID of 1. If I wanted to know how many Stone tiles were onscreen I could use - WorldGen.tileCount[1]
To know how many of your custom tiles are onscreen use - WorldGen.tileCount[TileDef.byName["InternalName:TileName"]]

To sum up, try this...
Code:
using System;
using TAPI;
using Terraria;


namespace InternalName {

    public class MPlayer : ModPlayer {
        
        public override void MidUpdate() {
            
            if (WorldGen.tileCount[TileDef.byName["InternalName:TileName"]] > 0)
                player.AddBuff("InternalName:BuffName", 1800); // The number is the duration of the buff in ticks. ~60 ticks per second

        }

    }

}
 
These is a way to check all the tiles within a given range of the player. Find the players tile position, (player.position / 16) then subtract half the distance you want to check within, then use nested for loops to search through each tile, checking to see if it's your tile. If the range your checking is small, say within 3 tiles of the player that'd be about 72 tiles to check each time. (3 left of player 2 on player 3 right of player for a width of 8, 3 above player 3 on player 3 below player for a height of 9) Which may seem like a lot, but probably wouldn't create any noticeable frame loss. If you did that for within a distance of 20 tiles, that'd be 1806 tiles to check, which could start to become problematic.

If the tile should have an effect as soon as it's onscreen, like the Heart Lantern, that's actually extremely easy to do. Every game update the game counts every tile while applying lighting effects and stores the number in an array. This array is used to determine which zone you're in and also whether the Camp Fire and Heart Lantern buffs should be applied or not. Stone has a Tile ID of 1. If I wanted to know how many Stone tiles were onscreen I could use - WorldGen.tileCount[1]
To know how many of your custom tiles are onscreen use - WorldGen.tileCount[TileDef.byName["InternalName:TileName"]]

To sum up, try this...
Code:
using System;
using TAPI;
using Terraria;


namespace InternalName {

    public class MPlayer : ModPlayer {
      
        public override void MidUpdate() {
          
            if (WorldGen.tileCount[TileDef.byName["InternalName:TileName"]] > 0)
                player.AddBuff("InternalName:BuffName", 1800); // The number is the duration of the buff in ticks. ~60 ticks per second

        }

    }

}

Fantastic! I can use this for all sorts of fun stuff. In addition to player-affecting tiles, I'm starting to work on one that will make nearby enemies deal more damage and regenerate life - it'll generate like an ore - which should prove interesting. I should have all I need to code my tiles, now. Looks like this function easily enables me to try other stuff, too, since I can get the number of tiles on screen: "the more of a dangerous tile, the greater the effects" is a possibility if I work outside of the buff code and write in MPlayer instead. I'm pretty pumped.

Edit: How can I get all the NPCs on the screen/within (x, y) of the player? I forgot that ModNPC doesn't let you refer to "NPC" as all NPCs. Also, I'll want to be using public override void AI() instead of MidUpdate() for ModNPC, yes?
 
Last edited:
How do you check if an accessory is equipped?
 
How do you check if an accessory is equipped?

Code:
                    for (int i = 3; i < 8; i++)
                    {
                        if (owner.armor[i].type == ItemDef.byName["InternalName:ItemName"].type)
                        {
                            //whatever
                        }
}

This checks all the accessory slots for the item.
 
So i'm (still) trying to make an accessory that adds health drain, and right now i've got this

Code:
 public class MProjectile : ModProjectile
    {
        public void DealtNPC(Player player, NPC npc, int hitDir, int dmgDealt, float knockback, bool crit)
        {
            for (int i = 3; i < 8; i++)
            {
                if (player.armor[i].type == ItemDef.byName["Rektmod:VampiricEmblem"].type)
                {
                    player.statLife = Math.Min(player.statLifeMax2, player.statLife + Math.Max(1, (int)((float)dmgDealt / 8f)));
                }
            }
        }
    }
The mod builds just fine but nothing happens when i shoot stuff.
What could be the problem?
 
So i'm (still) trying to make an accessory that adds health drain, and right now i've got this

Code:
 public class MProjectile : ModProjectile
    {
        public void DealtNPC(Player player, NPC npc, int hitDir, int dmgDealt, float knockback, bool crit)
        {
            for (int i = 3; i < 8; i++)
            {
                if (player.armor[i].type == ItemDef.byName["Rektmod:VampiricEmblem"].type)
                {
                    player.statLife = Math.Min(player.statLifeMax2, player.statLife + Math.Max(1, (int)((float)dmgDealt / 8f)));
                }
            }
        }
    }
The mod builds just fine but nothing happens when i shoot stuff.
What could be the problem?

I know that the accessory check works, since I've used it to add debuffs through ModProjectile, but that particular line of lifesteal code doesn't seem to work in this context, even if it does work for melee weapons. Try a simpler version to see if we can diagnose the exact issue. It could be the math function, for whatever reason - player.statLife += (int)(dmgDealt / 8) might work.
 
Last edited:
Does anyone know the c# code for detecting a player, as I need it for a multi table crafting station in my mod!
 
When starting up tAPI and it crashes without giving you any information you could use to fix it, does it save a log somewhere or something? Because there's too much stuff in my mod for me to figure out what's causing it.
 
When starting up tAPI and it crashes without giving you any information you could use to fix it, does it save a log somewhere or something? Because there's too much stuff in my mod for me to figure out what's causing it.

I'm not sure where you could find a log, but r15 crashes where r14a provided the error and sent you back to the main screen. This means that it could be anything that doesn't stop the mod from compiling, but prevents it from being loaded. I'd check to make sure every object has an image file (with the same name, ofc) - that's usually what I'd forgotten when this happens to me.
 
I figured the game didn't like the new NPC I made. Suppose I wanted my NPC to spawn like the bound NPCs (Randomly in the wild if you already have the NPC)
What would the CS file of a bound NPC look like that becomes released when you right click on it to talk to it?
 
I figured the game didn't like the new NPC I made. Suppose I wanted my NPC to spawn like the bound NPCs (Randomly in the wild if you already have the NPC)
What would the CS file of a bound NPC look like that becomes released when you right click on it to talk to it?

Since the vanilla bound NPCs are separate from their unbound counterparts, my money is on killing the bound NPC and spawning the unbound one at its location within the same hook. I haven't worked with NPCs much at all, though, so I don't know if there's a hook for when you attempt to talk to them.
 
Is it just me or does everytime you set "townNPC" to true for a town NPC it causes the game to crash every single time you try to start it up?
 
Since the vanilla bound NPCs are separate from their unbound counterparts, my money is on killing the bound NPC and spawning the unbound one at its location within the same hook. I haven't worked with NPCs much at all, though, so I don't know if there's a hook for when you attempt to talk to them.
I Havnt Ever Made An NPC Yet I Know How To Do Ore Gen :P
 
Is it just me or does everytime you set "townNPC" to true for a town NPC it causes the game to crash every single time you try to start it up?
Did you make sure to give it a "NPCName_Head.png" file, and an "occupation" property in the .json? (The head file is what's used for the town icons and stuff.)
 
I'm having a small problem with my mod, here's my code any help would be appreciated
upload_2015-6-8_18-42-58.png

here's the error i get:
upload_2015-6-8_18-44-16.png
 
I know that the accessory check works, since I've used it to add debuffs through ModProjectile, but that particular line of lifesteal code doesn't seem to work in this context, even if it does work for melee weapons. Try a simpler version to see if we can diagnose the exact issue. It could be the math function, for whatever reason - player.statLife += (int)(dmgDealt / 8) might work.
Still ain't working
 
Back
Top Bottom