Page 1 of 1

[Solved] Persistent var between two manialinks ?

Posted: 19 Sep 2016, 17:00
by BusardCendre
Hi all,

I'm actually working on a little title pack
and I have a little problem : I need to get a variable from a manialink to another ...
In fact, in a custom solo mode,
according to the guy you choose in the mappers page,
the maps page will be build with his maps.
I'm not sure you understood, if not : just remember I need a persistent data between two manialink ;)

from what I saw here
https://forum.maniaplanet.com/viewtopic ... 89#p271789
and here
https://forum.maniaplanet.com/viewtopic ... 45#p268945
it seems to be possible ...

here is what I've written in first file :

Code: Select all

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<manialink version="2">
    <label id="Busard" text="Busard" scriptevents="1" manialink="file://Media/Manialinks/TrackMania/VioK_Tests/VioK_Maps.xml"/>
    <label id="KriTtPen" text="KriTtPen" scriptevents="1" manialink="file://Media/Manialinks/TrackMania/VioK_Tests/VioK_Maps.xml"/>

<script><!--	
	#RequireContext CMlScript	
	***StartManialink***
	***
	declare persistent Text Persistent_VioKTM_ChosenMapper for LocalUser;
	***
 	
	***Yield***
	***
	foreach (Event in PendingEvents) {
		if (Event.Type == CMlEvent::Type::MouseClick) {
			if (Event.ControlId == "Busard") {
				Persistent_VioKTM_ChosenMapper = "Busard";
			}
			if (Event.ControlId == "KriTtPen") {
				Persistent_VioKTM_ChosenMapper = "KriTtPen";
			}
		}
	}
	***

   main() {
	---StartManialink---						
	    while (True) {
		    yield;
        	---Yield---					
        }
    }
--></script>
</manialink>

And in the second file :

Code: Select all

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<manialink version="2">
<script><!--		
    #RequireContext CMlScript 

    #Const C_MapperNames        ["Busard", "KriTtPen"]   // short list for tests =)

    ***StartManialink***
    ***			
        declare persistent Text Persistent_VioKTM_ChosenMapper for LocalUser;
        if (Persistent_VioKTM_ChosenMapper != "") {
            G_ChosenMapper = Persistent_VioKTM_ChosenMapper;	
            foreach (Mapper in C_MapperNames) {
                if (G_ChosenMapper == Mapper) {
                      DisplayMainPage();
                }
            }
        }
    ***
    ***Yield***
    ***
    // thing to be done when choosing a map ...
    ****

    DisplayMainPage() {
        // things to display according to mapper's name
    }

    main() {
        ---StartManialink---						
             while (True) {
                yield;
                ---Yield---					
            }
   }
--></script>
</manialink>
I know I could do only one manialink instead of two to get the same result ...
but I think I will need later to get another persistent variable between two manialniks, so ...

Thanks for help =)

Re: Persistent var between two manialinks ?

Posted: 19 Sep 2016, 22:34
by maxi031
Persistent data is saved on client pc inside ScriptCache file, so any data you save with it will be client bound and can be reused across multiple manialinks.

Re: Persistent var between two manialinks ?

Posted: 20 Sep 2016, 17:53
by Dommy
Declaring variable for LocalUser should make it disable in any manialink. Declaration for Page will only work per single manialink url.

Re: Persistent var between two manialinks ?

Posted: 20 Sep 2016, 20:21
by BusardCendre
Thanks a lot !!
I found it =)

the problem was I had a manialink attribute in my <label>.
When the label was clicked,
the "OnMouseClick" section of the script wasn't working,
I saw it with adding a log in it ;)

a strange thing is that :

Code: Select all

OpenLink("file://Media/Manialinks/TrackMania/VioK_Tests/VioK_Maps.xml", CMlScript::LinkType::Goto);
doesn't work in Interface Desiger, but works fine in the title pack.

Declaring variable for LocalUser is the only way I found to make it work in Interface Designer :
CGameManialinkPage doesn't support declaration of Persistent attribute for Page
and CGameManialinkScriptHandler doesn't support declaration of Persistent attribute at all.

Dommy, you're really a magician !!
this time you solved my prob without telling me where the error was. :mrgreen:

Re: [Solved] Persistent var between two manialinks ?

Posted: 20 Sep 2016, 22:48
by Dommy
Pro tip: never use interface designer

Re: [Solved] Persistent var between two manialinks ?

Posted: 21 Sep 2016, 11:37
by Nerpson
Pro tip+: Never use the interface designer if you do not edit the xml after.

Re: [Solved] Persistent var between two manialinks ?

Posted: 21 Sep 2016, 13:25
by BusardCendre
I really find Interface Designer very usefull :
to see exactly what will look the screen, and to quickly know if I well understood how to write some code.
I know it has some kind of bugs,
I think that the biggest is that ManiaPlanet crashes and close when we forget some part of code :
as I don't know all code by heart, I often need to search in other files or documentation,
it's often at this moment that I loose what I hadn't saved ...
but I think it's not a so bad thing :
the more I write and rewrite some code, the more I remember ;)

Nerpson, could you explain why I should edit the xml file after writing it with Interface Designer ?
the only strange thing I found is that it add some empty lines at the beginning of the file each time I save the file

Re: [Solved] Persistent var between two manialinks ?

Posted: 21 Sep 2016, 14:56
by Nerpson
I do not use the export functionality in it, I always copy/paste in my text editor.
You should always edit the output xml because after some time spent in editing your file, you'll find some extra spaces in tags.
In addition, attributes are not sorted, I prefer having all tags with attributes in this order:

Code: Select all

<tag id="..." class="..." posn="..." sizen="..." scale="..." halign="..." valign="..." style="..." substyle="..." etc />
So to recap, I'm designing UIs in this tool, but I always review the output xml.

Re: [Solved] Persistent var between two manialinks ?

Posted: 22 Sep 2016, 19:13
by BusardCendre
Thanks

I manually write ids, classes and datas attributes at the begining of tags too =)
but I haven't noticed any extra spaces yet