Page 1 of 1

[1.8.95] Plugin settings

Posted: Wed Jul 08, 2020 14:39
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
Interactive 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.

Re: [1.8.95] Plugin settings

Posted: Thu Jul 09, 2020 2:30
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

Re: [1.8.95] Plugin settings

Posted: Thu Jul 09, 2020 9:49
by Lobby
It's like the input dialog for rules in online mode. You use it to edit some sort of text.

Re: [1.8.95] Plugin settings

Posted: Thu Jul 09, 2020 10:52
by ian`
I see. Because it looks strange in the settings tab.

Re: [1.8.95] Plugin settings

Posted: Fri Jul 10, 2020 1:59
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'?

Re: [1.8.95] Plugin settings

Posted: Sat Jul 11, 2020 12:59
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
Interactive Lua editor
Run

Re: [1.8.95] Plugin settings

Posted: Tue Nov 03, 2020 17:07
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

Re: [1.8.95] Plugin settings

Posted: Thu Nov 05, 2020 10:08
by ian`
erksmit wrote:
Tue Nov 03, 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
Interactive Lua editor
Run

Re: [1.8.95] Plugin settings

Posted: Thu Nov 05, 2020 17:06
by rjroldan1
Is it really possible to add "hidden":true for all the ingame drafts if a made plugin settings for it?