-
Notifications
You must be signed in to change notification settings - Fork 1
Old Documentation Lua
utils.lua mostly contains spawning and replacement-related utility functions. These are the available conditions:
- Spawn/replacement requirements:
AfterAnyMechBoss()AfterGolem()AfterMoonLord()AfterPlantera()CustomIDContains(txt)NameContains()
- Spawn/replacement conditions:
DuringBloodMoon()DuringDay()DuringEclipse()DuringFrostMoon()DuringHardmode()DuringNight()DuringPumpkinMoon()DuringRain()DuringSlimeRain()
- Spawn level:
AtCavernLevel(player)AtSkyLevel(player)AtSurfaceLevel(player)AtUndergroundLevel(player)AtUnderworldLevel(player)
- Spawn areas:
AroundRegion(x, y, name)InAndAroundRegion(player, x, y, name)InBeach(player)InCorruption(player)InCrimson(player)InDesert(player)InDungeon(player)InForest(player)InGlowshroom(player)InHallow(player)InIce(player)InRegion(player, name)InJungle(player)InMeteor(player)
- Replacement conditions:
IsType(baseNpc, type)
Create a file with the following text, and name it example.lua:
require "npcs/utils"
OnAiUpdate = function(npc)
npc:Periodically(0, function()
npc:TargetClosestPlayer()
local target = npc.Target
if target ~= nil and npc:HasLineOfSight(target.TPlayer.position) then
npc:ShootProjectileAt(target.TPlayer.position, 100, 5, 5, 0)
end
end, 60)
return false
end
OnCheckReplace = function(baseNpc)
return IsType(baseNpc, 1) and 0.10 or 0.0
end
OnCheckSpawn = function(player, x, y)
return (DuringDay() and InRegion(x, y, "test")) and 1 or 0
end
OnCollision = function(npc, player)
end
OnKilled = function(npc)
if RandomDouble() < 0.35 then
SpawnCustomNpc("example", npc.Position + Vector2(-10, 0))
SpawnCustomNpc("example", npc.Position + Vector2(0, -10))
SpawnCustomNpc("example", npc.Position + Vector2(10, 0))
Broadcast("Muahahaha, I still live!", Color.Crimson)
else
Broadcast("No! I failed to multiply!", Color.Crimson)
end
end
OnSpawn = function(npc)
Broadcast("Muahahaha, I have spawned!", Color.Crimson)
return true
end
OnStrike = function(npc, player, damage, knockback, critical)
return false
endNow, set LuaPath above to example.lua, ShouldSpawn to true, and ShouldReplace to true. We'll go over each of the things in the file.
when using it for projectiles for replace 'npc' with 'projectile'
You can set OnAiUpdate to a function with parameters npc that returns a boolean value. Whenever the server performs an AI update on the custom NPC, this function will be called. If the function returns true, then the normal AI code will not run.
In our example Lua file, the custom NPC will periodically shoot a laser at the closest player.
-
npc: TheCustomNpcthat is being updated.
If ShouldReplace is true, you can set OnCheckReplace to a function with parameters baseNpc that returns a double from 0.0 to 1.0. Whenever the server spawns a normal NPC, it will check all custom NPCs to see if they can replace the normal NPC.
In our example Lua file, the custom NPC will check if the normal NPC is a blue slime. If so, then it will assign a 0.10 chance of turning the blue slime into the custom NPC.
-
baseNpc: AnNPCobject that represents the normal NPC that may be replaced.
If ShouldSpawn is true, you can set OnCheckSpawn to a function with parameters player, x, y that returns a positive integer. Whenever the server goes to spawn NPCs at a certain location and player, it will check all custom NPCs to see if they can spawn. NOTE: Custom NPC spawning is separate from normal NPC spawning.
In our example Lua file, the custom NPC will check if it is daytime and in the TShock region test. If so, then it will assign a weight of 1 to spawning. If this custom NPC is the only NPC allowed to spawn, then it will be guaranteed to spawn if the spawn cycle allows it.
-
player: TheTSPlayerthat is currently spawning NPCs. -
x: The tile X coordinate. -
y: The tile Y coordinate.
You can set OnCollision to a function with parameters npc, player. Whenever a player touches the custom NPC, this function will be called. You can, for example, the debuff the player.
-
npc: TheCustomNpcthat collided with the player. -
player: TheTSPlayerthat collided with the NPC.
You can set OnTileCollision to a function with parameters npc. Whenever a npc touches any valid tile, liquid or background tile, this function will be called. You can, for example, the debuff the player.
-
npc: TheCustomNpcthat collided with the tile.
You can set OnKilled to a function with parameters npc. Whenever the custom NPC is killed, this function will be called. You can, for example, have a custom death message.
-
npc: TheCustomNpcthat was killed.
You can set OnSpawn to a function with parameters npc. Whenever the custom NPC is spawned, this function will be called. You can, for example, have a custom summon message.
-
npc: TheCustomNpcthat was spawned.
You can set OnStrike to a function with parameters npc, player, damage, knockback, critical that returns a boolean. Whenever a player strikes the custom NPC, this function will be called with the relevant arguments. If the function returns true, then the strike will be ignored. You can, for example, push a player away in SSC mode.
-
npc: TheCustomNpcthat is being struck. -
player: TheTSPlayerthat struck the NPC. -
damage: The damage. -
knockback: The knockback. -
critical: A boolean indicating whether the hit was critical.
The CustomNpc object contains most of the properties and functions you'll need when creating a custom NPC. We'll go over them here.
Gets or sets the HP of the custom NPC. This can be useful if you want to heal the custom NPC on colliding with a player:
OnCollision = function(npc, player)
npc.Hp = npc.Hp + 200
npc.Hp = math.min(npc.Hp, 1000)
npc.SendNetUpdate = true
CreateCombatText("200", Color.LimeGreen, npc.Position)
endGets the custom NPC index. This is only necessary for advanced manipulation of the NPC.
Gets the wrapped normal NPC. This is only necessary for advanced manipulation of the NPC.
Gets or sets the position of the NPC. Normally, you wouldn't want to set the position directly, but would instead teleport the NPC with Teleport.
Gets or sets a value indicating whether to send a network update for the NPC. This is necessary if one of its values changed programatically and players haven't been informed of the change.
Gets or sets the target of the NPC. This is useful for firing projectiles at a player.
OnAiUpdate = function(npc)
npc:Periodically(0, function()
npc:TargetClosestPlayer()
local target = npc.Target
local position = target.TPlayer.position + target.TPlayer.velocity * 5
if target ~= nil and npc:HasLineOfSight(position) then
npc:ShootProjectileAt(position, 100, 5, 5, 0)
npc:ShootProjectileAt(position + Vector2(25, 0), 100, 5, 5, 0)
npc:ShootProjectileAt(position + Vector2(0, 25), 100, 5, 5, 0)
npc:ShootProjectileAt(position + Vector2(-25, 0), 100, 5, 5, 0)
npc:ShootProjectileAt(position + Vector2(0, -25), 100, 5, 5, 0)
end
end, 60)
return false
endGets or sets the velocity of the NPC. If modifying the velocity, make sure to send a network update with SendNetUpdate.
OnCollision = function(npc, player)
npc.Velocity = Vector2(0, -100)
npc.SendNetUpdate = true
player:SendInfoMessage("You can't touch me!")
endBuffs nearby players within the specified tile radius.
OnKilled = function(npc)
npc:BuffNearbyPlayers(164, 60)
npc:MessageNearbyPlayers("As a parting gift... enjoy this.", Color.Crimson)
endTransforms the custom NPC into the custom NPC with the specified name.
OnAiUpdate = function(npc)
if npc.Hp < 200 then
npc:CustomTransform("example2")
npc:MessageNearbyPlayers("And now... you will see my true form...", Color.Crimson)
end
return false
endApplies the callback to each nearby player within the tile radius.
Gets the stored variable attached to the custom NPC. Useful for custom logic.
Determines whether the custom NPC has line of sight with the specified position. Useful for only firing at players if the NPC can see them.
Determines whether the stored variable attached to the custom NPC exists. Useful for custom logic.
Messages nearby players within the specified tile radius.
Ensures that the callback is run only once. NOTE: Every time you use this method, you must supply a different key!
OnAiUpdate = function(npc)
if npc.Hp < 200 then
npc:OnlyOnce(0, function()
npc:MessageNearbyPlayers("Looks like I'm almost dead... here's a present for you.", Color.Crimson)
SpawnCustomNpc("example2", npc.Position - Vector2(100, 0))
SpawnCustomNpc("example2", npc.Position + Vector2(100, 0))
end)
end
return false
endPeriodically calls the callback with the specified period. NOTE: Every time you use this method, you must supply a different key!
OnAiUpdate = function(npc)
npc:Periodically(0, function()
npc:TargetClosestPlayer()
local target = npc.Target
local position = target.TPlayer.position
if target ~= nil and npc:HasLineOfSight(position) then
npc:ShootProjectileAt(position, 100, 5, 5, 0)
end
end, 60) -- Every 60 ticks
return false
endSets the variable with the specified name.
Shoots a projectile at the specified position. The speeds will be calculated automatically.
Targets the closest player.
Teleports the custom NPC to the specified position. This will display a teleportation effect, and is therefore better than just setting the position.
Also available are the following global functions:
Broadcasts a message to the entire server.
Creates a combat text with the custom string centered on the position. This is good for nice display of miscellaneous things.
Finds all custom NPCs with the specified internal name.
Finds all normal NPCs with the specified name.
Gets the region with the specified name.
Gets the tile at the specified location.
Returns a random double.
Returns a random integer in the range.
Spawns a custom NPC with the specified internal name, and return the CustomNpc object. This is good for keeping track of spawned NPCs.
Spawns a normal NPC with the specified name or type, and return the NPC object. This is good for keeping track of spawned NPCs.
Kills the Tile at the 'x,y' location
removes tiles within radius of 'x,y'
sets tile to 'type' at 'x,y' location
sets tiles within radius of 'x,y' to 'type'
damages all players within radius of 'x,y' Falloff is linear from 'x,y' to radius. Set to 0 to turn off damage falloff.
TileX(worldX) TileY(worldY)
WorldX(tileX) WorldY(tileY)