[1.8.95] Plugin settings

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

Moderator: Plugin Moderators

Lobby
Developer
Reactions:
Posts: 3639
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
Version: Beta

[1.8.95] Plugin settings

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.

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
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.
=^._.^= ∫

ian
Supporter
Reactions:
Posts: 102
Joined: Sat Apr 04, 2020 17:36
Location: Indonesien
Plugins: Show
Version: Beta

Re: [1.8.95] Plugin settings

What is the Generic Text have same functions with command (console)?

Lobby
Developer
Reactions:
Posts: 3639
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
Version: Beta

Re: [1.8.95] Plugin settings

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

ian
Supporter
Reactions:
Posts: 102
Joined: Sat Apr 04, 2020 17:36
Location: Indonesien
Plugins: Show
Version: Beta

Re: [1.8.95] Plugin settings

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

ian
Supporter
Reactions:
Posts: 102
Joined: Sat Apr 04, 2020 17:36
Location: Indonesien
Plugins: Show
Version: Beta

Re: [1.8.95] Plugin settings

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
Edit---
I think the problem is solved. but i have one question, what for is '== nil and true'?

Lobby
Developer
Reactions:
Posts: 3639
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
Version: Beta

Re: [1.8.95] Plugin settings

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
=^._.^= ∫

erksmit
Villager
Reactions:
Posts: 10
Joined: Fri Mar 02, 2018 12:39
Plugins: Show

Re: [1.8.95] Plugin settings

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

ian
Supporter
Reactions:
Posts: 102
Joined: Sat Apr 04, 2020 17:36
Location: Indonesien
Plugins: Show
Version: Beta

Re: [1.8.95] Plugin settings

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

rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 653
Joined: Mon Jul 17, 2017 16:16
Location: Philippines
Plugins: Show
Contact:

Re: [1.8.95] Plugin settings

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

.╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────
T.P.C.
Theotown Philippine Corporation