GUI Instance name for translation

Started by Mr.T, Wed 23/04/2008 22:28:45

Previous topic - Next topic

Mr.T

Greetings, I have searched about this and couldn't found an answer, to be honest I don't really
know how to name my request.

Is it possible when a game has multiple translations to do let say:

-when the game start it checks the language

-assign the gui with the language selected to an instance name

-using the instance name in the game just like it was a directly selected gui.

By doing this I'm trying to find a way to avoid re-writing for each language the
Code: ags

if (Game.TranslationFilename == "English") { 
   GUIEnglish.Visible = true
} else if (Game.TranslationFilename == "French") { 
   GUIFrench.Visible = true
} etc...


by

Code: ags

(globalscript)
#define GUI instance
if (Game.TranslationFilename == "English") { GUI = GUIEnglish }
} else if (Game.TranslationFilename == "French") { GUI =  GUIFrench }
etc...

(room)
GUI.Visible = true


I hope I was clear enough and it wasn't mentioned before, thank you very much for your help ;)

monkey0506

#1
What you want here is a GUI* also known as a GUI-pointer or pointer-to-GUI:

Code: ags
// top of script
GUI* gMultilingualMenu;

// inside the game_start function
if (Game.TranslationFilename == "English") gMultilingualMenu = gEnglishMenu;
else if (Game.TranslationFilename == "French") gMultilingualMenu = gFrenchMenu;
else if (Game.TranslationFilename == "German") gMultilingualMenu = gGermanMenu;
// etc.

// room script
gMultilingualMenu.Visible = true;


By using a GUI* we don't have to know which GUI we want to use in order to use it! In this instance you shouldn't encounter any null pointer errors, but if you're unfamiliar with pointers, you should know about them.

Basically when you create the pointer (the "GUI* gMultilingualMenu;" definition at the top of the screen), it isn't pointing to anything yet, so it holds the special value null. You can't do anything with a null pointer, doing so will crash your game. To avoid this you can make sure the pointer is non-null with a simple check such as:

Code: ags
if (myPointer != null) { /* do stuff! */ }
else { /* the pointer was null! we can't use it, but we may want to do some exception handling here */ }

Mr.T

Thank you very much Monkey_05_06 I'll try GUI* pointer  ;)

Mr.T

Would it be recommended to check if the instance returns NULL
in the global repeat executed function?

I could assign a default GUI in case it returns NULL but I'm affraid
it could slowdowns too much the game or something like that.

Thank you  ;)

Mr.T

#4
Thank you it works just great, one note thought, it seems Game.TranslationFilename
works only with the compiled game, not the preview game inside AGS.

I'm using AGS 3.0.1 is it normal?

edit: nevermind I need to copy the TRA in the root folder  ;)

monkey0506

In this example the GUI* should never be null, it should always be set in game_start. In fact, just to be sure, when you're making the list of all the languages, you could set up a final else-clause to set the default language:

Code: ags
if (Game.TranslationFilename == "French") gMultilingualMenu = gFrenchMenu;
else if...
else gMultilingualMenu = gEnglishMenu;

Mr.T

Thanks Monkey_05_06, I did as you said the check in the game start,
and I also check in repeat execute loop (we never know  ;D ) if the
pointer == null then there is the default language (English) set.

It works just fine! ;)

SMF spam blocked by CleanTalk