[1.8.95] Plugin settings

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

Moderator: Plugin Moderators

User avatar
Lobby
Developer
Posts: 3717
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

[1.8.95] Plugin settings

#1

Post by Lobby »

Version 1.8.95 will allow your plugin to add own settings to the game's settings window by providing a script:settings() function in a Lua script that is used in your plugin.
:img
image.png

Code: Select all    Reset

-- We define the local variable for our settings table here so that our functions can see it local settings function script:init() -- Let's get a storage table and initialize values that are nil settings = Util.optStorage(TheoTown.getStorage(), self:getDraft():getId()..':settings') settings.someBool = settings.someBool == nil and true or settings.someBool settings.someInt = settings.someInt or 1 settings.someNamedInt = settings.someNamedInt or 3 settings.someString = settings.someString or 'Xyz' settings.someGenericText = settings.someGenericText or 'Hello World' end -- This function will be called whenever the game enters or refreshes the settings view. -- The result is expected to be a table of tables that contain settings. function script:settings() -- Return a table of tables return { -- Let's define a boolean setting { -- The name of the setting name = 'Bool setting', -- The current value of this setting value = settings.someBool, -- onChange is a function that will be called when the values has changed onChange = function(newState) settings.someBool = newState end }, -- Let's define an integer setting { name = 'Integer setting', value = settings.someInt, -- We provide a list of possible values values = {1, 2, 3}, onChange = function(newState) settings.someInt = newState end }, -- Let's define an integer setting with custom names for the values { name = 'Named integer setting', value = settings.someNamedInt, values = {1, 2, 3}, -- We provide a list names for the of possible values that we've specified valueNames = {'A', 'B', 'C'}, onChange = function(newState) settings.someNamedInt = newState end }, -- Let's define a setting with multiple string options { name = 'Some strings', value = settings.someString, values = {'Abc', 'Xyz', '123'}, onChange = function(newState) settings.someString = newState end }, -- Let's define a setting for generic text that can be edited { name = 'Generic text', -- This description will be shown when user enters a new text desc = 'Here you can enter a text:', value = settings.someGenericText, onChange = function(newState) settings.someGenericText = newState end, -- onReset is a function that will be called when users presses the settings reset button onReset = function() settings.someGenericText = 'Reset was called' end } } end
Lua editor
Run
The label that is shown in front of the settings indicates what plugin/file provides these settings. For plugins from Plugin Store it shows the plugin's name.

User avatar
ian`
Supporter
Posts: 118
Joined: 04 Apr 2020, 17:36
Location: Indonesien
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: [1.8.95] Plugin settings

#2

Post by ian` »

What is the Generic Text have same functions with command (console)? :bt
Show
Screenshot_20200709-072150_60.png
Screenshot_20200709-072203_60.png

User avatar
Lobby
Developer
Posts: 3717
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

Re: [1.8.95] Plugin settings

#3

Post by Lobby »

It's like the input dialog for rules in online mode. You use it to edit some sort of text.

User avatar
ian`
Supporter
Posts: 118
Joined: 04 Apr 2020, 17:36
Location: Indonesien
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: [1.8.95] Plugin settings

#4

Post by ian` »

I see. Because it looks strange in the settings tab.

User avatar
ian`
Supporter
Posts: 118
Joined: 04 Apr 2020, 17:36
Location: Indonesien
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: [1.8.95] Plugin settings

#5

Post by ian` »

What is it code correct to set the default boolean value settings to be false? Because when i'm use that code, bad argument issue occurs.

Code: Select all

settings.someBool = settings.someBool == nil and false or settings.someBool
Show
Screenshot_20200710-063904.png
Edit---
I think the problem is solved. but i have one question, what for is '== nil and true'?

User avatar
Lobby
Developer
Posts: 3717
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

Re: [1.8.95] Plugin settings

#6

Post by Lobby »

Since nil is also considered false in Lua I have to check if it is actually nil before applying a default value. I have to ensure that the variable contains true or false because the game expects a boolean value in order to show the checkbox.

Although nil is considered as false, it is not the same as false, meaning that nil == false is false.

Code: Select all    Reset

local function foo(var) print(var == nil and true or var) end foo(nil) -- Should evaluate to the "default value" true foo(false) -- Should be false foo(true) -- Should be true
Lua editor
Run

User avatar
erksmit
Villager
Posts: 23
Joined: 02 Mar 2018, 12:39
Plugins: Showcase Store
Version: Beta

Plugin Creator

Re: [1.8.95] Plugin settings

#7

Post by erksmit »

is it possible to use a textbox or slider as a setting? i want the user to be able to enter any number value they want

User avatar
ian`
Supporter
Posts: 118
Joined: 04 Apr 2020, 17:36
Location: Indonesien
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: [1.8.95] Plugin settings

#8

Post by ian` »

erksmit wrote:
03 Nov 2020, 17:07
is it possible to use a textbox or slider as a setting? i want the user to be able to enter any number value they want
i think you only can use like generic text (in this case textField) and add more vars on the onChange options.

Code: Select all    Reset

function script:settings() return{ { name = 'Generic text', -- This description will be shown when user enters a new text desc = 'Here you can enter a text:', value = settings.someGenericText, onChange = function(newState) settings.someGenericText = newState vars1 = newstate -- optional variable to be changed with new value end } } end
Lua editor
Run

User avatar
Hadestia
Inhabitant of a Megalopolis
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: [1.8.95] Plugin settings

#9

Post by Hadestia »

Is it really possible to add "hidden":true for all the ingame drafts if a made plugin settings for it?

Post Reply Previous topicNext topic

Return to “Lua Scripting”