Jump to content

Recommended Posts

I'm new in this world, so I need a little help to understand this code:

for k, v in pairs(death_announcement) do
    AddPrefabPostInit(v, function(inst)
        inst:ListenForEvent("death", function()
            local lastattacker = inst.components.combat and inst.components.combat.lastattacker
            if lastattacker ~= nil then
                TheNet:Announce(trophy.."〖 "..inst:GetDisplayName().." 〗"..annstring.gotkilledby..heart.."〖 "..lastattacker.name.." 〗"..battle)
            else
                TheNet:Announce(trophy.."〖 "..inst:GetDisplayName().." 〗"..annstring.gotkilled..battle)
            end
        end)
    end)
end


This one is inside "modmain.lua"
I know the basic of programming, but because I didn't practice LUA before, this is new for me.

AddPrefabPostInit and ListenForEvent are the ones that I don't understand

Thanks

Link to comment
https://forums.kleientertainment.com/forums/topic/153838-how-this-works/
Share on other sites

AddPrefabPostInit is a function made specifically for modders, it basically lets you add code to an entity after it's constructed. The arguments of this function are the prefab name and the function to run the code with, with the argument of this other function being the entity itself (often called "inst").

ListenForEvent is a function entities have, it's used to run some code after a certain event is triggered. Its arguments are the name of the event, the function to run said code and optionally the entity that listens for said event (this defaults to itself if nil). Events are pushed to an entity with PushEvent, in your example the mod is listening for the "death" event, which is pushed when something dies.

Both of these functions are DS/T-exclusive, they're not part of Lua or anything like that.

Hope this helps! If you need further clarification, let me know.

Edited by ClumsyPenny
  • Like 1
  • Shopcat 1

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...