-
Your shopping cart is empty!
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).
EG:
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()
end
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.
EG:
if( value1==value2 ) then
Do something interesting ....
end
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....
end
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
21 Comment(s)
Heya i am for the first time here. I found this board and I in finding It truly helpful & it helped me out a lot. I'm hoping to present something again and aid others such as you helped me.
Every weekend i used to go to see this site, because i wish for enjoyment, as this this web site conations in fact fastidious funny information too.
You can certainly see your skills within the work you write. The arena hopes for even more passionate writers like you who are not afraid to say how they believe. All the time follow your heart.
You've made some really good points there. I checked on the internet to learn more about the issue and found most people will go along with your views on this web site.
Thanks for the good writeup. It if truth be told used to be a enjoyment account it. Look complicated to far delivered agreeable from you! By the way, how can we be in contact?
Hello! Do you know if they make any plugins to help with SEO? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good gains. If you know of any please share. Thank you!
Hello, I want to subscribe for this weblog to obtain latest updates, therefore where can i do it please help out.|
I’ll right away grab your rss feed as I can not find your e-mail subscription link or newsletter service. Do you have any? Kindly permit me recognize so that I may subscribe. Thanks.
Ahaa, its nice conversation on the topic of this paragraph at this place at this website, I have read all that, so at this time me also commenting at this place.
I am sure this post has touched all the internet people, its really really pleasant post on building up new weblog.
When someone writes an paragraph he/she retains the plan of a user in his/her mind that how a user can know it. So that's why this paragraph is perfect. Thanks!
You have made some really good points there. I looked on the internet for more info about the issue and found most individuals will go along with your views on this site.
Hurrah! In the end I got a webpage from where I can in fact obtain useful facts regarding my study and knowledge.
There is definately a great deal to find out about this subject. I really like all of the points you have made.
I am sure this article has touched all the internet visitors, its really really good article on building up new weblog.
If you evolve, as an alternative of selecting the Pokemon usually, its white/gold assaults will get +10 and purple attacks get an extra star.
I aam sre this artocle hass touched all thhe nternet visitors, itss rsally realkly fastkdious rticle on building up neww weeb site.
I am sure this article has touched all the internet viewers, its really really fastidious post on building up new web site.
Incredible! This blog looks just like my old one! It's on a totally different topic but it has pretty much the same page layout and design. Superb choice of colors!
I enjoy what you guys are usually up too. This kind of clever work and reporting! Keep up the great works guys I've incorporated you guys to my own blogroll.
Ahaa, its good discussion on the topic of this article here at this weblog, I have read all that, so at this time me also commenting here.
Leave a Comment