Jump to content

Recommended Posts

Hey! I'm trying to make a character based off the sample character mod, and I--with little coding knowledge--am trying to figure out how to add the following traits

--essentially turning batilisks into Catcoons, able to be befriended by this character to give little gifts.

--turning the 'murder' option into a 'drain' option; instead of killing the creature in your inventory, the goal is to remove some of its freshness and restore some hunger to the character (long story).

Any help would be greatly appreciated! Thanks for taking a look at this.

Batilisks would be a challenge because there's nothing in their code that lets them be fully neutral to players like some others have because of a character perk (for example, spiders with Webber.) There's no simple way to add that to a pre-existing prefab, and sadly I can't be much help there. The gifts part shouldn't be that bad if you did have friendly bats.

For turning the murder option to a drain you'd want to make your own action in your modmain file and give it a higher priority over the murder action (which has a possible side effect of also having higher priority over other actions so care is advised.)

Spoiler for some code for that :

Spoiler
-- In modmain.lua
local ACTIONS = GLOBAL.ACTIONS

-- "CHARACTER_DRAIN" is the name added to ACTIONS and can be whatever you want it to be but to avoid conflict you'd likely want it to be something no one else will be using
-- "Drain" is what will appear in-game as the action's name and won't conflict with anything
-- drain_component similarly can be named whatever you name the component file. The actual code for your drain can be done in place of "act.invobject.components.drain_component:DoDrain()", but you'll have more freedom over it within a component function
-- We wanna check that the person doing the action, "doer", is the same as your character so only they can do the action, then check that the thing having the action done to it is what we want to do the action to. In this case it will be the object that has the added component. You could also use the :HasTag() done in the next part, but checking for component means it can't crash if somehow it doesn't have the component were trying to call a function from right after
AddAction("CHARACTER_DRAIN", "Drain", function(act)
	if act.doer~=nil and act.doer.prefab=="my_character" and act.invobject ~= nil and act.invobject.components.drain_component then
		return act.invobject.components.drain_component:DoDrain(act.invobject, act.doer)
	end
end)

-- This part adds the action to a component, which doesn't need to be the same component used above, but you'll likely want it to be as this is the one that will be attached to whatever you want to drain
-- "INVENTORY" specifies what kind of action it is. There is also "USEITEM", "SCENE", "EQUIPPED", and "POINT". For this action you'd want INVENTORY though
-- There may be a tag thats on all the things you wanna be able to murder and nothing else, if so you can use that instead
AddComponentAction("INVENTORY", "drain_component", function(inst, doer, actions)
	if doer~=nil and doer.prefab=="my_character" and inst~=nil and inst:HasTag("drainable") then
		table.insert(actions, GLOBAL.ACTIONS.CHARACTER_DRAIN)
	end
end)
-- Adds the action to Wilson's Stategraph. Leave this as wilson as its what your character uses for a Stategraph
-- "domediumaction" can also be a few different animations, you can find more in the files. Most common is this one and "dolongaction" if you want it to be longer
AddStategraphActionHandler("wilson", GLOBAL.ActionHandler(ACTIONS.CHARACTER_DRAIN, "domediumaction"))
AddStategraphActionHandler("wilson_client", GLOBAL.ActionHandler(ACTIONS.CHARACTER_DRAIN, "domediumaction"))

-- Sets the priority of the action to be higher then Murder's priority of 1
ACTIONS.CHARACTER_DRAIN.priority = 2


-- Now we add our component and tag to whatever you want to have be drainable
-- This can be done with just "AddPrefabPostInit("prefabname", function(inst) inst:AddTag("drainable") inst:AddComponent("drain_component) end)"
-- But since you probably want to add this to more than one thing, you can do it like this
local drainable =
{
  "rabbit",
  "mole",
  -- ETC.
}

for i,v in pairs(drainable) do
  AddPrefabPostInit(v, function(inst)
      inst:AddTag("drainable")
      inst:AddComponent("drain_component")
  end)
end

-- Then were done in modmain

Next you'd need to add a components folder if you don't already have one in the same place as your prefabs folder, and create a new component named whatever you named it in modmain

local Drain_Component = Class(function(self, inst)
    self.inst = inst
    -- you can also initialize any other variables you need here that won't be local to a function, they all need to be "self.variablename ="
end)

-- These functions will not be local, as they can be called by other files. You can also add local functions in the component if they're only going to be called within this component file
function Drain_Component:DoDrain(inst, doer)
  -- Do whatever code you wanna do for draining here. inst will be whatever is being drained, doer will be your character. you are also able to use self.inst which will be whatever the component is attached to.
  return true -- True so the server knows the action was a success. You can also do return false if you add a check that would prevent the action from working
end

return Drain_Component

 

This post has some information on making actions too

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