Board index FlightGear Development Nasal

Nasal problem

Nasal is the scripting language of FlightGear.

Nasal problem

Postby SP-NTX » Mon Jul 19, 2021 11:45 pm

I have small problem with nasal.
Im going to make code for counting seconds
Code is not working and output is 1
Code: Select all
var flighttime = 0;
var counttime = func {
    var flighttime = flighttime + 1;
    print(flighttime);
}
 settimer(counttime, 1);

}
Freedom can be frightening if you've never felt it
User avatar
SP-NTX
 
Posts: 212
Joined: Wed Mar 18, 2020 12:14 pm
Location: Western Poland
Callsign: SP-NTX
Version: 2020.4.0
OS: Windows 11

Re: Nasal problem

Postby wkitty42 » Tue Jul 20, 2021 4:25 am

remove that "var" in front of your addition statement... you've already told the script that that variable is a var at the top... doing it again in the routine generally makes a private copy of the variable so each time the function is called, it starts with a fresh private copy of the variable... remove "var" and it should then be using the global one you defined at the top...
"You get more air close to the ground," said Angalo. "I read that in a book. You get lots of air low down, and not much when you go up."
"Why not?" said Gurder.
"Dunno. It's frightened of heights, I guess."
User avatar
wkitty42
 
Posts: 9146
Joined: Fri Feb 20, 2015 4:46 pm
Location: central NC, USA
Callsign: wk42
Version: git next
OS: Kubuntu 20.04

Re: Nasal problem

Postby WoodSTokk » Tue Jul 20, 2021 4:33 am

wkitty was faster then me.
But yes, delete the 'var' in front of the 'flighttime' inside the function.
The keyword 'var' declare a new variable. So its not a private version of the first one, it declares a new variable with the same name.
This lead to hide the variable declared outside of the function.
WoodSTokk
 
Posts: 1077
Joined: Tue Oct 17, 2017 3:30 pm
Location: Milky Way/Sol/Earth/Europe
Callsign: SX-W57
IRC name: WoodSTokk
Version: 2020.4.0
OS: Debian Bullseye

Re: Nasal problem

Postby Hooray » Fri Jul 23, 2021 3:52 pm

Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Nasal problem

Postby Johan G » Fri Jul 23, 2021 6:24 pm

I have heard that a good programming practice is using a limited set of global variables (to save memory), and using them for different things in different parts of the code. ;) :lol:
Low-level flying — It's all fun and games till someone looses an engine. (Paraphrased from a YouTube video)
Improving the Dassault Mirage F1 (Wiki, Forum, GitLab. Work in slow progress)
Some YouTube videos
Johan G
Moderator
 
Posts: 6629
Joined: Fri Aug 06, 2010 6:33 pm
Location: Sweden
Callsign: SE-JG
IRC name: Johan_G
Version: 2020.3.4
OS: Windows 10, 64 bit


Return to Nasal

Who is online

Users browsing this forum: No registered users and 2 guests