Nasal Hello World: Difference between revisions
Jump to navigation
Jump to search
m (Link to special directory articles) |
m (+canvas example) |
||
(8 intermediate revisions by 4 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. | |||
To show a message inside the FlightGear window using a [[Tooltips|GUI tooltip]] instead, use the following snippet: | |||
<syntaxhighlight lang=" | <syntaxhighlight lang="nasal"> | ||
gui.popupTip("Hello, World!"); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
To use a [[Canvas]] GUI dialog, try the following: | |||
<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=" | <syntaxhighlight lang="nasal"> | ||
# hello.nas | |||
print("Hello\nWorld!"); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Double quotes support typical escape sequences: | Double quotes support some of the typical escape sequences: | ||
* \n Newline | * \n Newline | ||
* \t Horizontal Tab | * \t Horizontal Tab | ||
* \r Carriage Return | * \r Carriage Return | ||
* \\ Backslash | * \\ Backslash | ||
* \" Double quote | * \" Double quote | ||
* \x''yy'' Hexadecimal-specified character (yy stands for two hexadecimal digits; \x is literal) | |||
For example, to print a new line, use: | For example, to print a new line, use: | ||
<syntaxhighlight lang="nasal"> | |||
print ("\n"); | |||
</syntaxhighlight> | |||
To print a quoted string, use: | To print a quoted string, use: | ||
<syntaxhighlight lang="nasal"> | |||
print ("\"quoted string\""); | |||
</syntaxhighlight> | |||
and so on. | and so on. | ||
Line 48: | 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
The FlightGear forum has a subforum related to: Nasal Scripting |
Nasal scripting |
---|
Nasal internals |
---|
Memory Management (GC) |
In the programming world, a "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):
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);
});