Mod:製作一個自定義天賦
基礎 |
---|
入門 • 基礎 • Lua腳本 • Data.wak • 實用工具 |
製作指南 |
音頻 • 敵人 • 生物群系 • 天賦 • 法術 • 精靈表 • 材料 • 圖像放射器 • 特殊行為 • CMake使用 |
組件/實體 |
組件文檔 • 枚舉 • 特殊標籤 • 所有標籤列表 |
Lua編程 |
Lua API • 實用腳本 |
其他信息 |
法術和天賦的ID • 聲音事件 • 魔數(Magic Numbers) |
Making a new perk is relatively simple, assuming you have gone through the Modding: Basics and have a mod directory set up.
Create & register your perk
- Add a new file to your mod (eg.
files/perk_list.lua
), where you define all your custom perks by appending to theperk_list
table provided by the base game, repeating this code for each perk you want to add:table.insert(perk_list, { id = "MY_CUSTOM_PERK", ui_name = "My Custom Perk Name", ui_description = "A Fancy Description", ui_icon = "data/ui_gfx/perk_icons/electricity.png", -- Change this to your own perk_icon = "data/items_gfx/perks/electricity.png", -- Change this to your own game_effect = "PROTECTION_ELECTRICITY", -- Hardcoded game effect, change or remove this usable_by_enemies = false, not_in_default_perk_pool = false, --set to true to not include it in the default perk pool func = function( entity_perk_item, entity_who_picked, item_name ) -- Any code that you want to run upon perk pickup goes here. end, } ) -- if you have more perks to add table.insert(perk_list, { id = "MY_CUSTOM_PERK_TWO", -- etc } )
- Add the following line to the very beginning of your
init.lua
, referencing the file you just created:ModLuaFileAppend("data/scripts/perks/perk_list.lua", "mods/<MY_AWESOME_MOD>/files/perk_list.lua")
- That's it! Your perk should now be included in the game.
Note:
The game_effect
s are generally hardcoded in-engine effects, which are not customizable via Lua. We can only string them together, or leave them out and make our own effects via custom code (or both!).
For a list of valid effects you can use, see ID#Perks or the data/scripts/perks/perk_list.lua
.
Spawning and picking up a perk
If you want to load the perk via script (and not rely only on holy mountains), you can spawn & pickup the perk with the following snippet:
-- NOTE: If in init.lua, do this *after* the ModLuaFileAppend line and after
-- the player has spawned, for instance in the OnPlayerSpawned callback
dofile_once("data/scripts/perks/perk.lua")
function OnPlayerSpawned(player_entity)
-- Simply spawn the entity in world at the player's location
local x, y = EntityGetTransform(player_entity)
local perk = perk_spawn(x, y, "MY_CUSTOM_PERK")
-- To pick up the perk instantly, you can continue:
perk_pickup(perk, player_entity, EntityGetName(perk), false, false)
end
Checking for active perks
The default perk_pickup()
adds every picked up perk as a "run flag", with the string format of PERK_PICKED_<PERK_ID>
. Thus you can use the following function for easily testing if a perk is active or not:
function has_perk(perk_id)
return GameHasFlagRun("PERK_PICKED_" .. perk_id)
end
Further reading
- You can find list of all base game perks in
data/scripts/perks/perk_list.lua
- You can find the base implementation of perks in
data/scripts/perks/perk.lua
- Most importantly the functions:
perk_spawn()
andperk_pickup()
- Most importantly the functions: