From f66013f0b1dc805ead3b1a6338c981f5352432e9 Mon Sep 17 00:00:00 2001 From: fly Date: Tue, 11 Feb 2020 16:39:41 +0000 Subject: [PATCH] save.nas Signed-off-by: fly --- Nasal/Systems/save.nas | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Nasal/Systems/save.nas diff --git a/Nasal/Systems/save.nas b/Nasal/Systems/save.nas new file mode 100644 index 00000000..927ab4cc --- /dev/null +++ b/Nasal/Systems/save.nas @@ -0,0 +1,57 @@ +# Copyright (C) 2020 Merspieler, merspieler _at_ airmail.cc +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +# Save and restore properties between sessions + +# To add more properties, just add them here: +var saved_props = [ + "/consumables/fuel/tank[0]/level-lbs", + "/consumables/fuel/tank[1]/level-lbs", + "/consumables/fuel/tank[2]/level-lbs", + "/consumables/fuel/tank[3]/level-lbs", + "/consumables/fuel/tank[4]/level-lbs" +]; +var file = getprop("/sim/fg-home") ~ "/Export/" ~ getprop("/sim/aircraft") ~ "-save.xml"; + +var save = func { + print("Saving state..."); + for (var i = 0; i < size(saved_props); i += 1) + { + setprop("/save" ~ saved_props[i], getprop(saved_props[i])); + } + + var saveNode = props.globals.getNode("/save", 0); + + io.write_properties(file, saveNode); + print("State saved"); +} + +var restore = func { + print("Loading saved state..."); + var readNode = props.globals.initNode("/save", ); + + io.read_properties(file, readNode); + + for (var i = 0; i < size(saved_props); i += 1) + { + var val = getprop("/save" ~ saved_props[i]); + if (val != nil) + { + setprop(saved_props[i], val); + } + } + print("Saved state loaded"); +}