Jump to content

Prefab randomly spawns behind character...


Recommended Posts

Hello, I have a problem if someone can help me with it since I don't know what to do anymore..

So, basically my character can spawn some bats to protect him like a shield & this's how it normally looks which is how it's supposed to always look!

Spoiler

howshudb.gif

Though, sometimes it randomly spawns the bats behind my character like this until I exit the game :(...

Spoiler

glitch.gif

I don't want that because it's super ugly...and I have no idea why this is happening... If somebody could help I'd really make my day :D!!

 

Here's the code

Spoiler

modmain.lua


local function bat_veil(act)
if act.target.lord_mode == nil then return end

   local fx = SpawnPrefab("batveil")
   if fx ~= nil and act.target.bat_cd == nil then
      fx.entity:SetParent(act.target.entity)
      fx.Transform:SetPosition(0, .2, 0)
	  act.target.bat_cd = true
	  act.target:DoTaskInTime(5, function() act.target.bat_cd = nil end)
   end
end

batveil.lua (the prefab I'm spawning)


local assets = { Asset("ANIM", "anim/shadow_bishop.zip") }

local CAN_ATTACK =
{
    CHOP = false,
    DIG = false,
    HAMMER = false,
    MINE = false,
	ATTACK = true,
}

local ATTACK_TAGS = { "_combat" }

for k, v in pairs(CAN_ATTACK) do
    table.insert(ATTACK_TAGS, k.."_workable")
end

local NON_ATTACK_TAGS = { "INLIMBO", "playerghost" }

local function BatVeilAttack_1(inst)
local x, y, z = inst.Transform:GetWorldPosition()
local adam = TheSim:FindEntities(x, y, z, .75, nil, NON_ATTACK_TAGS, ATTACK_TAGS)
local ents = TheSim:FindEntities(x, y, z, 1.75, nil, NON_ATTACK_TAGS, ATTACK_TAGS)

   for i, v in ipairs(ents) do
      if v:IsValid() and not v:IsInLimbo() then
	     -- For natural creatures
         if v.components.combat ~= nil and not v.lord_mode == true and v.components.health and not v.components.health:IsDead() and not v:HasTag("shadow") and not v:HasTag("shadowminion") and not v:HasTag("shadowchesspiece") and not v:HasTag("ghost") then
            v.components.combat:GetAttacked(inst, 5, nil)
			
            for i, v in ipairs(adam) do
               if v:IsValid() and not v:IsInLimbo() then
                  if v.lord_mode == true and v.components.health and not v.components.health:IsDead() then
                     v.components.health:DoDelta(.5)
                  end
               end
            end
		 -- For supernatural creatures
		 elseif v.components.combat ~= nil and v.components.health and not v.components.health:IsDead() and v:HasTag("shadow") or v:HasTag("shadowminion") or v:HasTag("shadowchesspiece") or v:HasTag("ghost") then
		    v.components.combat:GetAttacked(inst, 10, nil)
			
            for i, v in ipairs(adam) do
               if v:IsValid() and not v:IsInLimbo() then
                  if v.lord_mode == true and v.components.sanity and not v.components.health:IsDead() then
                     v.components.sanity:DoDelta(1)
                  end
               end
            end

         end
      end
   end

end

local function BatVeilAttack_2(inst)
local x, y, z = inst.Transform:GetWorldPosition()
local adam = TheSim:FindEntities(x, y, z, .75, nil, NON_ATTACK_TAGS, ATTACK_TAGS)
local ents = TheSim:FindEntities(x, y, z, 1.75, nil, NON_ATTACK_TAGS, ATTACK_TAGS)

   for i, v in ipairs(ents) do
      if v:IsValid() and not v:IsInLimbo() then
         if v.components.combat ~= nil and not v.lord_mode == true and v.components.health and not v.components.health:IsDead() and not v:HasTag("shadow") and not v:HasTag("shadowminion") and not v:HasTag("shadowchesspiece") and not v:HasTag("ghost") then
            v.components.health:DoDelta(-5)
			
            for i, v in ipairs(adam) do
               if v:IsValid() and not v:IsInLimbo() then
                  if v.lord_mode == true and v.components.health and not v.components.health:IsDead() then
                     v.components.health:DoDelta(.5)
                  end
               end
            end
		 elseif v.components.combat ~= nil and v.components.health and not v.components.health:IsDead() and v:HasTag("shadow") or v:HasTag("shadowminion") or v:HasTag("shadowchesspiece") or v:HasTag("ghost") then
		    v.components.health:DoDelta(-10)
			
            for i, v in ipairs(adam) do
               if v:IsValid() and not v:IsInLimbo() then
                  if v.lord_mode == true and v.components.sanity and not v.components.health:IsDead() then
                     v.components.sanity:DoDelta(1)
                  end
               end
            end

         end
      end
   end

end

local function KillSound(inst)
    inst.SoundEmitter:KillSound("bats")
end

local function RemoveBatVeil(inst)
    inst.AnimState:PlayAnimation("atk_side_loop_pst")
    inst:DoTaskInTime(0.5, ErodeAway)	
end

local function SpawnBatVeil(inst)
    SpawnPrefab("statue_transition_2").Transform:SetPosition(inst.Transform:GetWorldPosition())
    inst.SoundEmitter:PlaySound("dontstarve/sanity/bishop/attack_1", "bats")
end

local function fn()
    local inst = CreateEntity()
    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    MakeInventoryPhysics(inst)
    RemovePhysicsColliders(inst)
    inst.entity:AddNetwork()
		
    inst.AnimState:SetBank("shadow_bishop")
    inst.AnimState:SetBuild("shadow_bishop")
    inst.AnimState:PushAnimation("atk_side_loop_pre", true)
	inst.AnimState:PushAnimation("atk_side_loop", true)
	inst.Transform:SetScale(0.8, 0.8, 0.8)
    inst.AnimState:SetMultColour(1, 1, 1, .7)

    if not TheWorld.ismastersim then
        return inst
    end

	inst.entity:SetPristine()

	inst:AddTag("fx")
	inst:AddTag("scarytoprey")
	
	inst:DoTaskInTime(5, KillSound)	
    inst:DoTaskInTime(0, SpawnBatVeil)
    inst:DoTaskInTime(5, RemoveBatVeil)
	
	inst:DoTaskInTime(.5, BatVeilAttack_1)
	inst:DoTaskInTime(1, BatVeilAttack_2)
	inst:DoTaskInTime(1.5, BatVeilAttack_1)
	inst:DoTaskInTime(2, BatVeilAttack_2)
	inst:DoTaskInTime(2.5, BatVeilAttack_1)
	inst:DoTaskInTime(3, BatVeilAttack_2)
	inst:DoTaskInTime(3.5, BatVeilAttack_1)
	inst:DoTaskInTime(4, BatVeilAttack_2)
	inst:DoTaskInTime(4.5, BatVeilAttack_1)
	inst:DoTaskInTime(5, BatVeilAttack_2)
	
    return inst
end

return Prefab( "common/inventory/batveil", fn, assets ) 

 

 

Link to comment
Share on other sites

13 minutes ago, SuperDavid said:

Though, sometimes it randomly spawns the bats behind my character like this until I exit the game :(...

I don't want that because it's super ugly...and I have no idea why this is happening... If somebody could help I'd really make my day :D!!

In other game engines it would result in z-fighting, but with DST each prefab is being rendered most likely by entity guid index or some internal entity reference counter on the C-side in order with a simple depth test check to see where in the foreground it should be at.

 

There are two functions I see that can change the order but your mileage may vary.

inst.AnimState:SetSortOrder(0->inf?)

inst.AnimState:SetLayer(k)

k = one of these:

LAYER_BACKGROUND = 1
LAYER_WORLD_BACKGROUND = 2
LAYER_WORLD = 3
LAYER_WORLD_CEILING = 4
LAYER_FRONTEND = 6

Link to comment
Share on other sites

Thank you so much for your reply @CarlZalph if I can just ask do I put 

inst.AnimState:SetSortOrder(0->inf?)

inst.AnimState:SetLayer(k)

and then do one of the numbers from

LAYER_BACKGROUND = 1
LAYER_WORLD_BACKGROUND = 2
LAYER_WORLD = 3
LAYER_WORLD_CEILING = 4
LAYER_FRONTEND = 6

right :)? Like inst.AnimState:SetSortOrder(3)      inst.AnimState:SetLayer(3) ?

Link to comment
Share on other sites

26 minutes ago, SuperDavid said:

inst.AnimState:SetSortOrder(3)

inst.AnimState:SetLayer(LAYER_WORLD)

Yeah, and you may need to tinker with the values.

Though I'm not sure if it'll make the bats draw in front of things like trees when doing it so it might be a downside to it.

The engine seems pretty basic for z order when the zs are identical.

Link to comment
Share on other sites

So I tried 1 through 6 & unfortunately like you said they just made things weirder :(..

inst.AnimState:SetSortOrder()

inst.AnimState:SetLayer()

1 & 2 would make the bats go behind everything,  3, 4 & 5 made them overlap everything...and 6 didn't show anything, hahaha...

 

So...there's probably no other way, right :wilson_worried:?

But, thanks for your guidance CarlZalph :D!!!

Edited by SuperDavid
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...