Jump to content

Can't get re-defined variable to client, getting the initial nil value.


Recommended Posts

I've been stuck on this issue for a long time and so I hope someone could enlighten me with a solution.

In a prefab called 'spell_projectile' I initialized a global variable as such:

spell_animname = nil

And in a function called "OnHit" I declare a new value for spell_animname:

spell_animname = special_attack[math.random(#special_attack)]

And then the fx animation is spawned like this:

local impactfx = SpawnPrefab("spell_fx")
impactfx.Transform:SetPosition(x, y, z)

The prefab file for spell_fx:

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


local function PlayImpactAnim(proxy)
	local inst = CreateEntity()

    inst:AddTag("FX")
    --[[Non-networked entity]]
    inst.entity:SetCanSleep(false)
    inst.persists = false

	inst.entity:AddTransform()
	inst.entity:AddAnimState()

    inst.Transform:SetFromProxy(proxy.GUID)
	
    inst.AnimState:SetBank("spell_fx")
    inst.AnimState:SetBuild("royalmagicwand")

    if not TheWorld.ismastersim then
        return
    end

    inst.AnimState:PlayAnimation(spell_animname)
    
    inst:ListenForEvent("animover", inst.Remove)
end


local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddNetwork()

    --Dedicated server does not need to spawn the local fx
    if not TheNet:IsDedicated() then
        --Delay one frame so that we are positioned properly before starting the effect
        --or in case we are about to be removed
        inst:DoTaskInTime(0, PlayImpactAnim)
    end

    inst.Transform:SetTwoFaced()

    inst.entity:SetPristine()

    inst.persists = false
    inst:DoTaskInTime(0.5, inst.Remove)

    return inst
end

return Prefab("common/fx/spell_fx", fn, assets)

So this works as a host, but not as a client and I get a nil value error. The re-defined value for spell_animname is not being stored, and the client reads the first declared variable at the top of the prefab in 'spell_projectile.lua' which is initially nil. How do I get the new value for spell_animname from inside the function?

Thank you in advance

Edited by Tirimiru
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...