Jump to content

Hey guys really need some Help with item casting


dungdodger

Recommended Posts

Hello iv been working on this Item for days its been a lot of fun but i cant seem to get these last two parts working right. Please Help im trying to learn    

Heres my Issue... 

  i made a item (Copy and Pasted stuff till it started working lol ) that Teleports the target away and spawns a pigman  but it will only do this one time  after that it only teleports stuff.. But if i unequip it then requip it  it does Both effects again.

I have NO IDEA why it does this iv just been trying Learn what i can.

 

 Also i cant seem to figure out how to make it uncastable if im under 50 Sanity 

Here is some of what i have tried 

 

local function canteleport(inst, caster, target)
if target then
return target.components.locomotor ~= nil
end
--if caster.components.sanity then
--return (caster.components.sanity.current >= 50)
--end

return true
end

  I added the --out  bc it keeps giving and error about not know the value or sanity or caster i have tried to put it many places or make its own

local function.   but nothing works...  and i have this as a follow up to stop the cast or at least thought it would like it did when i had it as a Weapon or a Book.

 

local function oncast_pig(inst, target, caster, owner)


--Check sanity
if not canteleport(caster) then
if caster.components.talker then
caster.components.talker:Say ("Could_it_be_MY_Hat_is_FREE")
return true
end
end

 

this does not crash the game anymore iv tried  owner target inst   all kinda stuff to make them work but its been days working on this trying to LEARN.. some stuff ahah but still  NOTHING

  Any Help would be much appreciated

 

I can attach some files if need be just let me know what i need to do please  THANKS

 

local assets=
{
Asset("ANIM", "anim/pighat.zip"),
Asset("ANIM", "anim/swap_pighat.zip"),
Asset("ATLAS", "images/inventoryimages/pighat.xml"),
Asset("IMAGE", "images/inventoryimages/pighat.tex"),
}
prefabs = {


}
local function doeffects(inst, pos)
SpawnPrefab("statue_transition").Transform:SetPosition(pos:Get())
SpawnPrefab("statue_transition_2").Transform:SetPosition(pos:Get())
end

local function getrandomposition(inst)
local ground = GetWorld()
local centers = {}
for i,node in ipairs(ground.topology.nodes) do
local tile = GetWorld().Map:GetTileAtPoint(node.x, 0, node.y)
if tile and tile ~= GROUND.IMPASSABLE then
table.insert(centers, {x = node.x, z = node.y})
end
end
if #centers > 0 then
local pos = centers[math.random(#centers)]
return Point(pos.x, 0, pos.z)
else
return GetPlayer():GetPosition()
end
end

local function canteleport(inst, caster, target)
if target then
return target.components.locomotor ~= nil
end
--if caster.components.sanity then
--return (caster.components.sanity.current >= 50)
--end

return true
end



local function teleport_thread(inst, caster, teletarget, loctarget)

local ground = GetWorld()

local t_loc = nil
if loctarget then
t_loc = loctarget:GetPosition()
else
t_loc = getrandomposition()
end

local teleportee = teletarget
local pt = teleportee:GetPosition()
if teleportee.components.locomotor then
teleportee.components.locomotor:StopMoving()
end

if teleportee.components.health then
teleportee.components.health:SetInvincible(true)
end


teleportee:Hide()


if caster.components.sanity then
caster.components.sanity:DoDelta(-50)
end


if teleportee == GetPlayer() then
TheFrontEnd:Fade(false, 1)
Sleep(1)
end


teleportee.Transform:SetPosition(t_loc.x, 0, t_loc.z)

if teleportee == GetPlayer() then
TheCamera:Snap()
TheFrontEnd:DoFadeIn(1)
Sleep(0)
end
if loctarget and loctarget.onteleto then loctarget.onteleto(loctarget) end

teleportee:Show()


inst.Transform:SetPosition(t_loc.x+0.5, 0, t_loc.z+0.5)

if teleportee.components.health then
teleportee.components.health:SetInvincible(false)
end

if teleportee == GetPlayer() then
teleportee.sg:GoToState("idle")
end
end

local function teleport_func(inst, target, caster)
local mindistance = 1
local caster = inst.components.inventoryitem.owner
local tar = target or caster
local pt = tar:GetPosition()
local ents = TheSim:FindEntities(pt.x,pt.y,pt.z, 9000, {"telebase"})



if #ents <= 0 then
--There's no bases, active or inactive. Teleport randomly.
inst.task = inst:StartThread(function() teleport_thread(inst, caster, tar) end)
return
end

local targets = {}
for k,v in pairs(ents) do
local v_pt = v:GetPosition()
if distsq(pt, v_pt) >= mindistance * mindistance then
table.insert(targets, {base = v, distance = distsq(pt, v_pt)})
end
end

table.sort(targets, function(a,b) return (a.distance) < (b.distance) end)
for i = 1, #targets do
local teletarget = targets
if teletarget.base and teletarget.base.canteleto(teletarget.base) then

inst.task = inst:StartThread(function() teleport_thread(inst, caster, tar, teletarget.base) end)

return oncast_pig(inst, target)
end
end


inst.task = inst:StartThread(function() teleport_thread(inst, caster, tar) end)
end
----- ATTACK------
--local function canattack(inst, target)
-- if inst.components.sanity then
-- return (inst.components.sanity.current >= 50)
--target:HasTag("pigman") )
--end
-- return true
--end


--local function canteleport(inst)

--end

local function oncast_pig(inst, target, caster, owner)

------------------SANITY---------------------------



--Check sanity
if not canteleport(caster) then
if caster.components.talker then
caster.components.talker:Say ("Could_it_be_MY_Hat_is_FREE")
return true
end
end
--Check reagent
--if not owner.components.inventory:Has("petals", 5) then
--if owner.components.talker then
--owner.components.talker:Say("The_Flowers_have_Escaped")
--return true
--end
--end

--caster.components.inventory:ConsumeByName("petals", 5)

----------------SANITY-------------------------------



--Ok you had everything. Make the image.

local theta = math.random() * 2 * PI
local pt = inst:GetPosition()
local radius = math.random(3, 6)
local offset = FindWalkableOffset(pt, theta, radius, 12, true)
if offset then
local image = SpawnPrefab("pigman")
local pos = pt + offset
image.Transform:SetPosition(pos:Get())
doeffects(inst, pos)

inst.SoundEmitter:PlaySound("dontstarve/maxwell/shadowmax_appear")

end
return teleport_func(inst, target, caster)
end


local function OnEquip(inst, owner)
owner.AnimState:OverrideSymbol("swap_object", "swap_pighat", "swap_pighat")
owner.AnimState:Show("ARM_carry")
owner.AnimState:Hide("ARM_normal")
end

local function OnUnequip(inst, owner)
owner.AnimState:Hide("ARM_carry")
owner.AnimState:Show("ARM_normal")
end


local function fn()
local inst = CreateEntity()
local trans = inst.entity:AddTransform()
local anim = inst.entity:AddAnimState()
local sound = inst.entity:AddSoundEmitter()
MakeInventoryPhysics(inst)

anim:SetBank("pighat")
anim:SetBuild("pighat")
anim:PlayAnimation("idle")

local minimap = inst.entity:AddMiniMapEntity()
minimap:SetIcon("pighat.tex")

inst:AddComponent("inspectable")
inst:AddComponent("inventoryitem")
inst.components.inventoryitem.imagename = "pighat"
inst.components.inventoryitem.atlasname = "images/inventoryimages/pighat.xml"
--
inst:AddComponent("equippable")
inst.components.equippable:SetOnEquip( OnEquip )
inst.components.equippable:SetOnUnequip( OnUnequip )
--Shovel with your hat-------------------------
inst:AddComponent("tool")
inst.components.tool:SetAction(ACTIONS.DIG)
inst:AddInherentAction(ACTIONS.DIG)
--
inst:AddComponent("finiteuses")
--inst.components.finiteuses:SetMaxUses(0)
--inst.components.finiteuses:SetUses(0)
--inst.components.finiteuses:SetConsumption(ACTIONS.DIG, 0)

local
base_finiteuses_OnUsedAsItem =
inst.components.finiteuses.OnUsedAsItem
inst.components.finiteuses.OnUsedAsItem =

function( self, action )
base_finiteuses_OnUsedAsItem( self, action ) -- drain sanity on dig
if action == ACTIONS.DIG then
if self.inst.components.inventoryitem
and self.inst.components.inventoryitem.owner then
local owner = self.inst.components.inventoryitem.owner
owner.components.sanity:DoDelta( -10)
end
end
end

---------------------------------------------------
inst:AddComponent("spellcaster")
--inst.components.spellcaster:SetSpellTestFn(san_chk)
inst.components.spellcaster.canuseontargets = true
inst.components.spellcaster:SetSpellTestFn(canteleport)
inst.components.spellcaster:SetSpellFn(oncast_pig)
--inst:AddTag("dull")
--inst:AddComponent("weapon")
--inst.components.weapon:SetDamage(2)
--inst.components.weapon:SetOnAttack(onattack)
return inst
end
return Prefab("common/inventory/pighat", fn, assets)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...