Operation Flashpoint Dragon Rising OFDR Forums
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Statements and how they work.

Go down

Statements and how they work. Empty Statements and how they work.

Post by TheGeneral Mon 17 Dec 2012, 15:53

Statements

A statement can be added to the event you are writing so that the engine can differentiate what is happening. For this we write things like: "if", "elseif", "else", they are basically saying if this happens, do something, but if this happens do something else but if none of that happens do something totally different. This will become more clear when looking at coded missions.
Here is an example:
There are other things in here but for the purpose of statements this should make a bit of sense.



Code:
sky = "bright"
   
       if sky == "bright" then
             OFP:displaySystemMessage("It's day time!")
       elseif sky == "dark" then
             OFP:displaySystemMessage("It's night time!")
       else
             OFP:displaySystemMessage("I don't know if it's day or night")
       end
   

This may seem like a complex bit of code but after using the editor for a while you will eventually grasp it. If you think about the code above too much, do not get frustrated if you cannot make things work right away. This is common among new people to LUA and OFDR. Normally you will find that it is the silly errors that cause you problems more than understanding it, such as leaving out "", forgetting the extra keyword "end" at the end of a function or putting things in capitals where they are not needed.

---------------------------------------

elseif


You can have as many "elseif" statements as you want(in a chunk), they will be prioritsed from top to bottom. So what will happen is the game engine will read the code and look for the first elseif statement that will result in a positive answer or "true" response. It will then take the rest of the elseif statements as a negative answer or "false" response. The chunk of code where the "elseif" can back as true will be the one that is executed in the game.

---------------------------------------

else

If the chunk of code that contains all the if/elseif statements come back as false then nothing will happen unless you want something else to counteract it. This is where an else statement comes into effect. Basically if all your if and elseif statements are false then whatever you code to happen in the else statement at the end, that will happen in game instead.
So looking at that bit of code above, if the sky is bright or dark then we don't know if it is night or day.
If all you want to do in your cod is check whether or not something is true or not and you don't want anything else happening otherwise then the "else" statement is not needed.

----------------------------------------

Endings



We have already established that "end" is a keyword. We use this to finish of a piece of code so that we can move on to the next one. What you may find from time too time is that you cannot test your game because or a LUA corruption message appearing on screen. The Editor will notify you of any errors as you go along but if you think you have not missed anything out and you try to export it, the editor WILL tell you otherwise of any errors.

Like what is mentioned before in the "Chunks" section, every function is to finish with an "end" likewise every statement must finish with an end also.

Lets look at some examples:

Statements and how they work. Ending10

If you look down the left hand edge of the image you will see increasing numbers, these are the line numbers. next to those are plus and minus values. what this does is collapses statements for you so you can see where you are needing an end if you haven't got one.
We'll start at the top, line 19 - 23. this is a whole function without any if statements so it only needs just 1 "end" to finish the whole function.

Line 25 - 40 is the next function. As you can see it is split up with "if" statements and each one has an "end" keyword to it. This is because you are closing that specific statement.

If you would like to put this into practice you can follow a progressive mission that you will make from following the tutorial that has been made. You will encounter the code as you go through it but it also helps you to navigate your way around the mission editor for Dragon Rising.
TheGeneral
TheGeneral
Admin

Points : 780
Reputation : 35
Join date : 2012-12-15

http://maniacaldog.wix.com/keepdralive

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum