Howto:Persistent Nasal configuration: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
mNo edit summary
m (fix double redirect)
 
(4 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{Stub}}
{{Stub}}
== Objective ==
Demonstrate how to save/load a Nasal hash with variables for serialization/persistence purposes
== Background ==
It is pretty common for more sophisticated Nasal scripts (or even [[Addon]]s) to have some configuration values, i.e. default values and custom settings/configuration
== Problem ==
Conceiving a custom file format and implementing a custom parser can quickly become tedious and redundant.
== Approach ==
{{See also|Howto:Serializing Nasal data structures}}
Thus, we will be using a Nasal hash that is treated like JSON and then use this to serialize/unserialize the data as needed
== Implementation ==


Consider the following setup
Consider the following setup
Line 24: Line 40:


debug.dump(mySettings);
debug.dump(mySettings);
print(
mySettings.less_than(100,200);
);


</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 09:59, 12 July 2020

This article is a stub. You can help the wiki by expanding it.

Objective

Demonstrate how to save/load a Nasal hash with variables for serialization/persistence purposes

Background

It is pretty common for more sophisticated Nasal scripts (or even Addons) to have some configuration values, i.e. default values and custom settings/configuration

Problem

Conceiving a custom file format and implementing a custom parser can quickly become tedious and redundant.

Approach

Thus, we will be using a Nasal hash that is treated like JSON and then use this to serialize/unserialize the data as needed

Implementation

Consider the following setup

Create a file for your settings, save it as $FG_HOME/Nasal/rgatc.config

We will be using a Nasal hash, that is treated as JSON - this has a number of advantages, i.e. you don't need to write a parser for a custom configuration format, and your configuration can even include comments and arbitrary Nasal values, including even functions or objects

{
# you can also add comments
width: 100,
length : 200,
scale: 10,
range: 25,
less_than: func(a,b) {return a<b;},
};

Next, open the Nasal Console, and paste the follwing code:

var filename = "rgatc.config";
var path = getprop("/sim/fg-home") ~ '/Nasal/' ~ filename;
var mySettings = io.load_nasal( path );

debug.dump(mySettings);

print(
mySettings.less_than(100,200);
);