Jump to content

Recommended Posts

Grumpy I am and grumpy I will be. I said I won't come back, well, Steam doesn't have a modding section per se and as far as tutorials, be it written or video go... I have no idea why nobody does these. So many mods, yet nobody has bothered to try and make some tutorials in terms of what they've created or something that commonly could be useful (except character mods, which goddamn we have way too many of). Oh well.

So, my problem is, I've no idea how to apply something I have been writing a bunch of values for like 3 or so days now. My aim is to create weight... component? I've no idea what what things are called in lua (and yeah, I've tried to read something like this for example, but half the time it did not explain me anything, so don't please jump at me telling me I should just learn the language first. I had no problem learning GML, considering the huge Manual, the great help from yoyo games forum and everything being neatly placed in a nice engine. This is not the case for DS/DST, but even after being explained what to do and how and understanding why from this tutorial... I still cannot comprehend how I would go about doing what I wanna do atm). I've created two new scripts: a script which stores all of the weight for all of the items (not yet all, but a large portion of the existing ones in DST) and a script which is supposed to program in what the weight itself is and what it will do. What I am aiming for is the following:

* Check which item is in inventory

* Check how many of that item is in inventory (multiply the amount with item's ITEMWEIGHT with the item amount in inventory)

* Apply calculated weight to player character by reducing speed based on the full calculated weight (speed = speed - totalitemweight)

What I seem to struggle on the most is making sure WHEN the game checks and applies all of the above. Doing it every step/frame of the game could insue huge amounts of lag. Therefore, making an "if" statement, which checks whether an item has been picked up/given by another player/taken from a chest or in whatever else way your inventory has changed would be when all of the above would be carried out. The problem is, however... I have no idea how the inventory and picking up and all of those shenanigans work or which scripts from game's data actually have coded into them these actions and all of those shenanigans.

P.S I'll only use the forums for modding purposes from now on. Everything else will just create problems, most likely.

Edited by EuedeAdodooedoe
Link to comment
Share on other sites

Unfortunately you are right, there are not may tutorials and I don't know one that would help in this case.

All you can do at the moment is 1) ask here. 2) Study game scripts.

I will try to explain some basic knowledge about  how those things work:
- You have prefabs. These are mostly things ingame you can see, like animals, trees, character and so on.

- You have components. These are the skills/characteristics of the prefabs. Eg. the beard, health, sanity or the inventory and so on are components.

Quote

making an "if" statement, which checks whether an item has been picked up/given by another player/taken from a chest or in whatever else way your inventory has changed

Yes, it works simular to this. The game uses "Events". So e.g when anyone equip something, the event "equip" is fired. You can subscribe to this event to run a function. So everytime this event fires, you function will be called.
In case of equip look at this post:
http://forums.kleientertainment.com/topic/70022-need-help-making-character-transparentsolved/?do=findComment&comment=811255

Unfortunately there is no list of all the events that exist. But you can go to the inventory.lua component and search for "event". There you will most likely see a bunch of the events that are used. Just pick those you need.

Also a program like Notepad++ , which allows you do search hundreds of lua files at once, is really helpful, if you don't know where something is done ;)

So in your case, search the inventory.lua for Events that could help you. And look at the functions they are calling in this file. Then make your fuctions with the same parameters, but the stuff you want to do ;)

Link to comment
Share on other sites

@Serpens hmm, so I found a few things that were set under ListenForEvent. Most of these I believe I would need to set for my code:

Spoiler

 

"death"
"itemlose"
"dropitem"
"itemget"
"gotnewitem"
"inventoryfull" - probably no need to check during this event
"setoverflow" - no idea what event this is, but based on the code it has something to do with unequipping
"unequip"
"newactiveitem" - not too sure what this would do exactly

 

 

 

But question is... how? I do not see where these events are being set exactly or what to check them off of. I highly doubt this would work:

Spoiler

-- In the script which stores weight values of each item

ItemPhysicsAttributes("log", ITEMWEIGHT = 0.1)

Spoiler

-- In the script which creates weight mechanics

function ItemPhysicsAttributes(item)
    if ListenForEvent == "itemget" or "gotnewitem" or "inventoryfull" or "newactiveitem" then
        runspeed = runspeed - ITEMWEIGHT
    elseif ListenForEvent == "death" or "itemlose" or "dropitem" or "unequip" or "newactiveitem" or "setoverflow" then
        runspeed = runspeed + ITEMWEIGHT
    end
end

return runspeed

 

Edited by EuedeAdodooedoe
Link to comment
Share on other sites

Quote

if ListenForEvent == "itemget" or "gotnewitem" or "inventoryfull" or "newactiveitem" then

this is wrong, see my linked code for equip and unequip I posted above ;)

To subscribe to the events, I did:

local function OnEquip(inst,data) -- this function is called, when the event is fired. inst is in this case the player character, see comment below. data is a table of stuff that might be useful. I have no clue what it contains...
    -- do some stuff...
    for v,k in pairs(data) do -- get an idea of what data contains
        print("data v: "..tostring(v).." k: "..tostring(k)) -- this way you get an idea of what data contains. If k is again a table, you can print this the same way again... 
    end
end


AddPlayerPostInit(function(inst) -- is used to add something to every player. "inst" is now the character
    inst:ListenForEvent("equip", OnEquip) -- call the function everytime someone is equipped
    inst:ListenForEvent("unequip", OnEquip) -- also call it, if sth is unequipped
end)


 

Link to comment
Share on other sites

On 9/9/2016 at 10:55 PM, Serpens said:

this is wrong, see my linked code for equip and unequip I posted above ;)

To subscribe to the events, I did:


local function OnEquip(inst,data) -- this function is called, when the event is fired. inst is in this case the player character, see comment below. data is a table of stuff that might be useful. I have no clue what it contains...
    -- do some stuff...
    for v,k in pairs(data) do -- get an idea of what data contains
        print("data v: "..tostring(v).." k: "..tostring(k)) -- this way you get an idea of what data contains. If k is again a table, you can print this the same way again... 
    end
end


AddPlayerPostInit(function(inst) -- is used to add something to every player. "inst" is now the character
    inst:ListenForEvent("equip", OnEquip) -- call the function everytime someone is equipped
    inst:ListenForEvent("unequip", OnEquip) -- also call it, if sth is unequipped
end)


 

None of this or what you wrote in your first post makes any sense to me; I don't understand the reason to use any of this to do anything. What I also don't understand is why I could very easily understand GML language, but not Lua. I really do not know where to begin, considering even understanding how the language itself works made no sense to me half the time... With GML it was pretty straight forward; read the manual, which exaplains pretty much everything (and it was much easier to actually search for what things I needed, so lets say I need to do something with physics, like creating a collision box, the manual has it all), look at some tutorials and ask for help at the respective forum, where you can actually get a ton of help. Here, I have no idea where to even start, because nothing makes sense to me at all.

Like, what is inst? Instance? Am I creating a function here? Why would I need "data" for exactly and how does the program understand it in the place it's placed? And what does that mean for me? What the heck is "v" and "k" for the "for" statement? What does "in pairs" mean? Why would I want to print anything at all? Print displays things in the compiler, right? Why do I need this? The program just has to process things, none of this would help me in any way that I can see. What is "AddPlayer Post Init"? And what do you mean by "every character"? Why would "inst" change with what you've done there? How does the "ListenForEvent" work? As in, I understand it has two values for it, which is "equip" or "unequip" and the second one is OnEquip, but what do those stand for? What do they mean? What do they do? And why are they needed for?

Edited by EuedeAdodooedoe
Link to comment
Share on other sites

sry, I thought you know lua already.
For basic lua I'm sure you can find a tutorial ;)

But for the game code style itself, it is really mostly asking here, or studying the game code and try to understand, what it does and how it works... Of course you have to know lua to understand it first...

 

- "inst" is just a variable name and stands for instance. You can call it whatever you like,  since devs called it inst, we are naming it also inst.
- A function is created, if you use the word "function", so yes, in the code you quoted are 2 functions.
- The game developers defined events. So in this case, if someone equips something, the event "equip" is called. That means that the game is "looking" in every script, if there is something listening to the event. If so, the function connected to the event is called.
In our example I connected the function "OnEquip" to the "equip" event, with help of this "ListenForEvent" command.

Quote

What is "AddPlayer Post Init"? And what do you mean by "every character"? Why would "inst" change with what you've done there?

The script from your modmain.lua, this is the scriptfile you will fill for most mods, will be called when starting the game.
Every command will be executed.
AddPlayerPostInit is one command. The name says it already. The stuff you put into this command, will be execueted everytime a new player is initialized (so joins the game).
So in this case "inst" will be the joining player, which will change of course if you have more than 1 player.
And in our example we want to subscribe to the Event "equip". so we write:
inst:ListenForEvent("equip", OnEquip)
This means that inst will call the command ListenForEvent, so the event we listen for is "equip", and if the event happens (if this player equips something), we want the script to execute the function "OnEquip", which we defined above.
On this function "OnEquip" we can define now, what should happen if the players equip something. We can write anything we like to.
The first parameter is "inst" and the second one is "data". I think you know how you normally call a function? In our case we called the OnEquip function indirect with help of the ListenForEvent, where we defined no arguments. So you can't know these parameters for OnEquip, if you don't know how ListneForEvent handles this specific event. There might be a rule that it is always the same pattern, but I'm not sure if there is. So the best is to look at other scripts that uses ListForEvent with "equip" and see how it is done there ;) That is how I found out, that the parameters are "inst" and "data".


 

Quote

Why would I want to print anything at all? Print displays things in the compiler, right? Why do I need this?

Everything you print, will be in the logfile of the game or also ingame when you hit CTRL + L.
You print things to debug your code or to see which variable contains what.
In our example I have no clue what "data" contains, so I suggested that you print it first, see what it is, and then continue to do your code.

Quote

What the heck is "v" and "k" for the "for" statement? What does "in pairs" mean?

Do you know what a "for" loop is? It is a loop through all things eg. in a list.
The v,k is standing for "value" and "key". The key comes first (so I named it the wrong way round, sry :D). You can of course give it the names you like. So you can name it also "position" and "ListEntry".
I could make a list like this: list={"a",0,"z",34} . This list contains some random characters and numbers.
If I do now:
for k,v in pairs(list) do
    print(k.." "..v)
end
This will go through my list, and print each key and the value of my list.
In this case the key is just the position of the value in the list. And the values are "a",2,"z" and so on.
So this will print:
"1 a"
"2 0"
"3 z"
"4 34"
The ".." in my print statement is a string connector thing in lua. This way you can connect two strings (and also numbers).
In our example I also used "tostring()" because the ".." only works for strings and numbers. And scine we don't know what data contains, I change it to a string first.
The "pairs" is a special lua thing. I'm not expert in lua, but I for some reason lua needs this for getting the elements of the list. In some other languages you don't need such thing.

 

Link to comment
Share on other sites

On 9/9/2016 at 6:27 PM, Serpens said:

Unfortunately you are right, there are not may tutorials and I don't know one that would help in this case.

For DS modding guides and documentation, true (even though there's a bit of stuff on this site: http://www.dontstarveapi.com/ (EDIT: site is down - archived version) and also scattered throughout this forum, of course), sadly... For Lua usage and understanding, which is very helpful and pretty essential, there are naturally heaps of resources on the web, though. Not sure why the OP would peruse the links he gave but not, for example, the immediately-findable official online free guide book and official reference, which I find are good resources.

I also second the Notepad++ reccomendation (and recommend associating .lua files with it), both for the Find in Files feature (use "*.lua" as the filter, of course) and the Lua syntax highlighting. Then there are also extra conveniences including code block folding and file tabs, and just being an all-around good program/file editor.

Edited by blubuild
added archive link
Link to comment
Share on other sites

Omg... yesterday or the day before I had a lesson at college where I was taught about creating functions and listeners in Action script 3 language, so this whole thing makes a bit more sense to me :p (not entirely sure why you've put a listener after you've set a function there though... I guess it does not matter? Or it's necessary to be set before the listener in Lua specifically or for this instance).

I guess I'll learn a bit more and try and get things working somehow. If things still don't work out, I'll come here and ask for help, but for now... I've gotta educate myself :p

Link to comment
Share on other sites

instead of the listener thing above, you can also write:
 

local function OnPlayerAdd(inst)
    inst:ListenForEvent("equip", OnEquip) -- call the function everytime someone is equipped
    inst:ListenForEvent("unequip", OnEquip) -- also call it, if sth is unequipped
end
AddPlayerPostInit(OnPlayerAdd)

the AddplayerPostInit expects a function as argument, which it should call, if a player is added.
That's why I put a function there ;)

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