Nasal Hello World: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
m (Use Nasal highlighter)
m (+canvas example)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Nasal Navigation}}
{{Nasal Navigation}}


In the programming world, a {{wikipedia|"Hello, World!" program}} is used to illustrate basic syntax using a very simple program. The '''"Hello, World!" program in [[Nasal]]''' would look like this (run this in the [[Nasal Console]]):
<syntaxhighlight lang="nasal">
print("Hello, World!");
</syntaxhighlight>
This will print the string <code>Hello, World!</code> into the console.


A simple hello world example in Nasal would be (to be saved in [[$FG_ROOT]]/Nasal):
To show a message inside the FlightGear window using a [[Tooltips|GUI tooltip]] instead, use the following snippet:


<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
# [[$FG_ROOT]]/Nasal/hello.nas
gui.popupTip("Hello, World!");
print('Hello World!');
</syntaxhighlight>
</syntaxhighlight>


This will show the "Hello World" string during startup in the console window. The hash sign (#) just introduces comments (i.e. will be ignored by the interpreter).
To use a [[Canvas]] GUI dialog, try the following:
 


Note: Script-specific symbols such as global variables (or functions) will be put into a scope (namespace) based on the script's name, scripts embedded via aircraft-set.xml files can separately specify a corresponding module name (see [[Howto: Make an aircraft]] for details).
<syntaxhighlight lang="nasal">
# create a new InputDialog with a title, label, and a callback
canvas.InputDialog.getText("Hello World", "Please enter your name", func(btn,value) {
    if (value) gui.popupTip("You entered: "~value);
});
</syntaxhighlight>


<!--
Strings in Nasal can also use double quotes which support escaping:
Strings in Nasal can also use double quotes which support escaping:
<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
Line 45: Line 57:


Nasal strings are always arrays of bytes (never characters: see the utf8 library if you want character-based equivalents of substr() et. al.). They can be indexed just like in C (although note that there is no nul termination -- get the length with size()):
Nasal strings are always arrays of bytes (never characters: see the utf8 library if you want character-based equivalents of substr() et. al.). They can be indexed just like in C (although note that there is no nul termination -- get the length with size()):
-->

Latest revision as of 15:41, 12 January 2020


In the programming world, a "Hello, World!" program This is a link to a Wikipedia article is used to illustrate basic syntax using a very simple program. The "Hello, World!" program in Nasal would look like this (run this in the Nasal Console):

print("Hello, World!");

This will print the string Hello, World! into the console.

To show a message inside the FlightGear window using a GUI tooltip instead, use the following snippet:

gui.popupTip("Hello, World!");

To use a Canvas GUI dialog, try the following:


# create a new InputDialog with a title, label, and a callback
canvas.InputDialog.getText("Hello World", "Please enter your name", func(btn,value) {
    if (value) gui.popupTip("You entered: "~value);
});