Register for your free account! | Forgot your password?

Go Back   elitepvpers > Popular Games > Metin2 > Metin2 Private Server > Metin2 PServer Guides & Strategies
You last visited: Today at 13:40

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[Lua-Guide] short if statements (tenary operator)

Discussion on [Lua-Guide] short if statements (tenary operator) within the Metin2 PServer Guides & Strategies forum part of the Metin2 Private Server category.

Reply
 
Old   #1

 
elite*gold: 0
Join Date: Mar 2013
Posts: 2,449
Received Thanks: 6,446
[Lua-Guide] short if statements (tenary operator)

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:
Code:
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:
Code:
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:
Code:
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: Hatte keine Lust es nochmal in's Deutsche zu übersetzen
Lord iRemix is offline  
Thanks
15 Users
Old 02/18/2014, 19:29   #2
 
DasSchwarzeT's Avatar
 
elite*gold: 399
Join Date: Sep 2011
Posts: 5,323
Received Thanks: 3,937
Ist ganz nützlich

Danke dir!
DasSchwarzeT is offline  
Thanks
1 User
Old 02/18/2014, 19:34   #3
 
nybu's Avatar
 
elite*gold: 0
Join Date: May 2011
Posts: 2,806
Received Thanks: 8,536
Sehr schön erklärt, verstehe sogar ich :P
Aber is das ncht eher was für den coders den? :/
Bay the way, will you teach me some of your good english to do?
nybu is offline  
Old 02/18/2014, 19:52   #4
 
DasSchwarzeT's Avatar
 
elite*gold: 399
Join Date: Sep 2011
Posts: 5,323
Received Thanks: 3,937
Quote:
Originally Posted by Lord Nybu View Post
Sehr schön erklärt, verstehe sogar ich :P
Aber is das ncht eher was für den coders den? :/
Bay the way, will you teach me some of your good english to do?
Der erste Teil hat mit Metin2 nichts zu tun, da haste Recht. Das Anwendungsbeispiel wird vorallem in Metin2 etwas bringen
DasSchwarzeT is offline  
Old 02/18/2014, 20:46   #5
 
.Risan.'s Avatar
 
elite*gold: 30
Join Date: Jul 2010
Posts: 1,627
Received Thanks: 1,450
Und trotzdem machen wir alle.. if, eselif... xD
Aber deine Methode ist mir bekannt, wende sie aber irgendwie nicht an.. ka warum
.Risan. is offline  
Old 02/18/2014, 21:46   #6
 
elite*gold: 198
Join Date: Mar 2011
Posts: 835
Received Thanks: 263
Grundlegend eine gute Sache, programmiere zwar nur C++ und kein Lua aber ich verwende

Code:
foo(a>b ? a : b);
so gut wie nie da der Code 3 Monate später viel einfacher zu Lesen ist wenn man

Code:
if( a > b ){
    foo(a)
} else {
    foo(b)
}
verwendet.
ƬheGame is offline  
Thanks
3 Users
Old 02/18/2014, 21:54   #7
 
elite*gold: 2
Join Date: Jun 2013
Posts: 1,063
Received Thanks: 1,724
Man kann immer alles iwie kuerzer schreiben, ist nur die Frage wo der Sinn ist?

Spaeter musst du erstmal nochmal durchdenken was du da gemacht hast.
Ich bleibe daher bei if, elseif etc...

Aber dein Tutorial ansich ist super!
ProfessorEnte is offline  
Thanks
1 User
Old 02/18/2014, 21:57   #8

 
iSouli~'s Avatar
 
elite*gold: 258
Join Date: Feb 2011
Posts: 1,405
Received Thanks: 3,502
Ich glaube das ein guter Coder seinen eigenen Style finden muss.
Ich schreibe vieles "etwas" länger, weil es mir einfach so besser gefällt, und ich
damit besser klar komme.
Die kurzen If-Abfragen nutze ich aber, da ich sie für recht praktisch empfinde.
iSouli~ is offline  
Thanks
4 Users
Old 02/18/2014, 22:09   #9
 
elite*gold: 198
Join Date: Mar 2011
Posts: 835
Received Thanks: 263
Hier Mal ein Beispiel:

Langer Code:
Code:
void gotoxy(uint8_t x, uint8_t y){

	uint8_t addr = x;

	if( ( ( x < 16 ) && ( y < 4 ) ) ) {
		if( y == 0 ) {
			sendCMD( 0x80 | (addr+0x00) );
		} else if ( y == 1 ) {
			sendCMD( 0x80 | (addr+0x40) );
		} else if ( y == 2 ) {
			sendCMD( 0x80 | (addr+0x10) );
		} else if ( y == 3 ) {
			sendCMD( 0x80 | (addr+0x50) );
		}
	} else {
		return 0;
	}
}
kurzer code:

Code:
void gotoxy(uint8_t x, uint8_t y){

	uint8_t addr = x;
	( x < 16 ) && ( y < 4 ) ? y == 0 ? sendCMD( 0x80 | (addr+0x00) ) : y == 1 ? sendCMD( 0x80 | (addr+0x40) ) : y == 2 ? sendCMD( 0x80 | (addr+0x10) ) : y == 3 ? sendCMD( 0x80 | (addr+0x50) ) ::;
}
Bei Einfachen Sachen ist das Kurze super aber bei sowas wirds ein durcheinander ^^ und das ist ja auch noch nicht so Komplex.
ƬheGame is offline  
Old 02/18/2014, 23:04   #10


 
.Marcel''s Avatar
 
elite*gold: 100
Join Date: Sep 2009
Posts: 8,143
Received Thanks: 2,763
Quote:
Originally Posted by iSouli~ View Post
Ich glaube das ein guter Coder seinen eigenen Style finden muss.
Ich schreibe vieles "etwas" länger, weil es mir einfach so besser gefällt, und ich
damit besser klar komme.
Die kurzen If-Abfragen nutze ich aber, da ich sie für recht praktisch empfinde.
Aber um seinen eigenen Stil zu finden, sollte man auch nach den vorgegeben Normen Programmieren, sauber und übersichtlich, damit der Code auch noch nach Jahren verständlich ist. Wartungsfreundlichkeit spielt eine große Rolle.
In Script sprachen wie Lua, PHP etc. ist es nicht wirklich wichtig, ob man jetzt if...else if blabla pipapo macht. Aber wenn man in C++/Java o.Ä Programmiert, sollte man auch auf die Speicher nutzung achten. Auch das Dokumentieren des Codes ist sehr wichtig.

btw: Wieso in Englisch? Ist ja ganz gut geschrieben, aber eine Deutsche version wäre auch ganz nett.
.Marcel' is offline  
Old 02/19/2014, 01:05   #11
 
Hermannx3's Avatar
 
elite*gold: 130
Join Date: Jun 2013
Posts: 182
Received Thanks: 27
Ist jedem seins wie er es schreibt Hauptsache es funktioniert am Ende.
ty for release
Hermannx3 is offline  
Old 02/19/2014, 07:13   #12
 
elite*gold: 198
Join Date: Mar 2011
Posts: 835
Received Thanks: 263
Quote:
Originally Posted by .Marcel' View Post
Aber um seinen eigenen Stil zu finden, sollte man auch nach den vorgegeben Normen Programmieren, sauber und übersichtlich, damit der Code auch noch nach Jahren verständlich ist. Wartungsfreundlichkeit spielt eine große Rolle.
In Script sprachen wie Lua, PHP etc. ist es nicht wirklich wichtig, ob man jetzt if...else if blabla pipapo macht. Aber wenn man in C++/Java o.Ä Programmiert, sollte man auch auf die Speicher nutzung achten. Auch das Dokumentieren des Codes ist sehr wichtig.

btw: Wieso in Englisch? Ist ja ganz gut geschrieben, aber eine Deutsche version wäre auch ganz nett.
Wieso nicht in Englisch? Das versteht jeder Programmierer (oder sollte jedenfalls) und die meissten guten Guides sind sowieso in Englisch.
ƬheGame is offline  
Old 02/19/2014, 14:03   #13


 
.Marcel''s Avatar
 
elite*gold: 100
Join Date: Sep 2009
Posts: 8,143
Received Thanks: 2,763
Quote:
Originally Posted by ƬheGame View Post
Wieso nicht in Englisch? Das versteht jeder Programmierer (oder sollte jedenfalls) und die meissten guten Guides sind sowieso in Englisch.
Ich wusste das, dass kommt. *Ich bin ein Hellseher*..
Natürlich bin ich mir bewusst warum das in Englisch geschrieben ist, war nur ein Vorschlag eine Deutsche Version anzubieten...
Ich sollte meine Branche wechseln von IT zu Hellseherei ... aber ok, passt schon.
.Marcel' is offline  
Old 02/19/2014, 14:42   #14
 
elite*gold: 198
Join Date: Mar 2011
Posts: 835
Received Thanks: 263
Quote:
Originally Posted by .Marcel' View Post
Ich wusste das, dass kommt. *Ich bin ein Hellseher*..
Natürlich bin ich mir bewusst warum das in Englisch geschrieben ist, war nur ein Vorschlag eine Deutsche Version anzubieten...
Ich sollte meine Branche wechseln von IT zu Hellseherei ... aber ok, passt schon.
Finde Deutsch halt relativ sinnlos da man so seine Englisch Kenntnisse immer wider aufbessert wenn man englische Guides liest und man in der IT und allgemein Elektronik Branche nicht weit kommt ohne. Die Firma in der ich arbeite ist eine groupe company der Domino (Englische Firma) viel spass wenn du da kein englisch verstehst, immer nur mit "yes" und "no" antworten bringt dich da auch nicht weiter.
Sollte wohl noch anmerken das es eher Elektronik als IT ist -> Graph-Tech AG, based in Oftringen Switzerland is a leading developer and integrator of drop on demand inkjet technology including printers, print engines and controllers and is widely recognised for its industry expertise.
ƬheGame is offline  
Old 02/19/2014, 16:13   #15


 
.Marcel''s Avatar
 
elite*gold: 100
Join Date: Sep 2009
Posts: 8,143
Received Thanks: 2,763
Quote:
Originally Posted by ƬheGame View Post
Finde Deutsch halt relativ sinnlos da man so seine Englisch Kenntnisse immer wider aufbessert wenn man englische Guides liest und man in der IT und allgemein Elektronik Branche nicht weit kommt ohne. Die Firma in der ich arbeite ist eine groupe company der Domino (Englische Firma) viel spass wenn du da kein englisch verstehst, immer nur mit "yes" und "no" antworten bringt dich da auch nicht weiter.
Sollte wohl noch anmerken das es eher Elektronik als IT ist -> Graph-Tech AG, based in Oftringen Switzerland is a leading developer and integrator of drop on demand inkjet technology including printers, print engines and controllers and is widely recognised for its industry expertise.
In dem einen Punkt geb ich dir ja recht, dass man ohne Englisch kenntnisse nicht weit kommt, in dieser Branche. Aber wie gesagt war nur ein Vorschlag..
.Marcel' is offline  
Reply

Tags
lua guide


Similar Threads Similar Threads
FI short cut guide
06/03/2013 - Cabal Online - 3 Replies
i'm just wondering is there a short cut in FI like in other dungeon like RH, S2, etc. that you can finish it and gain DP? if there so, can you tell how to do it? (please excuse my grammar, i don't think if it's right :) )
Maquinas Outpost Short Cut Guide?
03/21/2013 - Cabal Online - 33 Replies
1st Sorry, if this is a wrong post? might as well move it in the Right Section? im finding "Guides Section" but i can`t find any so i Put this here >.< Sory Acid in Advance. now of with may Question: is there any One here know How to Short Cut MO? B`coz i`ve heard its possible. like like a boss that drops a quest item then up you go with thw last 3 Dei Frames.. any One who can Give a Guide on this?
[short GUIDE] setup china server
06/28/2010 - EO PServer Guides & Releases - 3 Replies
Hy, this is my first guide and my English is not so good, so since not so hard. I already have loaded the msg.exe and NPC.exe from cyberghoser1 up, because not connect the original files with the NPC And the ports I have already adjusted. Original Source from 4fcom.net Download the source: china_serverfiles.rar - 4shared.com and unzip it. Load the mysql files in your database. Create a new account or edit the existing ones.
1st rebirth short guide
06/03/2008 - CO2 Guides & Templates - 6 Replies
For some who don't know how to get 1st reborn( everyone should already know though) Heres a QUICK guide to explain it.. To qualify for 1st rebirth you need to be level 120 (water taos - 110) and have a celestial stone After rebirth you will be level 15 again in your chosen profession to reborn to. You will also recieve one of the following items: Poison blade Health point backsword
Real pleveling short guide - screenie will come soon
12/12/2007 - WoW Guides & Templates - 21 Replies
Level--------Plevel---------------Instance Name//XP / hours 10-15/////////60-70/////////////////RFC (VERY FAST)* 15-25/////////60-70/////////////////WC / Deadmines (FAST)* 25-35/////////60-70/////////////////Scarlet Monastery (Awesome Xp)* 35-45/////////70-70/////////////////Zul'Farrak (A wesome Xp)* 45-55/////////70-70/////////////////Black Rock Deep (Insane Xp)* 55-65/////////70-70/////////////////Strat / Scholo (Nice Xp)* Leve= Your level Plevel= Power leveler leve (another...



All times are GMT +1. The time now is 13:43.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.