Mobbstar Posted December 25, 2014 Share Posted December 25, 2014 Prefabs, components, stategraphs, what does all of that mean?! I just want X to Y when Z!And for that matter, you'll find a regularly expanded guide to various much-needed components and related things below! Make sure to read the [other guides] too! Since the syntax highlighter isn't good for Lua, my code-snippets will use the following format: generic - replace_this - comment/note - highlighted (important) If you have questions, reply! But if you need assistance with your mod, make a new topic instead! Content:GeneralArmorBeardBurnableContainerCookableDappernessEaterEdibleEquippableFinite UsesHarvestableHealthHungerInsulatorInventory-ItemPerishablePickablePropagatorRepairableSanity-AuraTalkerToolTradableUseable ItemWorkableLight General (assuming you are familiar with very basic Lua) A component is a bunch of prepared code which every prefab can get. Usually components describe a single property of that prefab, allowing for all kinds of creative combinations. Still confused about those words? [Wots the diff?] To use a component, you first need to add it to your prefab:inst:AddComponent("MyComponent") Then you can customize the exact details:inst.components.MyComponent.maxvalue = 100inst.components.MyComponent:SetSomething(true) If the component is used actively, you can now access that components function when you need them, or set functions the component executes for you:inst.components.MyComponent:BeFun() --prepared functioninst.components.MyComponent.befunfn = BackflipFn --set a function This is theory though, choose a component below to see examples that make sense! Or - even better - look at the game data! Armor This component makes your item armor, reducing all or certain types of damage dealt to the player. Sadly, this component also implies durability. important Variables:tags: table -- armor with tags only has an effect if the attack has an identical tag.onfinished: function -- fires just before the component removes the armor. Theoretically you could use this to make a replacement item or try to reset the condition.ontakedamage: function -- when the armor takes damage important Functions:InitCondition(amount, absorb_percent) --how many hits the armor takes, and how well it does thatSetCondition(amount) -- change the current durability Example: (from hats.lua, thulecite crown) local function ruinshat_proc(inst, owner) -- forcefield when hit inst:AddTag("forcefield") inst.components.armor:SetAbsorption(TUNING.FULL_ABSORPTION) [....] inst.components.armor.ontakedamage = function(inst, damage_amount) if owner then local sanity = owner.components.sanity if sanity then local unsaneness = damage_amount * TUNING.ARMOR_RUINSHAT_DMG_AS_SANITY sanity:DoDelta(-unsaneness, false) end end end inst.active = true owner:DoTaskInTime(TUNING.ARMOR_RUINSHAT_DURATION, function() [...] if inst:IsValid() then inst:RemoveTag("forcefield") inst.components.armor.ontakedamage = nil inst.components.armor:SetAbsorption(TUNING.ARMOR_RUINSHAT_ABSORPTION) owner:DoTaskInTime(TUNING.ARMOR_RUINSHAT_COOLDOWN, function() inst.active = false end) end end)endlocal function tryproc(inst, owner) if not inst.active and math.random() < TUNING.ARMOR_RUINSHAT_PROC_CHANCE then ruinshat_proc(inst, owner) endendlocal function ruins_onunequip(inst, owner) [...] owner:RemoveEventCallback("attacked", inst.procfn)endlocal function ruins_onequip(inst, owner) [....] inst.procfn = function() tryproc(inst, owner) end owner:ListenForEvent("attacked", inst.procfn)endlocal function ruins() local inst = simple() inst:AddComponent("armor") inst.components.armor:InitCondition(TUNING.ARMOR_RUINSHAT, TUNING.ARMOR_RUINSHAT_ABSORPTION) inst.components.equippable:SetOnEquip(ruins_onequip) inst.components.equippable:SetOnUnequip(ruins_onunequip) return instend Beard This component gives your creature a magnificant beard. Insulation and sanity from shaving oneself is hard-coded. important Variables:callbacks: table -- insert the days at which the beard changes state, including a functionprize: string -- the prefab name of the 'beardhair'bits: number -- how much 'beardhair' the beard drops when shavedcanshavetest: function -- a way to limit shaving, return both a boolean and a 'reason'onreset: function -- fires when shaving or ressurrecting important Functions:Reset() -- puts you back at day 0 Example: (from beefalo.lua) local function GetStatus(inst) if inst.components.follower.leader ~= nil then return "FOLLOWER" elseif inst.components.beard and inst.components.beard.bits == 0 then return "NAKED" endendlocal function fn(Sim) [....] local hair_growth_days = 3 inst:AddComponent("beard") -- assume the beefalo has already grown its hair inst.components.beard.bits = 3 inst.components.beard.daysgrowth = hair_growth_days + 1 inst.components.beard.onreset = function() inst.sg:GoToState("shaved") end inst.components.beard.canshavetest = function() if not inst.components.sleeper:IsAsleep() then return false, "AWAKEBEEFALO" end return true end inst.components.beard.prize = "beefalowool" inst.components.beard:AddCallback(0, function() -- same as callbacks[0] = function() if inst.components.beard.bits == 0 then anim:SetBuild("beefalo_shaved_build") end end) inst.components.beard:AddCallback(hair_growth_days, function() --do that for every individual state if inst.components.beard.bits == 0 then inst.hairGrowthPending = true --for stategraph end end) [...] return instend Burnable This component allows your prefab to ignite and burn. This does not include fire spread! (see propagator)Notice that you can use MakeSmallBurnable(inst, time, offset) , M akeMediumBurnable(inst, time, offset) and MakeLargeBurnable(inst, time, offset) for standard objects, and MakeSmallBurnableCharacter(inst, sym, offset) , MakeMediumBurnableCharacter(inst, sym, offset) and MakeLargeBurnableCharacter(inst, sym, offset) for standard creatures. important Variables:burntime: default = nil -- how long it takes to burn entirelyonignite: default = nil -- function when the prefab catches fireonextinguish: default = nil -- function when the prefab is no longer on fireonburnt: default = nil -- function when the prefab reached its burntime (includes onextinguish)canlight: default = true -- whether a lighter can light the prefabburning: --am I on fire? important Functions:AddBurnFX(prefab, offset, followsymbol) -- for advanced effectsSetFXLevel(level, percent) -- for when you need an inferno or a candleIgnite(immediate) -- catch fire, immediate only affects the fxExtinguish() -- stop the fire Example: (from firepit.lua) local function onignite(inst) --when you make a fire, you can cook if not inst.components.cooker then inst:AddComponent("cooker") endend local function onextinguish(inst) --remember to un-do the cooking! if inst.components.cooker then inst:RemoveComponent("cooker") endend local function fn(Sim) [...] inst.components.burnable:AddBurnFX("campfirefire", Vector3(0,.4,0) ) --the firepit has its own fx inst:ListenForEvent("onextinguish", onextinguish) --this isn't the optimal way to do it, but it works too inst:ListenForEvent("onignite", onignite) inst.components.fueled:SetUpdateFn( function() --fueled component handles consumption if GetSeasonManager() and GetSeasonManager():IsRaining() then inst.components.fueled.rate = 1 + TUNING.FIREPIT_RAIN_RATE*GetSeasonManager():GetPrecipitationRate() else inst.components.fueled.rate = 1 end end)end Notice that the firepit doesn't remove itself when burnt, unlike the campfire. As mentioned above, the fueled component handles most things. Container This component allows your prefab to hold items given to it, and all related shizzwaz. There is also an inventory component, but that is very adapted to characters. important Variables:numslots: default = 0 -- how many items your prefab acceptswidgetslotpos: table -- here you need to set the position (relative to the widget position) of the individual slots, using Vector3()canbeopened: default = true -- for when you want to lock your treasure awayacceptsstacks: default = true -- the crockpot, for example, does not accept stackstype: default = "chest" -- can't open two of the same type at the same time widgetanimbank/widgetanimbuild: string -- the visual background to the widgetwidgetpos: Vector3() -- where (relative to the center of the screen) the widget isside_align_tip: number -- unknown effect, but needed (maybe soft following of camera?)itemtestfn: function -- what can go in?widgetbuttoninfo: table -- this is for an interactive button, like the crockpot hasonopenfn/onclosefn: function -- these fire when opened/closed, only parameter is inst important Functions:IsFull()/IsEmpty() -- returns true or falseDropEverything() -- scatters the contents all around the prefabDestroyContents() -- consume all items insideGiveItem(item, slot, src_pos, drop_on_fail, skipsound) -- give an item ('item' is the instance, not the name!)GetItemInSlot(slot) -- returns the actual instanceClose() -- shut itFindItems(fn) -- returns a table with all items that cause 'fn' to return trueHas(item, amount) -- return true/false and the amount (amount is required!)ConsumeByName(item, amount) -- removes that many items which have that prefab name Example: (from cookpot.lua)local slotpos = { Vector3(0,64+32+8+4,0), --this table has the four slots' positions in form of Vector3(x,y,z) Vector3(0,32+4,0), Vector3(0,-(32+4),0), Vector3(0,-(64+32+8+4),0)}local widgetbuttoninfo = { --the cook button (optional) text = "Cook", position = Vector3(0, -165, 0), fn = function(inst) --when clicked inst.components.stewer:StartCooking() end, validfn = function(inst) --can it be clicked? return inst.components.stewer:CanCook() end,}local function itemtest(inst, item, slot) --test whether the item is cookable if cooking.IsCookingIngredient(item.prefab) then return true endend local function onopen(inst) --some fancy stuff inst.AnimState:PlayAnimation("cooking_pre_loop", true) inst.SoundEmitter:PlaySound("dontstarve/common/cookingpot_open", "open") inst.SoundEmitter:PlaySound("dontstarve/common/cookingpot", "snd")endlocal function onclose(inst) --more fancy stuff if not inst.components.stewer.cooking then inst.AnimState:PlayAnimation("idle_empty") inst.SoundEmitter:KillSound("snd") end inst.SoundEmitter:PlaySound("dontstarve/common/cookingpot_close", "close")end local function fn(Sim) [...] inst:AddComponent("container") inst.components.container.itemtestfn = itemtest --can it take that item? inst.components.container:SetNumSlots(#slotpos) --how many slots must it consider? inst.components.container.widgetslotpos = slotpos --where shall it display those slots? inst.components.container.widgetanimbank = "ui_cookpot_1x4" --background inst.components.container.widgetanimbuild = "ui_cookpot_1x4" inst.components.container.widgetpos = Vector3(200,0,0) --where is the entire GUI? inst.components.container.side_align_tip = 100 --??? just set it anyways inst.components.container.widgetbuttoninfo = widgetbuttoninfo --this is needed for a button inst.components.container.acceptsstacks = false --it wants only four ingredients! inst.components.container.type = "cooker" --let chests open parallel to it inst.components.container.onopenfn = onopen --for that fancy stuff inst.components.container.onclosefn = oncloseend Cookable This component allows your item to be cooked on a firepit. important Variables:product: string or function -- the prefab name of the cooked itemoncooked: function -- fires when cooked Example: (from tallbirdegg.lua) local function defaultfn() local inst = commonfn() [...] inst:AddComponent("cookable") inst.components.cookable.product = "tallbirdegg_cooked" return instend Dapperness This component is used to improve or wreck the owners state of sanity. It's handled by 'sanity'. Do not confuse this component with the variable dapperness in 'sanity'! important Variables:dapperness: default = 0 -- the ratedapperfn: function -- interactive sanity if you want it Example: (from sweatervest.lua, that's the ragged vest) local function fn(Sim) [...] inst:AddComponent("dapperness") inst.components.dapperness.dapperness = TUNING.DAPPERNESS_MED return instend EaterThis component allows your prefab to eat [Edible] items. important Variables:strongstomach: default = false -- whether the prefab can eat poisonous foods normallyfoodprefs: table -- all the types of dinner your prefab can feast ononeatfn: function -- fires when eatinglasteattime: GetTime() value -- if you want to compare when your creature last ate, use thisignoresspoilage: default = false -- for robots who like to wear a fedoracaneattest: function -- further specify what can and cannot be eaten important Functions:TimeSinceLastEating() -- similiar to lasteattime, you can use this to determine the time without a new meal Standard-Foodtypes:GENERIC, VEGGIE, MEAT, INSECT, SEEDS, WOOD, ELEMENTAL (minerals), HORRIBLE (you don't want to know...) Example: (from pigman.lua) local function OnEat(inst, food) --these are special food-effects if food.components.edible and food.components.edible.foodtype == "MEAT" --edible food has these types too and inst.components.werebeast and not inst.components.werebeast:IsInWereState() then if food.components.edible:GetHealth() < 0 then --monster meat does bad stuff inst.components.werebeast:TriggerDelta(1) end end if food.components.edible and food.components.edible.foodtype == "VEGGIE" then --poop local poo = SpawnPrefab("poop") poo.Transform:SetPosition(inst.Transform:GetWorldPosition()) endend local function fn(Sim) [...] inst:AddComponent("eater") inst.components.eater:SetOmnivore() -- all standard foodtypes besides wood, elemental and horrible inst.components.eater:SetCanEatHorrible() --add horrible inst.components.eater.strongstomach = true -- can eat monster meat inst.components.eater:SetOnEatFn(OnEat) return instend Edible This component makes your prefab food. important Variables:healthvalue: default = 10 -- how much health it giveshungervalue: default = 10 -- how much it feedssanityvalue: default = 0 -- by how much it raises sanityfoodtype: default = "GENERIC" --who can eat this and who can't Example: (custom) local function fn(Sim) [...] inst.components.edible.foodtype = "MyFoodType" --there's default ones in eater.lua inst.components.edible.healthvalue = -10 --can hurt inst.components.edible.hungervalue = 0 inst.components.edible.sanityvalue = 40 inst.components.edible.stale_health = 2 --you can set multiplier, in this case it hurts twice as much return instend Equippable This component makes your prefab wearable or holdable. You need a swap anim to get the visual working. important Variables:equipslot: default= EQUIPSLOTS.HANDS --where to equip itequipstack: default = false --required for blowdarts and suchisequipped: default = false --whether the prefab is equipped, read-only!onequipfn/onunequipfn: default = nil -- for things that only do stuff when wornwalkspeedmult: default = nil -- speed up or slow down the wearer Example: (from axe.lua)local function onequip(inst, owner) --visual stuff owner.AnimState:OverrideSymbol("swap_object", "swap_axe", "swap_axe") owner.AnimState:Show("ARM_carry") owner.AnimState:Hide("ARM_normal")endlocal function onunequip(inst, owner) --visual stuff owner.AnimState:Hide("ARM_carry") owner.AnimState:Show("ARM_normal")end local function fn(Sim) [...] inst:AddComponent("equippable") --don't need equipslot, as it is hands by default inst.components.equippable:SetOnEquip(onequip) --always remember to set your functions inst.components.equippable:SetOnUnequip(onunequip)end Finite Uses This component limits the lifespan of the prefab. important Variables:total: default = 100 -- set the maximal usescurrent: default = 100 -- how many uses does it have now? onfinished: function -- what to do when you're done (it doesn't break by itself!) important Functions:Use(num) -- use by that many usesSetConsumption(action, uses) --what action takes durability, and how much? Example: (from axe.lua) local function onfinished(inst) --remember, it doesn't dissappear per default inst:Remove()end local function fn(Sim) [...] inst:AddComponent("finiteuses") inst.components.finiteuses:SetMaxUses(TUNING.AXE_USES) --does the same as total = ... inst.components.finiteuses:SetUses(TUNING.AXE_USES) --it loads the saved usage afterwards, does the same as current = ... inst.components.finiteuses:SetOnFinished( onfinished) --does the same as onfinished = ... inst.components.finiteuses:SetConsumption(ACTIONS.CHOP, 1) --it loses durability for chopping return instend Harvestable This component allows your prefab to produce something that can then be taken. important Variables:produce: default = 0 -- how much the prefab holds at the momentgrowtime: default = nil -- how long it takes to produceproduct: default = nil -- what the prefab producesmaxproduce: default = 1 -- how much the prefab can hold important Functions:SetUp(product, max, time, onharvest, ongrow) -- declare all variables and set some functions at once!Grow() -- for when you're impatientStartGrowing(time) -- if you don't give a specific time, it'll use growtimeStopGrowing() -- cancel the outstanding growth task Example: (from beebox.lua) local function onchildgoinghome(inst, data) --when the bees come home... if data.child and data.child.components.pollinator and data.child.components.pollinator:HasCollectedEnough() then --and they bring pollen... if inst.components.harvestable then inst.components.harvestable:Grow() --make honey end endend local function fn(Sim) [...] inst:AddComponent("harvestable") inst.components.harvestable:SetUp("honey", 6, nil, onharvest, updatelevel) inst:ListenForEvent("childgoinghome", onchildgoinghome) --when the bees bring pollen... return instend Health This component adds life and death to your prefab. Required for combat component. important Variables:minhealth: default = 0 -- if you raise this, your prefab can't dieinvincible: default = false -- take a break from dying in battlevulnerabletoheatdamage: default = true -- whether your prefab can burn alivefire_damage_scale: default = 1 -- how devastating fire isnofadeout: default = false -- turn this on to let the prefab stay there, rather than washing outabsorb: default = 0 -- natural armorcanmurder: default = true -- when in inventorymurdersound: string -- soundpath for inventory-murdercanheal: default = true -- when the player tries to use healing items on the prefabredirect: function -- for when you really need to calculate damage/healing differently important Functions:SetMaxHealth(amount) -- initialize healthDoDelta(amount, overtime, cause, ignore_invincible) -- heal/hurtKill() -- put it out of its misery Example: (from slurper.lua) local function fn(Sim) [...] inst:AddComponent("health") inst.components.health:SetMaxHealth(200) inst.components.health.canmurder = false -- can't kill, eventhough it's in the inventory return instend Hunger This component lets your prefab starve over time. That's the entire game concept in a single component. important Variables:max: default = 100 -- how much your prefab can eathungerrate,hurtrate: default = 1 -- how fast you starveburning: default = true -- whether your prefab gets hungryperiod: default = 1 -- in how big steps hunger calculates Example: (from smallbird.lua) local function GetStatus(inst) if inst.components.hunger then if inst.components.hunger:IsStarving(inst) then return "STARVING" elseif inst.components.hunger:GetPercent() < .5 then return "HUNGRY" end endend local function GetPeepChance(inst) --they get louder to show their hunger local peep_percent = 0.1 if inst.components.hunger then if inst.components.hunger:IsStarving() then peep_percent = 1 elseif inst.components.hunger:GetPercent() < .25 then peep_percent = 0.9 elseif inst.components.hunger:GetPercent() < .5 then peep_percent = 0.75 end end return peep_percentend local function fn(Sim) [...] inst:AddComponent("hunger") inst.components.hunger:SetMax(TUNING.SMALLBIRD_HUNGER) inst.components.hunger:SetRate(TUNING.SMALLBIRD_HUNGER/TUNING.SMALLBIRD_STARVE_TIME) inst.components.hunger:SetKillRate(TUNING.SMALLBIRD_HEALTH/TUNING.SMALLBIRD_STARVE_KILL_TIME) return instend Insulator This components is used with 'temperature' to slow down the wearers temperature fluctuations (or at least the cooling out part, idk about RoG). important Variables:insulation: default = 0 --that is literally the whole component Example: (from sweatervest.lua, that's the ragged vest) inst:AddComponent("insulator")inst.components.insulator.insulation = TUNING.INSULATION_SMALL Inventory-Item This component allows your prefab to be picked up and stashed away. Generally this component doesn't need any specification. important Variables:canbepickedup: default = true -- for when your thing is really stickyonpickupfn: function -- fires when picked up, parameters are inst and picker_instcangoincontainer: default = true -- for when your thing is really sticky, say a mobile keepondeath: default = false -- maybe useful for very important character itemsimagename -- the image when in an inventory or containeratlasname -- atlas for your tex fileondropfn -- fires when dropped on the ground, only parameter is instonpickupfn -- fires when picked up, parameter is instonputininventoryfn -- fires when put in inventory... important Functions:GetContainer() -- returns the container/inventory component of the owner (for the owner instance, use GetContainer().inst)ChangeImageName(newname) -- change the appearance of your item Example: (from heatrock.lua) local function UpdateImages(inst, range) --this is called by another component inst.currentTempRange = range inst.components.inventoryitem:ChangeImageName("heat_rock"..tostring(range))[...]end local function inst() [...] inst:AddComponent("inventoryitem") -- usually all you need to do... return instend Perishable This component makes your prefab decompose over time. This does not imply removing the prefab unless you set a replacement! important Variables:perishtime: number -- how long it takes to perish in totalperishfn: function -- fires when perishing (before the replacement)onperishreplacement: string -- whether and what remains when perished important Functions:IsFresh(),IsStale(),IsSpoiled() -- return true or false, depending on the current percentGetPercent() -- returns a value between 1 and 0SetPercent(percent) -- percent must be between 1 and 0ReducePercent(amount) -- subtracts that many percent of total perishtime from the remaining perishtimeStartPerishing(),StopPerishing() -- control whether the prefab spoils at all Example: (from petals.lua) local function fn(Sim) local inst = CreateEntity() [...] inst:AddComponent("perishable") inst.components.perishable:SetPerishTime(TUNING.PERISH_FAST) inst.components.perishable:StartPerishing() inst.components.perishable.onperishreplacement = "spoiled_food" return instend Pickable This component is oddly similiar to [Harvestable], but when transplanted, can barren after being picked too often. important Variables:regentime: number -- how long it takes to regrowproduct: string -- the fruit of labouronregenfn,onpickedfn,makeemptyfn,makefullfn,makebarrenfn,ontransplantfn: functions --fire at those eventsmax_cycles,cycles_left: numbers -- how often the prefab can be picked before barreningcaninteractwith: boolean -- control whether it can be pickednumtoharvest: number -- set this if you want several items for each pickquickpick: boolean -- for when you need it fast and painless important Functions:FinishGrowing() -- for when you're impatientPause(),Resume() -- for when you need a breakSetUp(product, regen, numbertoharvest) -- three in one!IsBarren() -- returns true or falseMakeBarren() -- for when you want to request fertilizer Example: (from carrot.lua) local function onpickedfn(inst) inst:Remove()end local function fn(Sim) [....] inst:AddComponent("pickable") inst.components.pickable.picksound = "dontstarve/wilson/pickup_plants" --you can define a sound inst.components.pickable:SetUp("carrot", 10) inst.components.pickable.onpickedfn = onpickedfn inst.components.pickable.quickpick = true -- carrots are picked quickly, not by fumbling around return instend Example: (from grass.lua) local function ontransplantfn(inst) --it starts off barren when planted if inst.components.pickable then inst.components.pickable:MakeBarren() endend local function onregenfn(inst) --fancy stuff inst.AnimState:PlayAnimation("grow") inst.AnimState:PushAnimation("idle", true)endlocal function makefullfn(inst) inst.AnimState:PlayAnimation("idle", true)endlocal function makebarrenfn(inst) inst.AnimState:PlayAnimation("idle_dead")endlocal function onpickedfn(inst) inst.SoundEmitter:PlaySound("dontstarve/wilson/pickup_reeds") inst.AnimState:PlayAnimation("picking") if inst.components.pickable and inst.components.pickable:IsBarren() then inst.AnimState:PushAnimation("idle_dead") else inst.AnimState:PushAnimation("picked") endendlocal function makeemptyfn(inst) inst.AnimState:PlayAnimation("picked")end local function fn(Sim) [....] inst:AddComponent("pickable") inst.components.pickable.picksound = "dontstarve/wilson/pickup_reeds" --you can define a sound inst.components.pickable:SetUp("cutgrass", TUNING.GRASS_REGROW_TIME) inst.components.pickable.onregenfn = onregenfn --remember to set your funtions inst.components.pickable.onpickedfn = onpickedfn inst.components.pickable.makeemptyfn = makeemptyfn inst.components.pickable.makebarrenfn = makebarrenfn inst.components.pickable.makefullfn = makefullfn inst.components.pickable.max_cycles = 20 --grass can be picked twenty times (that's a lot) inst.components.pickable.cycles_left = 20 inst.components.pickable.ontransplantfn = ontransplantfn return instend Propagator This component allows fire (heat) to spread onto and from the prefab (even if it isn't burnable)Notice that you can use MakeSmallPropagator(inst) or MakeLargePropagator(inst) for standard propagating (and afterwards change stuff if you want to). important Variables:flashpoint: default = 100 --when it "ignites"currentheat: default = 0 --how hot is it?decayrate: default = 1 --how quickly it cools (per seconds)propagaterange: default = 3 --how far the fire can spreadheatoutput: default = 5 --how much heat (per seconds) the prefab emits while spreadingdamages: default = false --whether the fire hurtsdamagerange: default = 3 --how far the fire damages (if at all)acceptsheat: default = false -- whether fire can spread to the prefabonflashpoint: default = nil -- fires when it "ignites" important Functions:Delay(time) -- prevent taking heat for a whileStartSpreading() -- make the prefab spread heatStopSpreading() -- self-explainatoryAddHeat(amount) -- increase the heatFlash() -- force "ignition" Repairable This component allows your prefab to accept items and transform after a certain amount.Keep in mind that this component requires either health, [Workable] or [Perishable] to have an effect. important Variables:repairmaterial: string -- the material required (not the prefab name)announcecanfix: boolean -- whether the player character mentions this component on inspectingonrepaired: function -- fires when given items Example: (from walls.lua) local function onrepaired(inst) if data.buildsound then --these functions are within a function for all types of walls, hence data inst.SoundEmitter:PlaySound(data.buildsound) end makeobstacle(inst) --assure the wall is an obstacle when it gets repairedend local function fn(Sim) [....] inst:AddComponent("repairable") if data.name == "ruins" then inst.components.repairable.repairmaterial = "thulecite" else inst.components.repairable.repairmaterial = data.name --wood, stone or hay end inst.components.repairable.onrepaired = onrepaired return instendSanity-Aura This component raises or lowers the sanity of nearby people. It is very easy to use. For the effect when equipped, look into dapperness! important Variables:aura: default = 0 --set this as the rate (per seconds)aurafn: --limit the people who are affectedpenalty: default = 0 -- reduce the maximal sanity temporarily Talker This component allows your prefab to talk. Talk as in "text above it and some annoying sounds". important Functions:ShutUp() -- stop talking, for when doing more important thingsSay(script, time, noanim) -- say something, script is a string or a table which includes the other two variables Example: (custom) local function greet(inst) inst.components.talker:Say("Good morning!")end local function fn(Sim) [...] inst.ListenForEvent("daytime",greet) return instend Tool This component makes your handheld items tools (related to worker and workable components). important Functions:SetAction(act, effectiveness) -- set what the tool can and can't, effectiveness = nil removes that action Example: (from axe_pickaxe.lua) local function fn(Sim) [...] inst:AddComponent("tool") inst.components.tool:SetAction(ACTIONS.CHOP, 1.33) inst.components.tool:SetAction(ACTIONS.MINE, 1.33) return instend Tradable This component simply indicates that an item can be traded to traders. You can also set a gold value. Example: (custom) local function fn(Sim) [...] inst:AddComponent("tradable") inst.components.tradable.goldvalue = 3 --the pigking buys this inst:AddComponent("equippable") inst.components.equippable.equipslot = EQUIPSLOTS.HEAD --pigmen can wear this inst.components.equippable:SetOnEquip( onequip ) --pretend this is for visual appearance inst.components.equippable:SetOnUnequip( onunequip ) return instend Useable Item This component allows the player to use your prefab. What it does it up to you. This is useful for when there is no other fitting component. important Variables:onusefn -- this happens when the player uses itonstopusefn -- this happens when the player stopscaninteractfn -- this allows you to limit the players ability to use it Example: (from hats.lua, bushhat) local function stopusingbush(inst, data) local hat = inst.components.inventory and inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD) if hat and not (data.statename == "hide_idle" or data.statename == "hide") then hat.components.useableitem:StopUsingItem() endendlocal function onequipbush(inst, owner) [...] inst:ListenForEvent("newstate", stopusingbush, owner) --automatic event by stategraphendlocal function onunequipbush(inst, owner) [...] inst:RemoveEventCallback("newstate", stopusingbush, owner)endlocal function onusebush(inst) local owner = inst.components.inventoryitem.owner if owner then owner.sg:GoToState("hide") endendlocal function bush() local inst = simple() inst:AddTag("hide") inst.components.inventoryitem.foleysound = "dontstarve/movement/foley/bushhat" inst:AddComponent("useableitem") inst.components.useableitem:SetOnUseFn(onusebush) inst.components.equippable:SetOnEquip( onequipbush ) inst.components.equippable:SetOnUnequip( onunequipbush ) return instend Workable This component lets your prefab be mined, hammered, chopped, dug.... important Variables:onwork,onfinish: functions -- fire at those eventsaction: default = ACTIONS.CHOP -- how it can be workedworkleft: default = 10 -- how often it can be worked stillsavestate: default = false -- for when it is really important to stay broken important Functions:Destroy(destroyer) -- for basehassler and suchSetWorkLeft(work) -- manually set the work left Example: (from tree_marsh.lua)local function chop_down_tree(inst, chopper) [...] inst.components.workable:SetWorkAction(ACTIONS.DIG) --now it can no longer be chopped, but dug inst.components.workable:SetOnFinishCallback(dig_up_stump) inst.components.workable:SetWorkLeft(1)endlocal function fn(Sim) [...] inst:AddComponent("workable") inst.components.workable:SetWorkAction(ACTIONS.CHOP) inst.components.workable:SetWorkLeft(10) inst.components.workable:SetOnWorkCallback(chop_tree) --this plays an animation inst.components.workable:SetOnFinishCallback(chop_down_tree) --this makes a stump [...] return instend Light This isn't even a component, but acts a lot like one. Thus, I'll explain it in the same manner. Maybe I'll add images to visualize the differences. add using inst.entity:AddLight() important Functions:SetIntensity(percent) -- how intense and bright the light isSetRadius(number) -- how far the light shinesSetFalloff(percent) -- higher percent mean more rapid fading (after radius)Enable(boolean) -- if the boolean is true, the light turns on, if it is false, it turns off but keeps its propertiesSetColour(r, b, g) -- red/blue/green are percent values Visual Example: Example: (from mininglantern.lua) local function turnon(inst) -- enable light inst.Light:Enable(true) inst.AnimState:PlayAnimation("idle_on")[...]endlocal function turnoff(inst) --disable light inst.Light:Enable(false) inst.AnimState:PlayAnimation("idle_off")[...]end local function fuelupdate(inst) --calculate brightness using math functions local fuelpercent = inst.components.fueled:GetPercent() inst.Light:SetIntensity(Lerp(0.4, 0.6, fuelpercent)) inst.Light:SetRadius(Lerp(3, 5, fuelpercent)) inst.Light:SetFalloff(.9)end local function fn()[...] inst.entity:AddLight() --light is not a component inst.Light:SetColour(180/255, 195/255, 150/255) --clever way you can use the rbg-system fuelupdate(inst)end Of course you can just enable it at all times, or at fixed brightness. You can use the common rbg system using divisions like above, to implement easily pickable colours (for example using [colourpicker]) Link to comment Share on other sites More sharing options...
Prince143 Posted December 26, 2014 Share Posted December 26, 2014 Thanks , this will be useful for my project! Link to comment Share on other sites More sharing options...
Mobbstar Posted December 27, 2014 Author Share Posted December 27, 2014 Thanks , this will be useful for my project! It's my pleasure! I keep adding onto this thread, showing others how to use components and what components there are, and even learn new methods myself, but eventually this page is just going to break the Klei servers Link to comment Share on other sites More sharing options...
simplex Posted December 27, 2014 Share Posted December 27, 2014 Wow, this is a pretty impressive guide. Just the thought of the work you must've had doing the custom code formatting makes me dizzy. Link to comment Share on other sites More sharing options...
DeathDisciple Posted December 29, 2014 Share Posted December 29, 2014 You should definitely ask moderators to link this to the sticky list before it gets lost in here. I'm not sure what's the state of ds wiki or the modding resources on, I admit I never looked. I suppose it can't hurt tho - it's easier to format and maintain. Link to comment Share on other sites More sharing options...
MikeRobe Posted December 29, 2014 Share Posted December 29, 2014 So useful. I'll bookmark this. Link to comment Share on other sites More sharing options...
KingWyrm Posted December 29, 2014 Share Posted December 29, 2014 Can you explain other components that may be useful. I feel those were gear towards custom creation. CAn you explain some components that can be useful for making custom characters maybe. or is that something different. Link to comment Share on other sites More sharing options...
Mobbstar Posted December 29, 2014 Author Share Posted December 29, 2014 You should definitely ask moderators to link this to the sticky list before it gets lost in here. I'm not sure what's the state of ds wiki or the modding resources on, I admit I never looked. I suppose it can't hurt tho - it's easier to format and maintain. I... I already forgot about this thread... If the admins add this to the guides thread once they're back in office, that'd be amazing. EDIT: Wait, does the DS wiki have a modding section? Can you explain other components that may be useful. I feel those were gear towards custom creation. CAn you explain some components that can be useful for making custom characters maybe. or is that something different. I pick the next components by going over the list and checking some components I've heard or used before Do you have any components in mind? Link to comment Share on other sites More sharing options...
KingWyrm Posted December 29, 2014 Share Posted December 29, 2014 To name a few, Health, Hunger, Sanity(in general). I feel The component Combat could be use for on hit effects and hallucination could be used something other than the character going insane. Im kinda looking at the code and trying to understand what is it callling for or defining. I feel like it should be self explanatory but I guess Lack of sleep or plain ignorance is keeping me from it. Wait, does the DS wiki have a modding section? It does Here. Also, Does these thing pertain to DST. I feel the coding might be slightly different because of my other post. My mistake and i cant read apparently Link to comment Share on other sites More sharing options...
Mobbstar Posted December 29, 2014 Author Share Posted December 29, 2014 To name a few, Health, Hunger, Sanity(in general). I feel The component Combat could be use for on hit effects and hallucination could be used something other than the character going insane. I added Health and Hunger (and a few extras ). Maybe I still wind up going over Sanity or Combat tonight. The forums seem to not like that a lot, I'll try some time later. It does Here. Also, Does these thing pertain to DST. I feel the coding might be slightly different because of my other post. I wasn't talking about DST, but the DS wikia, a different website. It's rather messy and unorganized, and all in all not much better than this. Link to comment Share on other sites More sharing options...
KingWyrm Posted December 29, 2014 Share Posted December 29, 2014 Your a Savior. What you gave me another idea for my custom character. Link to comment Share on other sites More sharing options...
Renamon Posted December 30, 2014 Share Posted December 30, 2014 I do hope this gets pinned for all the help it can bring to new coders and some old ones! Link to comment Share on other sites More sharing options...
Mobbstar Posted January 4, 2015 Author Share Posted January 4, 2015 FYI, I found out why couldn't extend this lately... It's too much and blows up the Klei servers every time I try But seriously, I can't add the important components left because it's too much. Maybe I wind up moving this to the DS wikia (if I can get some support), but for now it'll stay as it is. Link to comment Share on other sites More sharing options...
KoopaKiy Posted January 4, 2015 Share Posted January 4, 2015 This is so helpful! It would be amazing if it could be added to the wikia. That was the first place I looked for these. Link to comment Share on other sites More sharing options...
czfshineself Posted January 5, 2015 Share Posted January 5, 2015 This good jod! Can I this topic translated into Chinese, and send it to Chinese forums? Link to comment Share on other sites More sharing options...
Mobbstar Posted January 5, 2015 Author Share Posted January 5, 2015 This good jod! Can I this topic translated into Chinese, and send it to Chinese forums? Yes, please feel free to! Link to comment Share on other sites More sharing options...
kordhrufan Posted January 18, 2015 Share Posted January 18, 2015 I'm trying to add a special power for my character, how I modify the damage she does, and how change it at %50 sanity loss? Link to comment Share on other sites More sharing options...
Blueberrys Posted January 28, 2015 Share Posted January 28, 2015 @MobbstarFirstly, this has been very helpful, great work. Whenever I need to know about a component, I search it here second to the corresponding lua file. May I add them to the unofficial API Docs? Link to comment Share on other sites More sharing options...
Mobbstar Posted January 29, 2015 Author Share Posted January 29, 2015 @MobbstarFirstly, this has been very helpful, great work. Whenever I need to know about a component, I search it here second to the corresponding lua file. May I add them to the unofficial API Docs? I would appreciate that. Sadly I had to stop expanding the list and amending the explanations because the post was too long. May I get permissions to contribute to the unofficial APID? (btw, it took me a while to realise it's Don't Starve only) EDIT: Maybe you could make (or invite me to) a PM? Link to comment Share on other sites More sharing options...
kordhrufan Posted January 30, 2015 Share Posted January 30, 2015 Any item we modify it has to be created in the mod folder? for example, a character does not lose sanity when equip shadow sword. I have to replicate the code of shadow sword in the prefab folder of my mod with the modification? Link to comment Share on other sites More sharing options...
Mobbstar Posted February 4, 2015 Author Share Posted February 4, 2015 I moved all the component explanations over to the unofficial API documentation for Don't Starve. I will no longer maintain or expand this thread. Link to comment Share on other sites More sharing options...
aile11 Posted January 2, 2016 Share Posted January 2, 2016 I'm editing characters for my Friend and me.I would like to know how i can add an effect to a character to gain sanity when another player is nearbyand for another character to gain sanity when it's raining Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.
Please be aware that the content of this thread may be outdated and no longer applicable.