File: prefs.md

package info (click to toggle)
surgescript 0.5.4.4-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,876 kB
  • sloc: ansic: 13,674; makefile: 16
file content (90 lines) | stat: -rw-r--r-- 1,869 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
Prefs
=====

Prefs is a SurgeEngine component used to save/load data to/from the disk (permanent storage). Prefs is a (key, value) storage that works like a [Dictionary](/reference/dictionary), but its data can be persisted between game sessions.

*Example*
```
using SurgeEngine.Prefs;

// Run the Application multiple times and see
// how the counter increments
object "Application"
{
    state "main"
    {
        Prefs["counter"] += 1;
        Console.print("counter: " + Prefs["counter"]);
    }

    fun constructor()
    {
        if(!Prefs.has("counter"))
            Prefs["counter"] = 0;
    }
}
```

Functions
---------

#### get

`get(key)`

Gets the value of the specified key. Instead of calling `get()` directly, you may equivalently use the `[ ]` operator.

*Arguments*

* `key`: string. The key must always be a string.

*Returns*

Returns the value associated with the specified key, or `null` if there is no such value.

#### set

`set(key, value)`

Sets the value of the specified key. Instead of calling `set()` directly, you may equivalently use the `[ ]` operator.

*Arguments*

* `key`: string. The key must always be a string.
* `value`: any type. The value you want to store.

#### clear

`clear()`

Removes all entries from Prefs. Use this very carefully, because data will be lost.

#### delete

`delete(key)`

Deletes the entry having the specified key.

*Arguments*

* `key`: string. The key of the entry to be removed.

#### has

`has(key)`

Checks if a specific entry exists.

*Arguments*

* `key`: string. The key of the entry.

*Returns*

Returns `true` if the specified entry exists, `false` otherwise.

#### save

`save()`

Writes the data to secondary storage. The data isn't saved immediately after you set an entry, but it is saved on key moments. Since the data is persisted automatically, normally you don't need to call this.