Jump to content

Mod Events? (or similar)


Recommended Posts

  • Developer

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=361336115

and look in HuntGameLogic.lua

 

Link to comment
Share on other sites

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:

1QDQBzb.png

 

 

 

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  endend

Another 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
Share on other sites

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
Share on other sites

@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
Share on other sites

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
Share on other sites

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! :encouragement:

Link to comment
Share on other sites

@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)  endend

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.

Link to comment
Share on other sites

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