Tree Planter Tool (Open Source)

The Lua scripting language allows you to give your plugin more advanced features.

Moderator: Plugin Moderators

User avatar
Posts: 3637
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
Version: Beta


Tree Planter Tool (Open Source)


Post by Lobby »

As of version 1.9.32 the game contains the so called tree planter tool that allows to plant a variety of trees easily by tapping somewhere on the map. This tool was implemented as a plugin and serves as a basic example of how to use Lua for tools and the new GUI features. The concept of the UI and the icon were made by @THEMAX .

The general structure of the code looks like that :lua: :

Code: Select all    Reset

-- Some local variables used for state local minBrushSize local maxBrushSize local treeDrafts local density local roughness local brushSize local treeSelection -- Cache drawing functions for performance reasons local setTile = Drawing.setTile local drawTileFrame = Drawing.drawTileFrame -- Gets the storage table we use for saving/loading the settings local function getStorage() end -- Save settings to storage local function saveState() end -- Load settings from storage local function loadState() end -- Show about dialog local function about() end -- Show configuration dialog local function configure() end -- True if trees can be placed here in general local function isValid(x, y) end -- Initialize stuff, e.g. load settings function script:init() end -- Setup tool, including the buttons for configuration and about dialog function script:event(x, y, level, event) end -- Draw tile based overlay, red frame for invalid placed function script:draw(tileX, tileY, hoverX, hoverY) end -- Execute action function script:click(tileX, tileY) end
Interactive Lua editor
The json features real time scripting for testing purposes :json :

Code: Select all    Reset

[ { "id":"$treeplantertool00_open", "type":"tool", "preview frames":[{"bmp":"icon.png"}], "title id":"draft_treeplantertool00_title", "text id":"draft_treeplantertool00_text", "category":"$cat_tree00", "script":"#LuaWrapper", "meta":{ "luawrapper":{ "script":"script.lua", "dev":true } } } ]
JSON checker
You'll find the git repository here:
You can use the option "Download ZIP" to download the whole plugin.

Please feel free to create your own tools based on this one. You can also make changes to this tool and make a pull request of the changes on GitHub.

More detailed documentation about the GUI features of Lua is coming soon.
=^._.^= ∫

Return to “Lua Scripting”