handling value in postshow
Moderator: Forum Moderators
- hermestrismi
- Posts: 626
- Joined: February 6th, 2016, 11:28 pm
- Location: Tunisia
- Contact:
handling value in postshow
I spare some to learn more in Lua and I learnt quite good things but still I get always hit by the postshow process like here:
So, I will be glad if someone help me to understand the way to use a return_value in postshow
full code (can be useful to anyone) using 4 different methods
Code: Select all
postshow3 = function(retval)
-- Check if the player clicked "OK" button
-- someting like:
if retval == 1 then
-- Get the selected music track from the 'preselected_track' variable
wml.variables.selected_track = selected_track_details
end
end
full code (can be useful to anyone) using 4 different methods
Code: Select all
-- Define local variables and import necessary modules
local T = wml.tag -- Alias for wml.tag to simplify tag creation
local wml_actions = wesnoth.wml_actions -- Reference to WML actions module
local _ = wesnoth.textdomain "wesnoth-test_freshII" -- Localization function
local helper = wesnoth.require "lua/helper.lua"
local selected_track_details = 1
-- local preselected_track = 1
-- Table to store track information
local tracks = {
-- Specify details for each track
{
id = "harp-song",
name_of_track = _ "harp-song", -- Localized name of the track
direct_source = "Illusion_Campaign", -- Source of the track
type_of_source = "campaign", -- Type of the source (e.g., campaign, addon)
upload_source_version = _ "Wesnoth 1.0" -- Version of the source where track was uploaded
},
{
id = "harp-song",
name_of_track = _ "harp-song",
direct_source = "Illusion_Campaign",
type_of_source = "campaign",
upload_source_version = _ "Wesnoth 1.0"
},
{
id = "Elvish_Aid",
name_of_track = _ "Elvish_Aid",
direct_source = "Mermens_Plight",
type_of_source = "campaign",
upload_source_version = _ "Wesnoth 1.0"
},
{
id = "Friends_and_Relatives",
name_of_track = _ "Friends_and_Relatives",
direct_source = "Mermens_Plight",
type_of_source = "campaign",
upload_source_version = _ "Wesnoth 1.0"
},
{
id = "fire",
name_of_track = _ "fire",
direct_source = "Unrest",
type_of_source = "campaign",
upload_source_version = _ "Wesnoth 1.0"
},
{
id = "wesnoth-3",
name_of_track = _ "wesnoth-3",
direct_source = "A_Princes_Tale",
type_of_source = "campaign",
upload_source_version = _ "Wesnoth 1.2"
},
{
id = "epilogue",
name_of_track = _ "epilogue",
direct_source = "Flight_Freedom_1_x",
type_of_source = "campaign",
upload_source_version = _ "Wesnoth 1.2"
},
{
id = "intro",
name_of_track = _ "intro",
direct_source = "Flight_Freedom_1_x",
type_of_source = "campaign",
upload_source_version = _ "Wesnoth 1.2"
},
{
id = "Survivor_theme",
name_of_track = _ "Survivor_theme",
direct_source = "Frezycuss_Junks",
type_of_source = "campaign",
upload_source_version = _ "Wesnoth 1.2"
},
-- more tracks with similar properties here
}
-- Define the GUI template and functions for the third music configuration dialog
local show_third_music_configuration_dialog = {
-- Function to execute before showing the dialog
preshow3 = function()
-- Add any necessary pre-dialog setup code here
end,
-- Define the GUI elements and layout for the third dialog
dialog3 = {
-- GUI elements for the 'Add', 'Remove', and 'OK' buttons
-- Customize the layout and buttons as needed for the third dialog
-- Add tooltips, linked groups, buttons, labels, etc. as required
T.tooltip {id = "tooltip_large"},
T.helptip {id = "tooltip_large"},
T.linked_group {id = "unit", fixed_width = true},
T.grid {
T.row {
T.column {
border = "all",
border_size = 30,
T.grid {
linked_group = "unit",
T.row {
T.column {
T.label {
use_markup = true,
label = "<span size='large' weight='bold'>" .. _("Music Configuration") .. "</span>",
id = "Music"
}
}
},
T.row {
T.column {
T.label {
id = "selected_t_details", -- Unique ID for the label
label = selected_track_details, -- Initial empty label
definition = "default", -- Define a named widget
style = "text" -- Style for the text
}
}
},
T.row {
T.column {
T.grid {
columns = 2,
T.row {
T.column {
T.button { id = "button_add_another", label = "Add", return_value = 1 }
},
T.column {
T.button { id = "button_remove_another", label = "Remove", return_value = 2 }
}
}
}
}
},
T.row {
T.column {
T.button { id = "Leave", label = _("Leave unchanged") }
}
}
}
}
}
}
},
-- Function to execute after the dialog is shown
postshow3 = function(retval)
-- Check if the player clicked "OK" button
if retval == 1 then
-- Get the selected music track from the 'preselected_track' variable
wml.variables.selected_track = selected_track_details
end
end
}
-- Function to show the second music configuration dialog
function show_second_music_configuration_dialog()
-- Initialize selection variable for track selection
local selection = 1
local selected_track_details = 1
local preselected_track = 1
-- Function to be called before showing the dialog
function pre_show(self)
-- Set the title and message of the dialog
self.title.label = _ "Select a Track"
self.message.label = _ "Before you get started, you need to select a track."
-- Populate the listbox with track information
-- Populate the listbox with track information
local listbox = self.tracks
-- Iterate over the 'tracks' table to populate the listbox items
for i, character in ipairs(tracks) do
-- Create a new item for each track in the listbox
local new_item = listbox:add_item()
-- Set the labels for each column in the listbox item
new_item.direct_source.label = character.direct_source -- Set the 'direct_source' label
new_item.name_of_track.label = character.name_of_track -- Set the 'name_of_track' label
new_item.type_of_source.label = character.type_of_source -- Set the 'type_of_source' label
new_item.upload_source_version.label = character.upload_source_version -- Set the 'upload_source_version' label
end
listbox.selected_index = selection -- Set default selection
end
-- Function to be called after the dialog is shown
function post_show(self)
selection = self.tracks.selected_index -- Update selection based on user input
-- selected_track_details = self.tracks.selected_index.name -- Update selection based on user inputif -1 == gui.show_dialog(wml.get_child(dialog_wml, 'resolution'), pre_show, post_show) then
result = tracks[selection].id -- Get the selected track ID
gui.show_dialog(show_third_music_configuration_dialog.dialog3, show_third_music_configuration_dialog.preshow3, show_third_music_configuration_dialog.postshow3)
end
-- Load the dialog WML and show the dialog
local dialog_wml = wml.load "~/add-ons/test_freshII/gui/Secondary_music_menu.cfg"
wml.variables.selected_track_details = tracks[selection].id
wml.variables.preselected_track = wesnoth.sync.evaluate_single(function()
local result = false
-- Check if the dialog was closed without selection
if -1 == gui.show_dialog(wml.get_child(dialog_wml, 'resolution'), pre_show, post_show) then
result = tracks[selection].id -- Get the selected track ID
end
wml.variables.selected_track_details = tracks[selection].id
return {selected = result}
end).selected
end
-- Function to show the first music configuration dialog
function show_first_music_configuration_dialog()
-- Load the primary music menu
local menu_selection_dialog = wml.load '~/add-ons/test_freshII/gui/Primary_music_menu.cfg'
local dialog_wml = wml.get_child(menu_selection_dialog, 'resolution')
-- Show the dialog and process user selection
local result = wesnoth.sync.evaluate_single(function()
return { value = gui.show_dialog(dialog_wml) } -- Get user selection from the dialog
end)
local selected_topic = result.value -- Determine the selected topic
if selected_topic == 1 then
show_second_music_configuration_dialog() -- Show the second music configuration dialog
end
wesnoth.redraw {} -- Refresh the display after processing dialog
end
-- WML action to handle menu selection
function wml_actions.select_menu()
-- Load the menu selection dialog
local menu_selection_dialog = wml.load '~/add-ons/test_freshII/gui/menu_selection.cfg'
local dialog_wml = wml.get_child(menu_selection_dialog, 'resolution')
-- Show the dialog and handle user selection
local result = wesnoth.sync.evaluate_single(function()
return { value = gui.show_dialog(dialog_wml) } -- Get user selection from the dialog
end)
local selected_topic = result.value -- Determine the selected topic
if selected_topic == 1 then
show_music_configuration_dialog() -- Call a function to handle music configuration
end
if selected_topic == 2 then
wesnoth.fire_event_by_id("openmenu") -- Fire an event to open a menu
end
if selected_topic == 3 then
show_first_music_configuration_dialog() -- Call a function to show first music configuration dialog
end
wesnoth.redraw {} -- Refresh the display after processing dialog
end
Last edited by Ravana on February 28th, 2024, 3:41 pm, edited 1 time in total.
Reason: [code] is better without [section]
Reason: [code] is better without [section]
The Dark Master, The Dark Hordes (Unofficial version), Return of the Legion , Eternal Kingdom, An Elvish Scout,Unrest in Elfland , Hidden War ...
The Dark Master Project, Arabic tra. maintainer
"But he loves you" G. Carlin
The Dark Master Project, Arabic tra. maintainer
"But he loves you" G. Carlin
Re: handling value in postshow
If you want to change music only on the currently active client, then you do not need anything related to sync, Lua by default runs on every applicable client independently.
You get return value when you show dialog, not in postshow https://github.com/ProditorMagnus/Royal ... C4-L225C60
You get return value when you show dialog, not in postshow https://github.com/ProditorMagnus/Royal ... C4-L225C60
- hermestrismi
- Posts: 626
- Joined: February 6th, 2016, 11:28 pm
- Location: Tunisia
- Contact:
Re: handling value in postshow
thank you for explanation. but I d like to understand the concept itself. how the value is usually handled and how is it synchronized before the ending of the dialog itself
The Dark Master, The Dark Hordes (Unofficial version), Return of the Legion , Eternal Kingdom, An Elvish Scout,Unrest in Elfland , Hidden War ...
The Dark Master Project, Arabic tra. maintainer
"But he loves you" G. Carlin
The Dark Master Project, Arabic tra. maintainer
"But he loves you" G. Carlin
- hermestrismi
- Posts: 626
- Joined: February 6th, 2016, 11:28 pm
- Location: Tunisia
- Contact:
Re: handling value in postshow
I used:
but it seems not synchronized (the first choice remain the same when reopen the dialog and choice another track)
Code: Select all
local button_pressed = gui.show_dialog(dialog3)
local selected_track_details = wml.variables.selected_track_details
if button_pressed == 1 then
wml.variables.selected_track = selected_track_details
-- the selected track to the scenario music
-- wesnoth.play_music(selected_track_details) to play the selected track
else
wml.variables.selected_track = 2
end
wesnoth.redraw {}
end
The Dark Master, The Dark Hordes (Unofficial version), Return of the Legion , Eternal Kingdom, An Elvish Scout,Unrest in Elfland , Hidden War ...
The Dark Master Project, Arabic tra. maintainer
"But he loves you" G. Carlin
The Dark Master Project, Arabic tra. maintainer
"But he loves you" G. Carlin