Teleglitch: Die More Edition

Teleglitch: Die More Edition

Not enough ratings
Advanced Weapon/Item Editing with Modloader
By Xangi
This is a follow-up to my first guide. if you haven't read it, you probably should before reading this one. This guide covers more advanced topics on weapon creation through parenting and item modding.
   
Award
Favorite
Favorited
Unfavorite
Intro
This guide assumes you have read "Creating a weapon with Modloader", or at least understand the basics of lua. If you haven't, go read that first. Also please note that you may format the code from this guide however you want, steam simply doesn't allow for neat fomatting like most text editors do, sorry about that.

So, now that you've gone and read that guide, you probably want to make something a bit more complex than just a magnum pistol. Well, that's good because it's exactly what we're gonna do. We're also going to do it in a highly compressed format to same with time and keystrokes. Create a new lua file in usermods called "guide2.lua" and a folder called "guide2". Set up the initialization file like we did in the first tutorial, with a dofile pointing to lua/usermods/guide2/guns.lua. Create guns.lua inside the folder guide2 and fill it with the following.
Parenting and our weapons

itemtable.extpystol =
{
clipsize = 25,
clipreloadtime = 40,
invtext = "9mmpstl+",
longtext = "9mm semi-automatic pistol.\nHas an extended mag."
}
MakeParent(itemtable.extpystol,itemtable.pystol)

itemtable.aupystol =
{
reloadtime = 2.5,frame = 14,
holdframe = 38,
shootframe = 39,
reloadframe = 7,
invtext = "autopstl",
longtext = "9mm automatic pistol.\nVery powerful."
}
MakeParent(itemtable.aupystol,itemtable.pystol)

itemtable.pystolx2 =
{
clipsize = 16,
reloadtime = -8,
clipreloadtime = 60,
ammocount = 2,
bulletcount = 2,
invtext = "9mmpstlx2",
longtext = "9mm semi-automatic pistol.\nGood against small numbers.\nHas two barrels."
}
MakeParent(itemtable.pystolx2,itemtable.pystol)


You might notice that these tables do not appear to contain all the necessary elements to be weapons, however they are parented to the base pistol weapon, known as "pystol". "aupystol" is also a redefinition of the autopistol weapon, which is completely allowed by modloader. By parenting these objects to an already valid weapon, we can simply change what values we want to make different and leave rest the same. What we've made here is 3 pistol upgrades, which we will be using later. The first simple has a longer magazine, the second is automatic and the third shoots 2 bullets at once.
Crafting Expanded
So, we're gonna need to put these in the game, and what better way to do that then through crafting. While we do that, let's take another, closer look at the crafttable table.


crafttable = {}
crafttable[1] = --pistols
{
{"aupystol","hardware","pystol"},
{"smg","aupystol","hardware","tube"}
}


The line crafttable = {} defines a new table named crafttable. This may seem obvious, but the key here is the {}. Anything in {} is treated as an element of a table, so crafttable is initialized as a table rather than a simple variable. We can also see that the elements of crafttable's subtables are in fact tables themselves. Now, there are many ways in which we can add recipes to crafttable as well. Firstly, what we did in the last guide, which is using table.insert.

table.insert(crafttable[1],{"extpystol","pystol","extendmod"})
table.insert(crafttable[1],{"aupystol","pystol","rapidmod"})
table.insert(crafttable[1],{"pystolx2","pystol","doublemod"})


This will simply insert the appropriate recipes at the end of crafttable, however there is a problem. There is already a recipe for aupystol, and while table.insert can also insert in the middle of tables, it cannot erase existing elements. Therefore, if we were to use this method, we will want to remove the second line and add this instead.

crafttable[1][1] = {"aupystol","pystol","rapidmod"}

This will overwrite the existing recipe with our new recipe. But again, there is a problem. Our new recipes appear after the SMG recipe, which is a bit messy when the SMG should be at the bottom as it is clearly a more powerful gun. While we could mess with table.insert, it's really less work in this case to simple redefine crafttable[1].

crafttable[1] =
{
{"extpystol","pystol","extendmod"},
{"aupystol","pystol","rapidmod"},
{"pystolx2","pystol","doublemod"}
{"smg","aupystol","extendmod"}
}


This way we have both reordered the crafting table and added our own recipes. The only disadvantage to this is that it is rather incompatible with other mods, so a minor mod should probably avoid this. However, since we are just doing a tutorial, we'll use this format. Make sure you enter the above before continuing.
Basic Items
So you may be wondering what an "extendmod" or "rapidmod" is. Well, they don't exist yet. We're going to make them. But first let's take a look at a simple item.

itemtable.fattube={
itype =itemtypes.simple,
invtext ="largetube",
longtext ="A long and fat metal tube.",
sprite ="spr_asjad",
frame =27,
stack =1,
onground =true,
}
setmetatable(itemtable.fattube,basictemplate)


This is, again, a subtable of itemtable. However, this one is much smaller than a weapon, and contains some different elements.

itype is the item type, which can be a number of things (list is found in itemtypes in relvad.lua)

stack is how many of these items fit into a single inventory slot.

onground says whether or not this item can actually be dropped and picked back up. If se to false, this item will vanish when dropped.

Other than that, everything seems pretty good and simple. The item uses frame 27 from spr_asjad when it's on the ground, and has no other actions. We could easily use this to make some items like this.

itemtable.newitem={
itype =itemtypes.simple,
invtext ="customitem",
longtext ="This is an example. Don't actually enter it.",
sprite ="spr_asjad",
frame =26,
stack =4,
onground =true,
}
setmetatable(itemtable.fattube,basictemplate)


However, this isn't very exciting, and we're here to do some fun stuff right? Let's take a look at something more complex then.
Lua Items
itemtable.cmeat=
{
itype = itemtypes.lua,
frame = 47,
invtext = "cannedmeat",
longtext = "Canned meat. Tastes like cardboard\nand stays edible for 200 years.",
onactivate = function(this,act,aiming)
if act:GetHealth()<150 then
this:Delete()
act:Heal(10,150)
act:GiveItem("emptycan")
PlaySound("medkit_use")
else
PlaySound("inv_fail")
end
end,
stack=4,
onground = true,
}
setmetatable(itemtable.cmeat,medkittemplate)


This is canned meat. We can clearly see some major differences in between this and the previous item we looked at. Firstly and most importantly, it is of type itemtypes.lua which indicates that it can run lua scripts when various things happen. It also has an onactivate key, which contains a function which runs when the item is selected and used with left click. This function takes 3 arguments from the game: itself, the actor that used it, and whether or not the player is aiming. It then checks if the player is at less than 150HP, and if they are it heals them for 10HP to a max of 150, deletes itself, and adds an empty can to the player's inventory. it also plays the medkit sound. If the player is already at 150HP, it simply does nothing and plays the failure sound.
Item Loops
So what are we gonna do? Well, this function contains the basic parts to create what I call an item loop, which is an item that can loop through various forms. So let's make some, enter the following.

itemtable.extendmod=
{
itype = itemtypes.lua,
frame = 47,invtext = "extendmod",
longtext = "A special piece of hardware that\ncan extend a weapon's ammo cap.\nUse it to change its form.",
onactivate = function(this,act,aiming)
this:Delete()
act:GiveItem("rapidmod")
PlaySound("inv_combine")
end,
stack = 1,
onground = true,
}
setmetatable(itemtable.extendmod,basictemplate)

itemtable.rapidmod=
{
invtext = "rapidmod",
longtext = "A special piece of hardware that\ncan improve a weapon's fire rate.\nUse it to change its form.",
onactivate = function(this,act,aiming)
this:Delete()
act:GiveItem("doublemod")
PlaySound("inv_combine")
end
}
MakeParent(itemtable.rapidmod,itemtable.extendmod)

itemtable.doublemod=
{
invtext = "doublemod",
longtext = "A special piece of hardware that\ncan add a barrel to a weapon.\nUse it to change its form.",
onactivate = function(this,act,aiming)
this:Delete()
act:GiveItem("extendmod")
PlaySound("inv_combine")
end
}
MakeParent(itemtable.doublemod,itemtable.extendmod)

crafttable[13] =
{
{"extendmod","emptycan","hardware"}
{"rapidmod","emptycan","hardware"}
{"doublemod","emptycan","hardware"}
}

Wow is that ever a lot of code, good thing you can just copypaste it. Anyhow, what we did first was create a new lua item, but we set it as a metatable of the basic items so we could get its text to be green. Then we created 2 more, which are children of the first with minor differences. Then, at the end, we created an entirely new crafting category. What is important to not is that when doing this you MUST actually define the new table as shown here, or else you will get a crash and an error. You can also write crafttable[13] = {} and then add elements as normal, but this way is usually faster. So go try it out ingame! Craft the item then click it. Use it to craft one of the new guns. Go on, I'll wait.

Expanding Lua Items & Uncrafting
Ok, so you saw that it worked. You could cycle the item by clicking it, and your new guns all functioned properly. So now we're going to allow you to uncraft your new guns too, by using a method I call expanding lua items. Enter the following.

itemtable.uc_pystol=
{
itype = itemtypes.lua,
frame = 47,
invtext = "9mmpstl",
longtext = "9mm semi-automatic pistol.\nGood against small numbers.",
onpickup = function(this,act)
this:Delete()
act:GiveItem("pystol")
act:GiveItem("hardware")
act:GiveItem("extendmod")
PlaySound("inv_combine")
end,
stack=1,
onground =true,
}
setmetatable(itemtable.uc_pystol,relvtemplate)

table.insert(crafttable[13],{"uc_pystol","extpystol","hardware"})
table.insert(crafttable[13],{"uc_pystol","aupystol","hardware"})
table.insert(crafttable[13],{"uc_pystol","pystolx2","hardware"})


This is a simple uncrafting object. Uncrafting objects are a type of expanding lua object which decraft themselves partially into some of the crafting components used to create them when they enter your inventory. Uncrafting objects use onpickup to perform their action, so that they decraft into the desired parts as soon as the player creates them. If you test this one out ingame, you'll find that you can use hardware to remove the mod from your pistol and keep the mod to reuse later. Expanding lua objects can be used to do many things, this is just a good example of them.
Multiple mod files
So you may have noticed that this file is getting a bit large. I noticed that too. We should fix that, so go into your guide2 folder and make a new file called items.lua. Cut everything after the crafting table rewrite for the weapons we did much earlier out of guns.lua and put it into items.lua. Next, go back into guide2.lua and make a second dofile like, this time pointing to items.lua.

Congratulations, you just organized your mod. When you're doing this on your own, feel free to do it any way you like. I prefer to try to keep each type of item in its own file if I can, and have a big file at the end for recipes, but pretty much any possible way works so long as you don't attempt to parent up the load order or anything silly like that. It might work sometimes if you parent out of order, but I wouldn't try it as a rule.
End
And you're done, you now know pretty much all there is to know about making items and crafting recipes, as well as a few neat tricks. In a later guide we'll cover sounds and graphics, as well as enemy modding and level modding, but for now you should experiment with your new skills. Remember, the worst thing that can happen is a crash, and that's really not very bad.
13 Comments
Xangi  [author] 21 Nov, 2014 @ 8:22pm 
Yes, but only if you make it swap via crafting recipe.
Revenant 21 Nov, 2014 @ 8:19pm 
I know you haven't posted here in a while, but is it possible to make a weapon that can swap beteen 2 forms of shooting?
C Tank 3 Nov, 2014 @ 2:51pm 
Would it be possible to edit in game items?
Xangi  [author] 31 Dec, 2013 @ 3:06pm 
Well, you could possibly simulate a flamethrower with a low range rapid multishot weapon. The "flames" would be all white though.
Krill 31 Dec, 2013 @ 3:01pm 
So creating our own bullet types is out of the question. There goes my idea for a flamethrower. Anyway, thanks and keep up the good work.
Xangi  [author] 31 Dec, 2013 @ 2:58pm 
I do intend to write more, but I've been insanely busy with a number of things, and I'm going out of town soon. When I get some rest and can relax enough to get a good guide done I'll get the next one finished. It'll probably be on levels and how to mess around with them.

Bullet types can't really be modded, but you can do more with them than what you'd expect. In relvad.lua there's a list of each type that can be used.
Krill 31 Dec, 2013 @ 2:38pm 
Love your guides, very helpful. Do you still intend to write more? I would also like to know how bullet types can be modded as I can't seem to find the item tables anywhere.
Xangi  [author] 29 Dec, 2013 @ 6:23pm 
Honestly I'm not sure about that one, it seems to be hardcoded.
[ĜĞ] Grifon 29 Dec, 2013 @ 6:22pm 
Excellent guide, but I'm curious if there is a possibility to edit the stock knife? I feel the game is too unforgiving in the hitboxes for enemies when knifing them and could do for an extended knife (1 or 2 "pixels" at most)
Xangi  [author] 29 Oct, 2013 @ 12:39pm 
It's really quite easy. If you look in resources.lua in modloader you may be able to figure it out on your own. Just remember how tables work and you'll probably get it after a few tries.