"Hello world!" tutorial

In this tutorial, I won't bother you with long-winded introductions. Our goal will be to render the words "Hello world!" on the screen as quickly and succinctly as possible. When you run "Agenoria.exe", the engine by default tries to compile and execute the Lua script located in "Scripts/main.lua". Therefore, we need to create our own plain text file and save it in the "Scripts/" directory of the engine under the name "main.lua". Next time you click on "Agenoria.exe" the script will be executed automatically. The engine can run script files through the command line as well.

The script

Let's go over the code required for our "Hello world!" example. First, we're going to open a new application window by calling the "create" method in the global display object. We then create a new sprite object. The default constructor of the sprite gives it zero X and Y coordinates. In order to have it rendered, we must add our sprite as a child node to a viewport. There is a default viewport which exists as a property of the global display object. Sprites added as child nodes to the default viewport are positioned relative to the center of the window. Since our sprite is by default located at the origin (0, 0) it will appear right in the center of the window.

Everything is great so far, except that our sprite is completely blank. To write some text on it, we're going to create a new font object. After creating a new font object, we make a call to its "load_system" method. This allows us to use a font that is already installed on the user's operating system. Notice that it is also possible to load True Type fonts directly from file. Once the font has been loaded, we apply it to the sprite's canvas and write the words "Hello world!"

display:create ( "My Window", 800, 600, 32, true )

my_sprite = Sprite ( )
display.viewport:add_child ( my_sprite )

my_font = Font ( )
my_font:load_system ( "Arial", 18 )

my_sprite.canvas:set_font ( my_font )
my_sprite.canvas:write ( "Hello world!" )
Download:  hello.lua

That's everything required for our "Hello world!" application. In just 7 lines of code, we have done what would have taken considerably more effort in a C/C++ environment. To be fair, there are certain things that we did not account for. In particular, the "create" and "load_system" functions could have failed due to invalid parameters or possible hardware issues. Ideally, your script should check the return values of those functions and notify the end user of what went wrong.