declare persistent data for ?

You can talk about ManiaScript for ManiaPlanet here

Moderator: English Moderator

Fadden
Posts: 181
Joined: 10 May 2011, 18:21

declare persistent data for ?

Post by Fadden »

Hi,

I tried this code :

Code: Select all

declare persistent Integer[Text] RaidRecords;
RaidRecords["Paris-Dakar"] = 85542;
It works :)

But in most script I see "declare persistent data for something". For example in the TMPlatform script :

Code: Select all

declare persistent BestScore_NbRespawns for Map = -1;

What is the interest of "for ..." in this case ?

Thx
Elle est où la poulette ?
User avatar
steeffeen
Translator
Translator
Posts: 2463
Joined: 14 Oct 2012, 16:22
Location: Germany

Re: declare persistent data for ?

Post by steeffeen »

if you leave out the "for XY" it's equivalent to writing "for This"
This is always the context you're in, like CTmMode, CTmMlScriptIngame, CSmMode, etc.

that "for" part basically says that the Object is carrying the variable you are declaring
this way it will keep being accessible as long as the object is alive
the "persistent" modifier just says that the object should persist it so that it can still be accessed after a restart and a new object of the same type has been created

a

Code: Select all

declare persistent Variable for Map
has a different value than

Code: Select all

declare persistent Variable for User
because they are owned by different objects


similar to that there are the netwrite/netread modifiers

Code: Select all

declare netwrite Variable for UI
can be used to send data between server and client because the UI object synchronizes its owning variables between the two
->

Code: Select all

declare netread Variable for UI
for reading the Variable on the other end


it can be seen as "Key-Value-Coding" which is common in many languages
    Game Mode and Title Pack Creator, Developer, ShootMania-Player & more

    ManiaControl, FancyManiaLinks
    Fadden
    Posts: 181
    Joined: 10 May 2011, 18:21

    Re: declare persistent data for ?

    Post by Fadden »

    Thanks you very much :thumbsup:
    Elle est où la poulette ?
    User avatar
    steeffeen
    Translator
    Translator
    Posts: 2463
    Joined: 14 Oct 2012, 16:22
    Location: Germany

    Re: declare persistent data for ?

    Post by steeffeen »

    steeffeen wrote:that "for" part basically says that the Object is carrying the variable you are declaring
    this way it will keep being accessible as long as the object is alive
    just wanted to add a small note to make that more clear
    if you declare the same variable again later on it will have the same value as earlier

    Code: Select all

    {
       declare Integer Var for This;
       Var = 5;
    }
    ...
    {
       declare Integer Var for This;
       log(Var);
    }
    prints "5"
      Game Mode and Title Pack Creator, Developer, ShootMania-Player & more

      ManiaControl, FancyManiaLinks
      User avatar
      fleo
      Posts: 304
      Joined: 02 Sep 2011, 12:56

      Re: declare persistent data for ?

      Post by fleo »

      And what about the following value? Is it used to initialised with this value or something? Let's take this example to keep with the persistent data ^^

      Code: Select all

      		declare persistent Boolean NadeoElite_PersistentShowRulesReminder for This = True;
      If "= True" is to initialise, I don't get how the persistence is done...maybe only if this variable has no value yet.
      Follow me on my maniaflash. Try the new elitealternative! Feedbacks on this post. :thumbsup:

      ♫ I need αεяø. I'm holding out for αεяø 'til the end of the night. ♫
      User avatar
      steeffeen
      Translator
      Translator
      Posts: 2463
      Joined: 14 Oct 2012, 16:22
      Location: Germany

      Re: declare persistent data for ?

      Post by steeffeen »

      fleo wrote:maybe only if this variable has no value yet.
      exactly
      this statement defines the default value that is assigned if there is no value yet
        Game Mode and Title Pack Creator, Developer, ShootMania-Player & more

        ManiaControl, FancyManiaLinks
        User avatar
        fleo
        Posts: 304
        Joined: 02 Sep 2011, 12:56

        Re: declare persistent data for ?

        Post by fleo »

        Ok thanks a lot :clap:
        Follow me on my maniaflash. Try the new elitealternative! Feedbacks on this post. :thumbsup:

        ♫ I need αεяø. I'm holding out for αεяø 'til the end of the night. ♫
        User avatar
        djhubertus
        Translator
        Translator
        Posts: 1097
        Joined: 09 Jul 2012, 18:30
        Location: Poland
        Contact:

        Re: declare persistent data for ?

        Post by djhubertus »

        Hey, If I want to use array as persistent (Integer[Integer]), how can I init the default value?
        My Gamemodes:
        MP4 - Countdown, Firefight
        MP3 - Reduction, Overload, Flashpoint, Territory, SM Race, Escape
        MP2 - Search & Destroy, Oscillation, Instagib
        User avatar
        maxi031
        Posts: 378
        Joined: 17 Jul 2011, 00:55

        Re: declare persistent data for ?

        Post by maxi031 »

        Just declare it, it is initialized by default i think.
        If you really want some default value go Array[0] = 1, or all values then loop for(i,0,maxNumber){Array = someValue} that is how i did it.
        Or Array= [19, 85, 8, 24 ], or Array = [3 => 1, 2 => 11, 3 =>100, 7=12 ]
        I think there is not fixed arrays just dynamic arrays. So you can grow them anytime you like.
        Last edited by maxi031 on 17 Jun 2014, 12:33, edited 1 time in total.
        My specs:
        MOBO: MB AM3+ 970 Gigabyte GA-970A-DS3P
        CPU: AM3+ AMD FX-8320E
        GPU: Nvidia GeForce GTX750-Ti ASUS 2GB DDR5
        RAM: DDR3 8GB 1866MHz Kingston HyperX
        SSD: SATA3 120gb SanDisk
        OS: Ubuntu 19.04
        TY MICMO
        User avatar
        steeffeen
        Translator
        Translator
        Posts: 2463
        Joined: 14 Oct 2012, 16:22
        Location: Germany

        Re: declare persistent data for ?

        Post by steeffeen »

        an array is empty by default
        so if you want to use an element that doesn't exist: set the default value, otherwise use it

        you might be able to write something like

        Code: Select all

        declare A for Object = [1 => "First", 2 => "Second"];
        but i wouldn't recommend doing so
        especially as changing the default value later to add a new element would most likely lead to an error

        Code: Select all

        declare A for Object = [1 => "First", 2 => "Second", 3 => "Third"];
        log(A[3]); // <- probably error because it's not initialized
        instead:

        Code: Select all

        if (!A.existskey(1)) {
            A[1] = "First";
        }
        log(A[1]);
        i know it's more code but it's safe
          Game Mode and Title Pack Creator, Developer, ShootMania-Player & more

          ManiaControl, FancyManiaLinks
          Post Reply

          Return to “ManiaScript”

          Who is online

          Users browsing this forum: No registered users and 1 guest