Cyphereion Posted November 17, 2015 Share Posted November 17, 2015 Basically I was wondering how I would do something when an action is performed such as a tree being cut down or the player killing an entity. I have looked around but haven't actually found any useful information as far as this goes. Link to comment https://forums.kleientertainment.com/forums/topic/59165-mod-events-or-similar/ Share on other sites More sharing options...
Developer PeterA Posted November 17, 2015 Developer Share Posted November 17, 2015 To do something when a player kills an entity you'll want to do something similar to the following self.inst:ListenForEvent("entity_death", function(world, data) self:OnEntityDied(data) end, TheWorld)...function HuntGameLogic:OnEntityDied(data) if data.afflicter ~= nil and ( data.inst:HasTag("monster") or data.inst:HasTag("killer") or data.inst:HasTag("animal") ) and data.afflicter:HasTag("player") then...To see all the code, check out my mod http://steamcommunity.com/sharedfiles/filedetails/?id=361336115and look in HuntGameLogic.lua Link to comment https://forums.kleientertainment.com/forums/topic/59165-mod-events-or-similar/#findComment-686708 Share on other sites More sharing options...
Cyphereion Posted November 18, 2015 Author Share Posted November 18, 2015 Thank you for the help so far but it seems that I have run into an issue.For some reason when I go to load the mod it throws an error: Here is my code: modmain:------------------------------PREFABS-------------------------------PrefabFiles = { "prefabtwigblade",}-----------------------------EVENTS------------------------------function EventSetup(inst) inst:AddComponent("EventManager")endAddPrefabPostInit("world", EventSetup)--------------------------NAMES AND VARIABLES------------------------GLOBAL.STRINGS.NAMES.TWIGBLADE = "Twigblade"GLOBAL.STRINGS.CHARACTERS.GENERIC.DESCRIBE.TWIGBLADE = "Pointy and held together by grass.."GLOBAL.STRINGS.RECIPE_DESC.TWIGBLADE = "Pointy and held together by grass.."-----------------------------CONFIGURATION------------------------------TUNING.TWIGBLADE_USES = GetModConfigData("twigblade_uses")TUNING.TWIGBLADE_DAMAGE = GetModConfigData("twigblade_damage")------------------------------RECIPES-------------------------------- STRINGS = GLOBAL.STRINGSRECIPETABS = GLOBAL.RECIPETABSRecipe = GLOBAL.RecipeIngredient = GLOBAL.IngredientTECH = GLOBAL.TECHSTRINGS.RECIPE_DESC.TWIGS = "Split a log into twigs!"--TECH TYPES: TECH.NONE TECH.SCIENCE_ONE TECH.SCIENCE_TWO-- (name, ingredients, tab, level, placer, min_spacing, nounlock, numtogive(# to Craft), builder_tag, atlas, image)Recipe("twigs", {Ingredient("log", 1)}, RECIPETABS.REFINE, TECH.NONE, nil, nil, nil, 5, nil,nil, nil)local twigblade = Recipe("twigblade", {Ingredient("twigs", 10), Ingredient("cutgrass", 5)}, RECIPETABS.WAR, TECH.NONE, nil, nil, nil, nil, nil, "images/inventoryimages/twigblade.xml", "twigblade.tex" )twigblade.atlas = "images/inventoryimages/twigblade.xml"twigblade:SetModRPCID()scrips.components.EventManager:local EventManager = Class(function(self, inst) if TheWorld.ismastersim then self.inst:ListenForEvent("entity_death", function(world, data) self:OnEntityDied(data) end, TheWorld) --self.inst:ListenForEvent("ms_playerspawn", OnPlayerSpawn, TheWorld) endend)function EventManager:OnEntityDied(data) if data.afflicter ~= nil and data.afflicter:HasTag("player") and data.inst:HasTag("killer") then for k,v in pairs(AllPlayers) do v.components.health:DoDelta( 1 ) end endendAnother thing that I am confused about is exactly what can I do with the data and how would I get the specific player object for use? There isn't much documentation on this so i'm stuck asking you and seeing where I can get. Link to comment https://forums.kleientertainment.com/forums/topic/59165-mod-events-or-similar/#findComment-686742 Share on other sites More sharing options...
Mobbstar Posted November 18, 2015 Share Posted November 18, 2015 Pardon if I completely missed the point or my suggestion does not work in Together, but wouldn't it be easier and cleaner to attach the event listener to the player entity directly? It should be possible to construct the callback function in the weapon's prefab file and attach/remove the event listener in the equip/unequip functions. Link to comment https://forums.kleientertainment.com/forums/topic/59165-mod-events-or-similar/#findComment-686852 Share on other sites More sharing options...
Developer PeterA Posted November 18, 2015 Developer Share Posted November 18, 2015 @Cyphereion, looks like your EventManager file isn't returning the component. You'll want to add return EventManagerto the bottom your file. Link to comment https://forums.kleientertainment.com/forums/topic/59165-mod-events-or-similar/#findComment-686938 Share on other sites More sharing options...
Cyphereion Posted November 19, 2015 Author Share Posted November 19, 2015 @PeterA Getting there, I seem to be having an issue with the self.inst argument being null. I've tried setting it to only call if it is not a null object however it seems it only registers once. I should note that I would like this to fire for every player. Link to comment https://forums.kleientertainment.com/forums/topic/59165-mod-events-or-similar/#findComment-686951 Share on other sites More sharing options...
Cyphereion Posted November 19, 2015 Author Share Posted November 19, 2015 Pardon if I completely missed the point or my suggestion does not work in Together, but wouldn't it be easier and cleaner to attach the event listener to the player entity directly? It should be possible to construct the callback function in the weapon's prefab file and attach/remove the event listener in the equip/unequip functions.I am trying to grab it as a global variable, this has nothing to do with the item which is why I didn't mention it. Link to comment https://forums.kleientertainment.com/forums/topic/59165-mod-events-or-similar/#findComment-686952 Share on other sites More sharing options...
Mobbstar Posted November 19, 2015 Share Posted November 19, 2015 Getting there, I seem to be having an issue with the self.inst argument being null. I've tried setting it to only call if it is not a null object however it seems it only registers once. I should note that I would like this to fire for every player. You need to place "self.inst = inst" at the top of the constructor function. The instance that receives the component is given as an argument (inst) but needed all over the component, so the component should keep a refernce to its instance. I am trying to grab it as a global variable, this has nothing to do with the item which is why I didn't mention it. I see, you're aiming to make a big mod. Nice! Link to comment https://forums.kleientertainment.com/forums/topic/59165-mod-events-or-similar/#findComment-687073 Share on other sites More sharing options...
Cyphereion Posted November 19, 2015 Author Share Posted November 19, 2015 @Mobbstar Thanks a lot for helping me out, but again I am running into issues due to the lack of documentation for the API. In the code block:function EventManager:OnEntityDied(data)print("Event Fired!") if not(data.afflicter == nil) and data.afflicter:HasTag("player") then --print("Is killer!" + data.afflicter) data.afflicter.sethealth(0.75) endendhow would I actually do something with the player and what are the limitations to this? sethealth doesn't seem to be a method and I can't print the object itself so i'm at a loss here. Link to comment https://forums.kleientertainment.com/forums/topic/59165-mod-events-or-similar/#findComment-687209 Share on other sites More sharing options...
Mobbstar Posted November 20, 2015 Share Posted November 20, 2015 how would I actually do something with the player and what are the limitations to this? sethealth doesn't seem to be a method and I can't print the object itself so i'm at a loss here. there's a so-called component for health. Unfortunately, DST is highly confusing in this matter, as only host's entities have components, and all client's entities have replicas. I'm sure there's plenty of mods that demostrate this better than I could explain it, not to mention game code. Link to comment https://forums.kleientertainment.com/forums/topic/59165-mod-events-or-similar/#findComment-687445 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now