[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
Reactions:
Posts: 3610
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
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
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.
=^._.^= ∫

User avatar
ian`
Townsman
Reactions:
Posts: 81
Joined: Sat Apr 04, 2020 17:36
Location: Indonesien
Plugins: Show
Version: Beta

Platform

Plugin Creator

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
:fire

User avatar
Lobby
Developer
Reactions:
Posts: 3610
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
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`
Townsman
Reactions:
Posts: 81
Joined: Sat Apr 04, 2020 17:36
Location: Indonesien
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: [1.8.95] Plugin settings

#4

Post by ian` »

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

User avatar
ian`
Townsman
Reactions:
Posts: 81
Joined: Sat Apr 04, 2020 17:36
Location: Indonesien
Plugins: Show
Version: Beta

Platform

Plugin Creator

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'?
:fire

User avatar
Lobby
Developer
Reactions:
Posts: 3610
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
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
Interactive Lua editor
Run
=^._.^= ∫

User avatar
erksmit
Settler
Reactions:
Posts: 7
Joined: Fri Mar 02, 2018 12:39
Plugins: Show

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`
Townsman
Reactions:
Posts: 81
Joined: Sat Apr 04, 2020 17:36
Location: Indonesien
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: [1.8.95] Plugin settings

#8

Post 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
:fire

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

Platform

Plugin Creator

Re: [1.8.95] Plugin settings

#9

Post by rjroldan1 »

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
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

Post Reply Previous topicNext topic

Return to “Lua Scripting”