Difference between revisions of "Dota 2 Workshop Tools/Scripting/Getting Started"

From Valve Developer Community
Jump to: navigation, search
(Undo revision 186832 by Dota2jili (talk))
Line 1: Line 1:
{{otherlang2
+
__TOC__All of ''Dota 2 Workshop Tools'' scripting for addons is handled in Lua. If you're familiar writing script or code in another language then writing concepts in Lua will likely be fairly straight forward.
|zh-cn = Dota 2 Workshop Tools:zh_cn/Scripting:zh_cn
 
}}
 
__TOC__ ''DOTA2创意工坊工具'' 的程序使用的是LUA编程语言,如果你对于编写其他语言比较熟悉的话,那么编写LUA也会是一件很容易的事。
 
  
{{tip|在游戏运行的时候,你能够使用<code>script_reload</code>命令来重新载入你的代码。}}
+
{{tip|You can reload script files at runtime using the console command <code>script_reload</code>.}}
 +
 
 +
== Required Files ==
 +
The following file must be present to have it start running a script. This file must live in the <code>/game</code> directory of the addon and is executed at run time when the addon loads.
  
== 所需要的文件 ==
 
如果你想要运行一些程序,以下的文件将会是必须的,这些文件必须放置在<code>/game</code>目录内对应的插件文件夹,这样在插件被载入的时候,他们就能被执行。
 
 
<source lang="Lua">../game/dota_addons/your_addon/scripts/vscripts/addon_game_mode.lua</source>
 
<source lang="Lua">../game/dota_addons/your_addon/scripts/vscripts/addon_game_mode.lua</source>
  
这个文件并不是可选的,而且必须使用这个名字,其他文件能够在<code>/vscripts</code>文件夹里面创建,你能够在你任何想要引用他们的地方来引用。
+
This file is not optional and is restricted to the provided name. Additional files can be created and referenced from any point inside <code>/vscripts</code> of course and can take on whatever organization method preferred.
  
{{tip|当你第一次创建一个插件的时候,一个非常简单的程序文件会被自动生成在对应的位置,你可以使用它来快速开始你程序的编写。}}
+
{{tip|When creating an addon for the first time a very simple script file will be placed in your addon in the appropriate place, you can use that as a starting point for new addons}}
  
== 运行程序文本 ==
+
== Running Script Files ==
当插件被载入的时候,<code>addon_game_mode.lua</code> 文件将会被立即执行,其他的文件你能够在<code>addon_game_mode.lua</code>中使用<source lang="Lua">
+
When loading an addon the <code>addon_game_mode.lua</code> script file will be executed immediately. Additional script files can be loaded along with <code>addon_game_mode.lua</code> by using
 +
 
 +
<source lang="Lua">
 
require( "lua_filename_here" )
 
require( "lua_filename_here" )
 
</source>
 
</source>
来载入他们。
 
  
== 程序函数 ==
+
== Required Script Functions ==
以下是一些默认的程序函数,在你的插件中应该保留他们。
+
There are some reserved function calls made in every addon that should be observed.
  
=== Precache(预载入) ===
+
=== Precache ===
<code>Precache</code> (预载入)函数将会在游戏之前预先载入一些资源文件,以下的示例包含了三种不同资源的载入方式,一个粒子特效<code>.vpcf</code>文件,一个粒子特效文件夹,还有一个物品。
+
The <code>Precache</code> function will load all required assets in advance of them being required by the game. The example below includes three types of resource precaching. A specific particle file, a particle folder which will load all particle files in that folder, and a specific item.
  
 
<source lang="Lua">
 
<source lang="Lua">
Line 34: Line 33:
 
</source>
 
</source>
  
=== Activate(激活) ===
+
=== Activate ===
<code>Activate</code> (激活)函数用来在程序第一次运行的时候设置游戏模式相关参数,他在文件被载入之后执行。
+
The <code>Activate</code> function is used to setup the game mode for the first time. It is run when the file is loaded and is generally used to setup classes for the game mode to utilize.
  
 
<source lang="Lua">
 
<source lang="Lua">
Line 45: Line 44:
  
 
{{shortpagetitle}}
 
{{shortpagetitle}}
[[Category:Dota 2 创意工坊工具]]
+
[[Category:Dota 2 Workshop Tools]]

Revision as of 10:09, 19 August 2014

All of Dota 2 Workshop Tools scripting for addons is handled in Lua. If you're familiar writing script or code in another language then writing concepts in Lua will likely be fairly straight forward.
Tip:You can reload script files at runtime using the console command script_reload.

Required Files

The following file must be present to have it start running a script. This file must live in the /game directory of the addon and is executed at run time when the addon loads.

../game/dota_addons/your_addon/scripts/vscripts/addon_game_mode.lua

This file is not optional and is restricted to the provided name. Additional files can be created and referenced from any point inside /vscripts of course and can take on whatever organization method preferred.

Tip:When creating an addon for the first time a very simple script file will be placed in your addon in the appropriate place, you can use that as a starting point for new addons

Running Script Files

When loading an addon the addon_game_mode.lua script file will be executed immediately. Additional script files can be loaded along with addon_game_mode.lua by using

require( "lua_filename_here" )

Required Script Functions

There are some reserved function calls made in every addon that should be observed.

Precache

The Precache function will load all required assets in advance of them being required by the game. The example below includes three types of resource precaching. A specific particle file, a particle folder which will load all particle files in that folder, and a specific item.

function Precache( context )
	PrecacheResource( "particle", "particles/items2_fx/veil_of_discord.vpcf", context )	
	PrecacheResource( "particle_folder", "particles/frostivus_gameplay", context )
	PrecacheItemByNameSync( "item_bag_of_gold", context )
end

Activate

The Activate function is used to setup the game mode for the first time. It is run when the file is loaded and is generally used to setup classes for the game mode to utilize.

function Activate()
	GameRules.holdOut = CHoldoutGameMode()
	GameRules.holdOut:InitGameMode()
end