Dota 2 inventory editor

Dota 2 inventory editor

Dota 2 inventory editor

Dota 2 lua scripting

The aim of this tutorial is to give an introduction to the scripting system the dota engine uses, and on giving some tips and tricks for developing.
Basic knowledge of OOP programming and lua is assumed.

For more tutorials on addons, or for lua scripting applied to existing (open-source) addons check the main tutorial page.

Please don’t use notepad for this, at least get Sublime or notepad++.

This file does not contain any information about datadriven abilities, for that check out the wiki!

Table of contents

The basics

We will be working in your game/dota_addons/your_addon/scripts/vscripts directory. When your addon is loaded the dota 2 engine executes one file here, namely addon_game_mode. lua, this means that whatever you want to use has to originate from that file, but it does not prevent you from using ‘requires’ to include other lua files. Separating your code into multiple lua files is a great idea for structuring your code. Usually you will have a few libraries/utilities files and a main addon. lua. Dota 2 inventory editor

To include another lua file add this to the top of your file:

The core of your addon

Valve has been nice enough to provide us with a nice addon template, you can find this file in the game/dota_addons/template_addon/scripts/vscripts directory. Just take this file as a basis for your addon, change the class name (CAddonTemplateGameMode) and start developing!

Extending the core

Having your core is nice and everything, but it does not do anything. So now we’ll go over adding some basic functionality through the use of events (that happen ingame) and commands, which can be called from your UI

Events

Events are built into the engine and get called whenever something happens ingame. There are a lot of events such as dota_roshan_kill, dota_courier_lost or dota_player_gained_level. Most events have some extra data, for example on the dota_player_gained_level event, you can also find the playerID and level.
A list of events can be found here. You can also set up your own events in scripts/custom_events. txt (useful for UI interaction)

So how to use these events? There are two main components to using an event in your script, which are the listener and the handler.

The listener just tells your script to listen to a certain event, and once it occurs to execute a certain handler, which is just a function. You can set up a listener everywhere, usually it’s done in the init function. A listener looks like this:

Example
Let’s say we want to give 1000 gold to players that reach level 6. We add our listener to our InitGameMode() like so:
Now we add the handler for this event as a new function in our CustomGameMode object, for this example it looks like this: If you want more examples, you can look at the open source addons on the main page.

Commands

Commands are similar to events, due to the fact that they are also triggers for certain functions. The difference is that the game engine does not call any commands, you have to be the one to call them. This is the best way to have interaction between your flash UI and lua scripts.

We register a command like this:
Now when the server recieves Command1 X in its console, our Handler function is called with parameter X (NB: the parameter is a string!).

Example
Let’s say we have a button in our UI that allows the player to get ability points. From our UI we call the GiveAbilityPoints command with a parameter of how many points we want to give. For example a command call could look like this: ‘GiveAbilityPoints 3’, giving 3 points. We register it like this (place this in init or a function called from it):
Ofcourse we should also add the handler:

Dota 2 inventory editor

Leave a comment