Skyline Game Engine Store uses cookies to store certain information to make your site visit more enjoyable. By proceeding to use the store, you are agreeing to the use of cookies.

Artists Guide to Lua Part-2

Posted by SolarPortal 22/01/2018 0 Comment(s)

Ready for the next installment of the Lua introduction tutorial, "Oh no not more learning" I hear you say. A few more basic bits of information then we can just dig in and get our hands dirty with some practical examples and get you making dynamic Skyline scenes.

All this information will be repeated over and over again in the upcoming practical examples, but don't worry about terminology as it's more important to know how to use the features and what they do rather than know what they are called.

Variables, Functions, Events, Conditions and Loops

Variables, Functions, Events, Conditions and Loops will be the next important script features we must quickly look at. Dont worry we wont be going into much depth, just covering what you will need to know to get going. As with all things code these features can get very deep and powerful but this is not always necessary, so without further ado:

The Variable:

Variables: A variable is a container to hold the value that is returned by calling a command. From part-1 we mentioned the analogy of picking apples. Using our pseudocode to represent the actions of  picking the apples: tree.getApples() and then placing these apples in a box. Well the box in this case is our variable. A variable can be any word or letters+number(apple1), but not numbers or number+letter(1apple).

Variable  = command();
box1       = tree.getApples();

When we want know how many apples are inside our box we just need to open it and see what is stored inside , or in the case of scripting we look at the value stored in the variable. We will look at how to read a stored value in the next exciting installment!.

The Function:

Function: Simply put a container for many other commands. Remember the commands we talked about in the part-1, imagine if you wanted to call many commands over and over. It would take many lines of repeated script but by using a function you can put it all neatly inside and use the name of the function as a command when you want to run the repeated commands.

Example: Don't worry about remembering or full understanding the following example as you will see this over and over in the practical examples in future tutorials, so here we go.....
treeAppple = tree.getApples()
treePear      = tree.getPears()
treePlum     = tree.getplums()

OR set up a function like so:
    function treeFruit()    
        treeAppple = tree.getApples()
        treePear      = tree.getPears()
        treePlum     = tree.getplums()


and just call the function treeFruit(); when you need to get the tree's fruit. Functions can also return you a vale in the same way as a command but don't worry about this for now as we will cover this later.

The Event:

An Event is a special function that differs to the normal function in that Skyline calls the function in relation to some task the engine has performed. There is a huge list of special event functions that are specific to Skyline and these can be seen in the Skyline Lua API. We will take a closer look at the events as and when we need them, so for now just know they are there.

Skyline has been designed from the ground up as an event driven game engine. This means that no dynamic operation will take place until something happens in the form of an event, having the engine work this way makes Skylines scripting very performance friendly as no CPU cycles are wasted when not doing anything.

An example of an Event trigger is the computer Keyboard. Every time a key is pressed Skyline fires off a keyDown Event and when the key is released Skyline calls a keyReleased event. In your Lua script you set up a function called onKeyDown(keyPressed) and onKeyUp(keyPressed) which gets called on the key event. In the next chapter we will look at a practical example of using Events.

The Condition:

I know, way too much reading but trust me it will be worth it when you are playing with you carefully crafted art in a fully dynamic 3D scene. The Condition is a way you can check the relationship between the contents of two variables. To keep it simple "Does the contents of value1 equal the contents of value2" or  "Is the contents of value1 different to the contents to value2" and if this is the case then do something with the code.

if( value1==value2 ) then      
    Do something interesting ....


You will notice that the condition is written with a specific syntax starting with the word "if" then followed by the "(condition)" then the words "then" and "end" are just part of the way Lua does this condition. One more thing I must mention is the operators, that is the way we define what type of condition.

EG: == Check for Equality, ie is the contents of the two variables the same.
~= Is the contents of the two variables not equal to each other
< Is the contents of the first variable less than the contents in the second variable
> Is the contents of the first variable greater than the contents in the second variable
<= less than or equal to
>= greater than or equal to

You will get so used to these operators as they will become second nature and again don't worry about remembering, you will use these time and time again and they are here if you need them for reference.

The Loop:

There will be many times where you will need to repeat bits of script, to do this we use a loop. There are various ways to do looping in Lua the most common is the "for" loop. This type of loop will enable you to repeat a bit of code a predetermined amount of times.

EG Syntax: for var1=loop_Start_Value, loop_End_Value, optional_Step_Value do        Do something interesting each pass of the loop.... end

What it would look like in your script:

for i = 0, 10 do    
     Do something interesting each pass of the loop....


There is another loop method called the "while" loop this is a little more complex but you will be using the "for" loop for the purposes of these lessons .

Note: The Skyline API has a section for syntax reference which covers everything we have explained above plus more,  check it out here: Lua Syntax Reference I think that is all of the boring stuff covered, sorry for dragging you through it and well done for getting this far! In the next  lessons we will be moving into the practical fun stuff and getting our hands dirty playing with Skyline ;)

See you in the next blog :D

Leave a Comment