Jump to content

How to Start Coding


Recommended Posts

So I'm someone who has a o.k understanding of programming languages and wish to make a mod with custom perks. I find that other threads on "mod help" usually just give solutions rather than the processes to get there. As a newbie, I am quite confused looking at these new functions from the game's source codes. I don't know where to find these function's function, or a good place to start on how to understand how items, mobs and characters work. I just need someone to point me to the right direction to appropriate resources. Thank you.     

Link to comment
Share on other sites

Yes, usually post give solution, rather than a process. I guess it's mainly because most people are modding with an idea in mind and try to do what is needed to make this idea. At least i know it's how i'm working : i have an idea, and i try to find a way to make it.

And because of this, it's hard for people like me to give advices about where to start, because i don't know where to start : i started with what i wanted to do (a recipe for an item, adding a new resource...), rather than in  a way that is methodical or organized.

Link to comment
Share on other sites

And that's how you should start I guess. I think that modding is not an abstract thing and is best to be learnt on concrete things.

If you don't really have an idea of what you want to achieve right now, then maybe the simplest would be to take a random recipe in game and modify it to see how that is done. Then move to do a simple decorative item which has no purpose. Then try to do an equippable item following the tutorial about these. Even if what you will produce is not what you want to do and you will never use it, you will learn how to do these basic things.

Once you have successfully accomplished that, you are ready to do more advanced stuffs and then the forum will look much more helpful.

Link to comment
Share on other sites

What I'm trying to get at I guess, is that I don't know what every function does or what functions I need for certain actions. Usually in a programming language, like C#, I can just look up a function on the internet. C# is universal but this code isn't. Is there a library for Don't starve we have access to?  

Link to comment
Share on other sites

I don't know.

 

But there is an API (a little outdated possibly) here with some example of function :

Another good way to know what a function is doing is to search in game file for similar function. A text editor often has an option for this, allowing you to search in an entire folder. So you can search other examples of one function, for example.

 

Link to comment
Share on other sites

The way DST works is not very complicated.

The first layer for modders I would say is the prefab/components one.

Each entity in the world (a tree, the players, monsters, a spear, meatballs, ...) is a prefabs. You have in the don't starve folder, a subfolder named data/scripts/prefabs

All the entity which exist in the game have a prefab script in this folder.

It's called prefab because these scripts are just blueprints to create a new entity in game. When you craft a spear for example, a function is called to use the script data/scripts/prefabs/spear.lua and execute the constructor for the spear inside that script to create an instance of the spear prefab (usually referred as inst in the code).

How these constructors works?

First a new generic entity is created. It has nothing attached to it and is not doing anything. From here the prefab functions will populate that entity with various things, like C-side elements which we don't really have access has modders (we can merely use the functions exposed to us) like an AnimState (to handle the animations), a Transform (the position of your entity in the world), a Physics (which function is obvious), ...

And then comes the main part for us modder as these are pure lua scripts so we can do whatever we want with them: the components.

Components are the bricks you use to build you entity. You will find the Klei's built-in components in data/scripts/components

In your spear prefab constructor function, you will add to your generic entity the following things

a InventoryItem component which has functions used to handle the fact that this entity can go inside a container or in the player inventory.

a Equippable component which handles the fact that it can be equipped by the player and be used

a Weapon component which indicated it can be used to deal damages

etc...

You should look at a few basic prefab scripts by yourself and you will understand the logic behind this.

Then you have additional layers which are irrelevant when you begin with modding and as long as you don't have a good understanding of what's happening at the lower prefab/components level. These are the Stategraphs, the Brains, the Behaviours, the Widgets and are a discussion for another time.

I would advice to use a tool like http://stefanstools.sourceforge.net/grepWin.html which is free and allow you to search for a string in all the files of a specific folder.

Let's imagine you want to make an item which is a hat with an integrated pickaxe that would allow you to mine while still having your hands free. You would look at the components:

components/inventoryitem.lua

components/equippable.lua (the hat is an equippable item)

components/tool.lua

From here you identify function which look interesting/promising/the name suggest it would be useful for you, you open grepWin in the data/scripts folder and search for this function. It will return you all the files where this function appear. You could then try to see how it is used in Klei's prefabs.

Hope this helps

Edited by ZupaleX
Link to comment
Share on other sites

@hetimperley You might be a little overwhelmed ^_^" I'll simplify everything a little ;)
To start modding, you need to create, at the bare minimum, 1 folder containing 2 files: modinfo.lua and modmain.lua

Take a look at those examples from my mod, [Hk]Config++ (rewritten for presentation purposes):
modinfo.lua

-- This is comment. This line won't do anything! I write important things here ^_^ -- This is addition info, you don't need to read it if it looks threatening :D
name = "mod name" -- It's name of your mod -- This will be visible to everybody :)
description = "mod description" -- This is mod secription, as seen in-game. -- You can add ne lines by writing "\n". Note that it should be somewhat short.
author = "mod author" -- Write you nick here ^_^
version = "1.0.0" -- This is mod version. It's important! You can update mod only if you'll change mod version. -- Use x.y.z notation with numbers only where x means major overhaul, y indicates adding new things and z means fixes in last version (reset z to 0 after you change y, and y and z after you change)
forumthread = "/files/file/<YOUR_FILE_ULR>/" -- You can add link to this forum here! ^_^ -- Or leave it blank to point to workshop page instead ^_^"
api_version = 10 -- It has to 10 right now! -- It informs game if your mod is up-to-date with latest major update.
dst_compatible = true -- For DST it must be true!
dont_starve_compatible = false
reign_of_giants_compatible = false
all_clients_require_mod = true -- If it changes game behaviour it must be true. If it's client-only, it must to false.
client_only_mod = false -- If it's server mod it must be false. If it's client-only, it must to true.
server_filter_tags = {"tag1","tag2",} -- Here you add mod tags -- People will find your mod if they will write part of it's name or one of those!
configuration_options = -- If you DON'T want option write configuration_options = { }
{
  {
    name = "use_mod",
    label = "Use mod",
    hover = "Mod will work if this is set to \"yes\"",
    options = {
      {description = "yes", hover = "use mod", data = true},
      {description = "no", hover = "don't use mod!", data = false},
    },
    default = true,
  },
  {
    name = "icebox_spoil_rate",
    label = "Permanent food",
    hover = "Should food in iceboxes or icepacks spoil?",
    options = {
      {description = "yup", hover = "normal", data = .5},
      {description = "nope!", data = 0},
    },
    default = 0,
  },
  {
    name = "fast",
    label = "Fast work",
    hover = "Select which things you want to do fast",
    options = {
      {description = "cutting!", data = "cut"},
      {description = "mining!", data = "mine"},
      {description = "picking!", data = "pick"},
      {description = "none.", data = "no"},      
    },
    default = "pick",
  },
}

modmain.lua

local TUNING = GLOBAL.TUNING -- This way we can access game variables

if GetModConfigData("use_mod") then -- NOTHING will work if user will select "no" as a first option!
    local spoil_rate = GetModConfigData("icebox_spoil_rate")
    if spoil_rate ~= 0.5 then
        TUNING.PERISH_FRIDGE_MULT = spoil_rate
    end
    local quick_action = GetModConfigData("fast")
    if quick_action == "cut" then
        TUNING.EVERGREEN_CHOPS_SMALL = 1
        TUNING.EVERGREEN_CHOPS_NORMAL = 1
        TUNING.EVERGREEN_CHOPS_TALL = 1
        TUNING.MUSHTREE_CHOPS_SMALL = 1
        TUNING.MUSHTREE_CHOPS_MEDIUM = 1
        TUNING.MUSHTREE_CHOPS_TALL = 1
        TUNING.DECIDUOUS_CHOPS_SMALL = 1
        TUNING.DECIDUOUS_CHOPS_NORMAL = 1
        TUNING.DECIDUOUS_CHOPS_TALL = 1
    elseif quick_action == "mine" then
        TUNING.ICE_MINE = 1
        TUNING.ROCKS_MINE = 1
        TUNING.ROCKS_MINE_MED = 1
        TUNING.ROCKS_MINE_LOW = 1
        TUNING.MARBLEPILLAR_MINE = 1
        TUNING.MARBLETREE_MINE = 1
        TUNING.CAVEIN_BOULDER_MINE = 1
        TUNING.PETRIFIED_TREE_SMALL = 1
        TUNING.PETRIFIED_TREE_NORMAL = 1
        TUNING.PETRIFIED_TREE_TALL = 1
        TUNING.PETRIFIED_TREE_OLD = 1    
    elseif quick_action == "pick" then
        local function quickpick(inst)
            if inst.components.pickable then
                inst.components.pickable.quickpick = true
            end
        end
        AddPrefabPostInit("berrybush", quickpick)
        AddPrefabPostInit("berrybush2", quickpick)
        AddPrefabPostInit("blue_mushroom", quickpick)
        AddPrefabPostInit("cactus", quickpick)
        AddPrefabPostInit("flower_cave", quickpick)
        AddPrefabPostInit("flower_cave_double", quickpick)
        AddPrefabPostInit("flower_cave_triple", quickpick)
        AddPrefabPostInit("grass", quickpick)
        AddPrefabPostInit("green_mushroom", quickpick)
        AddPrefabPostInit("lichen", quickpick)
        AddPrefabPostInit("marsh_bush", quickpick)
        AddPrefabPostInit("red_mushroom", quickpick)
        AddPrefabPostInit("reeds", quickpick)
        AddPrefabPostInit("sapling", quickpick)
    end
end

After making your first mod you need to place it in "mods" directory within DST folder. If you want to go public, grab Don't Starve Mod Tools ^_^
Also, 2 more short examples:

Adding gears recipe:

local Ingredient = GLOBAL.Ingredient
local CHARACTER_INGREDIENT = GLOBAL.CHARACTER_INGREDIENT
local RECIPETABS = GLOBAL.RECIPETABS
local Recipe = GLOBAL.Recipe
local TECH = GLOBAL.TECH
AddRecipe("gears", {Ingredient("nightmarefuel", 2), Ingredient("transistor", 1)}, RECIPETABS.SCIENCE, TECH.MAGIC_ONE) 

Making rabbits stackable:

local stackable_replica = require "components/stackable_replica"
local IsServer = GLOBAL.TheNet:GetIsServer()
local function makeAnimalStackable(inst)
    if inst ~= nil then
        if (inst.components.stackable == nil) then
            inst:AddComponent("stackable")
        end
        inst.components.inventoryitem:SetOnDroppedFn(function(animal)
            inst.components.perishable:StopPerishing()
            inst.sg:GoToState("stunned")
            if inst.components.stackable then
                while inst.components.stackable:StackSize() > 1 do
                    local item = inst.components.stackable:Get()
                    if item then
                        if item.components.inventoryitem then
                            item.components.inventoryitem:OnDropped()
                        end
                        item.Physics:Teleport(inst.Transform:GetWorldPosition())
                    end
                end
            end
        end)
    end
end
if IsServer then
    AddPrefabPostInit("rabbit", makeAnimalStackable)
end

 

Link to comment
Share on other sites

Looking at Beehive, what is the difference between pushAnimation and playAnimation?

  • Looking at the codes below from Ipsquiggle: What does priority mean?

local flowercake = {
 name = "flowercake",
 test = function(cooker, names, tags) return tags.flower >= 2 and names.butter end,
 priority = 1,
 weight = 1,
 foodtype="VEGGIE",
 health = TUNING.HEALING_TINY,
 hunger = TUNING.CALORIES_LARGE,
 sanity = TUNING.SANITY_TINY,
 perishtime = TUNING.PERISH_MED,
 cooktime = 0.75,
}

  • What is this trying to tell me? Or is "test" an actual attribute.

test = function(cooker, names, tags) return tags.flower >= 2 and names.butter end,

  • Here, what is "ipairs" , "Parallel" Also is node.children[1].name referring to the pig?

local fireindex = nil
 for i,node in ipairs(brain.bt.root.children) do
  if node.name == "Parallel" and node.children[1].name == "OnFire" then
   fireindex = i
   break
  end
 end
 

  • What does this mean:

local assets =
{
    Asset("ANIM", "anim/carrot.zip"),
}

 

Thank you guys for helping me, I know it might be tedious.  

Edited by hetimperley
Link to comment
Share on other sites

21 minutes ago, hetimperley said:

Looking at Beehive, what is the difference between pushAnimation and playAnimation?

Not sure, i think it replace an existing animation but i'm not familiar with it.

21 minutes ago, hetimperley said:

Looking at the codes below from Ipsquiggle: What does priority mean?

Priority is used when you use ingredients that could be used in multiple recipes. For example, if i use mandrake, i will end with a mandrake soup, because the recipe has a high priority (10).

If i use only veggies, i will end with ratatouille (priority 0), only if no other recipe is possible, because any other recipe will have higher priority (for example, stuffed eggplant (priority 1).

21 minutes ago, hetimperley said:

What is this trying to tell me? Or is "test" an actual attribute.

It's testing the tags/names you need to make the recipe. It's here you'll put the requirement of the recipe. Some examples :

        test = function(cooker, names, tags) return (names.pumpkin or names.pumpkin_cooked) and tags.sweetener and tags.sweetener >= 2 end,

Pumpkin cookies needs pumpkin, and at least ingredients with a sweetener value of minimum 2. Usually, honey, but you can add custom ingredient with a sweetener tag. So you need at least 1 pumpkin (cooked or not) and a value of 2 sweetener (2 honey, or one ingredient with 2 sweetener value, or...)

		test = function(cooker, names, tags) return tags.meat and tags.veggie and tags.meat >= 1.5 and tags.veggie >= 1.5 end,

Hot chili needs meat and veggies, each with at least a total value of 1.5. So for example, it could be a monster meat + a small meat, a monster meat and a meat, two meats. For veggies, most veggies have a value of 1, but mushrooms and cactus flower seems to have a value of 0.5. So one recipe could be "monster meat + small meat + blue cap + carrot".

Look at preparedfoods.lua and cooking.lua (in scripts) for examples of recipes and tags.

 

Edited by Lumina
Link to comment
Share on other sites

PlayAnimation reset the animation queue and plays the animation you specify.

PushAnimation push the animation at the end of the queue. Which means it will wait for all the previous animatios in the queue to be played before playing the one you just pushed.

Link to comment
Share on other sites

Well, this thread was damn useful. I wanted to create similar one, but now I don't need it. I know a thing or two about programming, but I have other specialty. I'm a network engineer. Game coding is different from the stuff I do usually. Not so long ago I got Cisco certification (BTW, it was damn hard, I've even taken help from SPOTO CLUB site), but it doesn't help when you want to code a game. What else should I learn about game coding? I think that bumping this thread is a good idea. And that's why I'm doing it.

Edited by BaronSamedi
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...