VGUI refresh ways.

Hello. Im searching way to refresh all panels. Im making language system(like in TTT) where player choses language. So, how i can refresh VGUI? I mean, how to refresh variables such as panel title or button text.

It’s automatic when you save a file. TTT Isn’t the absolute best in terms of auto-refresh capability.

Just ensure you have auto-refresh enabled which it should be by default, then edit the files using Notepad++ or if you’re using sublime then disable the symlink system where it saves in a different location then copies it over the other.

Additionally, instead of wasting time on a system like TTT; build it off of the original language system. What I mean by that is just add a new argument to language.AddPhrase / GetPhrase and store them in local dictionaries. That’s what I did; I’m actually considering uploading it to gmod so we don’t need a C based language system.

I think you misunderstood him. I believe he was asking how to refresh a panel displaying a variable that was changed in-game, not by editing the file.

Every time user changing clientconvar it should save file?
Maybe you haven’t understand the question.
There code for example


MixturesButton:SetText(LocalPlayer():GetQuote("mixtures"))

GetQuote returns string of translation. When im changing language it returning other translation but text stay the same

You’re right, my bad.

To update the text you’d need to go through all of the vgui and manually update them. Luckily all panels are parented to vgui.GetWorldPanel( ) meaning you can recursively traverse and look for PANEL:GetClassName( ) DLabel and update text.

An easier way would be to only store the language variable in the text segment, then on paint of the panel, translate it and show it there; if you do it like that then when you change the language all text will update too!

[editline]9th March 2014[/editline]

I’d recommend using just #mixtures or so for SetText, then change the DLabel paint function to do exactly what it did before except one additional step of getting the translation and applying it.

You could even cache the translation and update it when the language variable has changed by setting a panel.__cachedLanguage variable to something, do an if statement to see if the current language is different, if so get the new text and draw the text.

Is that your idea?


for k,v in pairs(vgui.GetWorldPanel():GetChildren()) do
if v:GetClassName() == "DLabel" then
v:SetText(v:GetText())
end
end

or make all panels im using use paint function for draw it?(bad at english. sorry)

There’s an easier and more reliable way:

For each of those panels, store a variable which is a table = { language = cached_language_name, text = translation_string_such_as_mixtures_in_quotes } and create a PANEL:LanguageUpdated callback hook. Then do a hook.Add( “LanguageChanged”, “MyPanelUpdateLanguage”, PANEL.LanguageUpdated );

When the language is changed do hook.Call( “LanguageChanged”, GAMEMODE, “new_lang” );

If you’re using cvars, there is a gameevent.Listen you can create for cvar changed which can be used by the client. If it’s your lang cvar which updates, do the call from there.

I need to update this to include it, I’ll do that in a bit, but this shows how to use game event listeners.
https://dl.dropboxusercontent.com/u/26074909/tutoring/game_event_listeners.lua

for the function PANEL.LanguageUpdated( self, new_lang ); to make it easier you could use:

hook.Add( “LanguageChanged”, “MyPanelUpdateLanguage”, function( new_lang ) PANEL:LanguageUpdated( new_lang ) end );

Then you only need to have it call SetText for the label in the panels once it updates.


//Translation Script Code
cvars.AddChangeCallback( "convar name", function( convar_name, oldValue, newValue )
 hook.Call("LanguageChanged")
end )
//EveryPanelScript
hook.Add("LanguageChanged", "uniqueshit", function() PANEL:SetText(PANEL:GetText()) PANEL:SetTitle(PANEL.Title) end)

Correct?

Almost; for the hook.Call you will need to supply GAMEMODE in addition to an argument with the newvalue.

[lua]hook.Call(“LanguageChanged”, GAMEMODE, newValue)[/lua]

For each of the panels, you would need to create a new function:
[lua]function PANEL:LanguageUpdated( newValue )
// Here is where you’d update the language for the dLabels
// Remember, when you create a label, or anything with text in it, create some way to identify each element which needs to be updated with the language phrase name, language, even a link to the panel would help.
// If you have that table inside a huge vgui panel you created, you could simply do a for loop and update each panel that way, OR you’d put these in the lowest level panel such as DLabel and just update one
end[/lua]

Inside the panel code right under the above function, you’d need to add a callback link to run that function and pass in the argument:
[lua]hook.Add(“LanguageChanged”, “uniqueshit”, function( newValue ) PANEL:LanguageUpdated( newValue ) end)[/lua]