Jump to content

Recommended Posts

I've tried the code below

for i, prefab in pairs({ "spat", "warg", "gingerbreadwarg" }) do
    AddPrefabPostInit(prefab, function(inst)
        if inst.event_listening and inst.event_listening.spawnedforhunt then
            table.insert(inst.event_listening.spawnedforhunt[inst], 1, OnSpawnedForHunt)
        else
            inst:ListenForEvent("spawnedforhunt", OnSpawnedForHunt)
        end
    end)
end

No error occurs, but it doesn't work.

Edited by yanecc

You missed half the code. Look at how ListenForEvent adds new listeners.

function EntityScript:ListenForEvent(event, fn, source)
    --print ("Listen for event", self, event, source)
    source = source or self

    if not source.event_listeners then
        source.event_listeners = {}
    end

    AddListener(source.event_listeners, event, self, fn)

    if not self.event_listening then
        self.event_listening = {}
    end

    AddListener(self.event_listening, event, source, fn)
end

 

52 minutes ago, Rickzzs said:

You missed half the code. Look at how ListenForEvent adds new listeners.

function EntityScript:ListenForEvent(event, fn, source)
    --print ("Listen for event", self, event, source)
    source = source or self

    if not source.event_listeners then
        source.event_listeners = {}
    end

    AddListener(source.event_listeners, event, self, fn)

    if not self.event_listening then
        self.event_listening = {}
    end

    AddListener(self.event_listening, event, source, fn)
end

 

Ah, Yes. Thank you very much. As you said I missed some code. So the full version is as below and it works as expected.

if inst.event_listeners and inst.event_listeners.spawnedforhunt or
    inst.event_listening and inst.event_listening.spawnedforhunt then
    table.insert(inst.event_listeners.spawnedforhunt[inst], 1, OnSpawnedForHunt)
    table.insert(inst.event_listening.spawnedforhunt[inst], 1, OnSpawnedForHunt)
else
    inst:ListenForEvent("spawnedforhunt", OnSpawnedForHunt)
end


 

Edited by yanecc

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
×
  • Create New...