Jump to content
Sign in to follow this  
Remix

[How To] Short if statements

Recommended Posts

Hey,

 

because i'm very bored and i don't know what i should do till i'll go to bed i decided to publish a lua guide about short if statements.

 

Maybe you know the short if statements from other languages like C++.

 

Example:

cout << (a == true ? "1" : "2");

At this example the console would write "1" if a would be true.

If a would be false it will write "2".

 

In lua the short if statements are a little bit different but basicly they are very easy.

 

Imagine you want to give a information about a event.

For example if it's running or something else.

 

There are many ways to show the current status of a event.

You can use the long version with a text output two different if/else blocks.

 

 

Another way is to use a table.

 

Example:

say("The event is ".. ({"inactive","active"})[game.get_event_flag("event")])

But it's also to much text if you don't use a special function.

 

So the shortest version for something like this is a short if statement.

 

 

The construction is very simple.

 

 

The short if version of the shown table method is:

say("The event is ".. (game.get_event_flag("event") == 1 and "active" or "inactive"))

On the first view it's maybe a little bit longer but if you'll use it everytime you will see it's shorter to use this method.

 

 

Construction:

 

You have a variable to check the value. (game.get_event_flag("event"))

Now you use the datatype boolean to get true or false (game.get_event_flag("event") == 1).

 

The datatype boolean includes nothing else as a normal if statement.

 

Now you must set two possibilities ("active" and "inactive")

 

If the boolean statement returns true the possibility which was set to "and" will be used. ("active")

But if it returns false the other possibility will be used. ("inactive")

 

 

I hope i helped somebody with this tutorial.

If you have any questions just ask me.

 

Sincerely,

iRemix

 

P.S: Please don't publish this tutorial on elitepvpers. They don't deserve it.

  • Love 8

Share this post


Link to post
Guest

Nice tutorial but consider setting the right syntax highlighting language in your post I want to know what to add.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.