How to set/pass values with Gui objects

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

Moderator: Plugin Moderators

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

Plugin Creator

Platform

How to set/pass values with Gui objects

#1

Post by Hadestia »

This time ill teach you how you can pass values from one gui object to another gui object

I know anyone here also know how it will work, but im here to share this method that I've been using from my past plugins. Some may think that this is possible if you just pass the value to a function with another objects through parameters. Yes That's right, but how about if you want to update the first dialog through second dialog(nested opened dialog) for example? Then this method will help you.

This method would only applicable if you have 2 or more gui objects that all are opened/visible at the same time.

How it Works / things we need
1) Every Gui objects has 'onInit' function that will excute upon initializing the object if provided. The function returns it self for you to modified it from the sets of defined function in it and that's the important thing we need.
2) the second thing, The GUI ID it was important for you to provide an id for that object/s if you want to update or get the value on other gui objects.

Example on how you can use it

Code: Select all    Reset

-- create object 1 function Object1() local canvas = GUI.getRoot():addPanel{ -- set an id to this object (this is important for you to fetch and set values anywhere) id = 'myCanvasWithStorage', x = 10, y = 10, w = 200, h = 100, onInit = function (self) -- lets put a test value in the object self.myValue = '' end, -- lets open object2 when you click this canvas onClick = function () Object2() end } -- shows what button you pressed from object2 canvas:addLabel{ text = '', onUpdate = function (self) self:setText('pressed button:' .. canvas.myValue) --the value from the object end } end --create object2 function Object2() local dialog = GUI.createDialog{ h = 130, w = 230 } local layout = dialog.content:addLayout{ h = 30} -- lets add a button that when you click the button name will display at object1 for i = 1, 2 do layout:addButton{ w = 50, text = 'Button - ' .. i, onClick = function (self) -- fetch object1 to update the value local object1 = GUI.get('myCanvasWithStorage') -- update the value object1.myValue = self:getText() -- close the dialog dialog.close() end } end end -- display object1 function script:buildCityGUI() Object1() end
Interactive Lua editor
Run
The code simply explain the interaction between object1 and object2 as shown on this clip(click to play)
Screenrecording_20220818_175649.gif
User avatar
Memebooms
Inhabitant of a Megacity
Reactions:
Posts: 311
Joined: 23 Oct 2021, 07:20

Re: How to set/pass values with Gui objects

#2

Post by Memebooms »

Hadestia wrote: 18 Aug 2022, 12:07 This time ill teach you how you can pass values from one gui object to another gui object

I know anyone here also know how it will work, but im here to share this method that I've been using from my past plugins. Some may think that this is possible if you just pass the value to a function with another objects through parameters. Yes That's right, but how about if you want to update the first dialog through second dialog(nested opened dialog) for example? Then this method will help you.

This method would only applicable if you have 2 or more gui objects that all are opened/visible at the same time.

How it Works / things we need
1) Every Gui objects has 'onInit' function that will excute upon initializing the object if provided. The function returns it self for you to modified it from the sets of defined function in it and that's the important thing we need.
2) the second thing, The GUI ID it was important for you to provide an id for that object/s if you want to update or get the value on other gui objects.

Example on how you can use it

Code: Select all    Reset

-- create object 1 function Object1() local canvas = GUI.getRoot():addPanel{ -- set an id to this object (this is important for you to fetch and set values anywhere) id = 'myCanvasWithStorage', x = 10, y = 10, w = 200, h = 100, onInit = function (self) -- lets put a test value in the object self.myValue = '' end, -- lets open object2 when you click this canvas onClick = function () Object2() end } -- shows what button you pressed from object2 canvas:addLabel{ text = '', onUpdate = function (self) self:setText('pressed button:' .. canvas.myValue) --the value from the object end } end --create object2 function Object2() local dialog = GUI.createDialog{ h = 130, w = 230 } local layout = dialog.content:addLayout{ h = 30} -- lets add a button that when you click the button name will display at object1 for i = 1, 2 do layout:addButton{ w = 50, text = 'Button - ' .. i, onClick = function (self) -- fetch object1 to update the value local object1 = GUI.get('myCanvasWithStorage') -- update the value object1.myValue = self:getText() -- close the dialog dialog.close() end } end end -- display object1 function script:buildCityGUI() Object1() end
Interactive Lua editor
Run
The code simply explain the interaction between object1 and object2 as shown on this clip(click to play)
Screenrecording_20220818_175649.gif
Did you solve the Age of sail problem?
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: How to set/pass values with Gui objects

#3

Post by Hadestia »


Did you solve the Age of sail problem?
Ye
Post Reply Previous topicNext topic

Return to “Lua Scripting”