ID:117139
 
Alright guys. I know you're new. I know you're just learning the language. I know you want to create that Mermaid Melody game (Who wouldn't?!!). And I know you didn't bother to read the guide, nor the other resources that people have pointed you towards.

I'm not gonna lie, you're shooting yourself in the foot on that one. But I am going to offer some help. So before I do anything else, here's one more link to the guide.

http://www.byond.com/docs/guide/
http://www.byond.com/docs/ref/

I even added the reference. I'm such a nice person! Now back to business. (I know that you're probably gonna skip the two links anyway)

So what's this post about Artemis? What is the whole business going on behind the scenes? Well, in an effort to stem silly mistakes and silly questions, I am going to explain the code that you're probably going to be using quite a bit in your Mermaid Melody game!

Disclaimer: I am in no way perfect. I too have made many a silly mistake and asked a silly question.

1. if()

F1 says

Format:
if( E ) Statement1
else if( E2 ) Statement2
else Statement3
If the expression E is true (non-zero) then execute Statement1. Otherwise, test E2, etc. Finally, if none of the expressions are true, execute Statement3. The else nodes are all optional.

That might be a little above your understanding. So I'm going to simplify it for you.

The if statement is a binary expression. The smarter ones will go, "I see what you did thar" the less fortunate will ask, "What does binary mean?" When I say binary, it just means that it can return one of two values. 1 or 0. It's either true (1) or it isn't (0). Time for examples. (Obligatory code for those who don't read text!)

if(src.Age >=30)
src << "You are old!"
else
src << "You are young!"


When the game gets to the if statement, the game will check the age of the source. If it's true (1) it will tell the source that it is old. If it's age is below 30, it will skip telling the source that it is old (Because it isn't) and instead, do the else part of the code. You can combine these ifs and elses too! Observe
if(src.Age >= 30) // If the source's age is greater than or equal to 30
src << "You are old!"
if(src.Age >= 60) // If the source's age is greater than or equal to 60
src << "You are retired, your life is now over"
else // If their age is less than 30
src << "You are not old!"
if(src.Age <= 5) // If their age is less than or equal to 5
src << "You are probably in diapers!"


See? It's not that difficult to tab over to use more ifs.

"But Artemis! What if I want to check for more than one thing at a time?! What if I want to check if my sister has Swine Flu OR Rabies?"

Well I'm glad you asked! There are a ton of operators to do things like that! One that you are probably familiar with is the += operator! But you want to check if a value is one of two values. Well, that's not hard at all! Thanks to the || (or) operator! Check it.

var/mob/M = src.Sister // Define M to be the sister
if(M.Disease == "Swine Flu" || M.Disease == "Rabies") // If your sister's Disease variable is either Swine Flue or Rabies
src << "Your sister aint clean. Avoid her!"
else // If your sister has neither Swine Flu nor Rabies
src << "Your sister is clean. I'd avoid her anyway, just to be sure."

What the || (or) operator does is if either of the statements are true, return 1. So if your sister has either Swine Flu or Rabies, return 1. If she has both, return 1. If she has neither, return 0.

"Oh that's real nifty Artemis. But what if I want to check if a person's salary is between two values so I can place them as middle class?"

Coincidentally, there's an operator for that too! It's the && (and) operator! And you'd use it the same way you'd use the previous operator. Only you need to remember to place the variables in properly.

WRONG WAY
if(src.Salary <= 100 && >= 30)

Putting it in like that will give you a "Missing left hand argument to >="

RIGHT WAY
if(src.Salary <= 100 && src.Salary >= 30)


And that's just something for you to remember.

There are tons of operators, they're the first things in the topic list in F1. Check it out sometime. You might find something you can use to vastly reduce your code length.

"Okay. That's nice and all. But I keep getting this problem with very long code of if statement after if statement. Is there a way that I can shorten it?"

Yes there is! You're looking at switch() Here's what F1 says.

Format:
switch(E)
if(A1,A2,...) Statement1
if(B1,B2,...) Statement1
else Statement3
The "switch" instruction is a compact notation for a lengthy "else-if" chain. The expression E is compared to the values A1, A2, B1, B2, etc. When a match is found, the following statement (or code block) is executed. An optional "else" statement is run if no match is found. Once a matching switch condition is found, no further conditions will be tested.

The values A1, A2, etc. must be constants. As a convenience, a range of values may be specified in the form: A1 to An.

(For the sake of length, I've left out the last paragraph as it only explains efficiency)

So reading this, we see that we can use switch to return a value.

mob
var
Age = 29 // Almost old

mob
proc
Check_Retirement()
switch(src.Age) // This will return 29
if()
if()
else


So the switch() will return 29. Then we adjust our if statements and else to do stuff depending on the age. Simple enough right? I'll edit it a bit to show an example.

Note: Edited because MFC noticed that I had made in error in my quick typing. You're supposed to us "to" for ranges. (A1 to An)

mob
var
Age = 29 // Almost old

mob
proc
Check_Retirement()
switch(src.Age) // This will return 29
if(0 to 5)
src << "You're still in diapers!"
if(5 to 15)
src << "You never even got a girlfriend!"
if(16 to 30)
src << "You're a long way off buddy"
if(30 to 60)
src << "Getting there..."
else
src << "You're already retired! Did you forget?"


It's just a way to lessen the tabbing for a dozen if statements.

"Okay, I get all that. But what about making a fancy textbox popup? How do I do that?"

Oh that's just the input() procedure my little friend!

Format:
input(Usr=usr,Message,Title,Default) as Type in List
Returns:
User's response.

As you can see, this is a little different from the other things that I've explained. It has multiple args.

The first arg is usr by default. It is what the input goes to. The second arg is the message. It's what you generally use to tell the person what they're inputting. Title is what will go in the blue bar above the popup. Default is the default value, what will be returned if nothing is put in.

as Type in List is a little different.

as Type means that we want a specific type of thing. In List is a list that you can pick from. For the name deal, we just want something simple.

src.Name = input(src,"What do you want to call yourself?","Name","Bob") as text


That just defines the Name variable to be whatever the person inputs which must be text. If they put nothing in, their name becomes "Bob" (Because I put "Bob" as the default). You can also utilize this input to send to other people!

mob
verb
Marry(mob/M in world)
if(M.client)
switch(input(M,"Will you marry [src.Name]?","WILL YOU MARRY ME?") in list ("Yes","No"))
if("Yes")
else


It's a little different from what you're probably used to. But I'll walk you through it.

When you do the verb, you'll be asked to select a mob in world. If the mob has a client (No marrying NPCS!) the input() will be sent to M because M is the first argument of the input() Then they will be asked to select either Yes or No. The switch() will return that value ("Yes" or "No") and then we do if statements to check it. If they chose "Yes", it will do the "Yes" if. If not, it will do the else.

Note: Just like in calculus, there is no "no way" to solve a problem. There are many ways to code the same thing (with varying AIC, but I won't discuss that)

"Ohhh, I see... But that makes a list pop up. I want buttons. How do I do buttons?"

Well you can handle that with skin options, but the pure code way is with the alert() proc.

F1 says:

Format:
alert(Usr=usr,Message,Title,Button1="Ok",Button2,Button3)
Returns:
Selected button

This is similar to input in that you can leave out the first arg if you want to. (Instinctually, I always add it...) But now the fourth, fifth, and sixth arguments are buttons. (If you got ahead of me you'll notice that there is a max of three buttons.)

It works almost exactly like input, only now the last three arguments will pop up as buttons. However, alert is not really good all by itself. You generally want to either use switch() with it (as I used switch() in the marriage example) or define the return to be a variable, which can be done quite easily using this.

var/X = alert()


Nothing too hard right?

"That's great and all, but what about bombs? How do I have a countdown?"

So you want a countdown huh? Well that's no problem. Just have a countdown procedure! And this time, we're going to use the while() proc.

F1 says:

Format:
while( E ) Statement
If E is true (non-zero) execute Statement. Continue testing E and doing the while block until E becomes false (zero).

Statement may be a block of code or a single statement

It's pretty simple. While the arg of the while() is true, do what's after the while. When it's no longer true, go down the list.

mob
proc
Countdown(X) // Countdown proc - X is a time value in ticks (1/10 second)
while(X) // While X is non-zero (Which will be greater than zero in this case...)
sleep(1) ; X -- // Sleep a tick, subtract 1 from X. Note that I used the semi-colon (;) which lets me put multiple lines in one!
src << "Countdown over - Kaboom!"


When X finally reaches 0, it will do the output proc (Because it is not tabbed after the while())

The last thing I need to mention is the for() proc which is an easy way to handle multiple objects.

for(var/obj/O in src.contents)
O.InContents = 1


Every object in the source's contents will be defined as O. Then you modify all of the object's "InContents" variables at once. It's pretty convenient.

These are the main things that you are going to be using in your code. I've tried to lay them out as simply as I can. Again, if you are new, I strongly recommend reading the guide but if you can't do that, at least use F1. (It's your best friend!)

One last note, when coding, you need to think like a computer. Here is a diagram of how lines will be read and in what order.

first
second
third
fourth
fifth
sixth
seventh
eighth
ninth


Try to remember that. And with that, I let you get back to your coding. Have fun!

if() doesn't take expressions like that while under switch() and your code won't compile. It only checks constants, not variables. If you're trying to switch() a range, it's best to use the to proc.

mob
var
Age = 29 // Almost old

mob
proc
Check_Retirement()
switch(src.Age) // This will return 29
if(0 to 5)
src << "You're still in diapers!"
if(6 to 15)
src << "You never even got a girlfriend!"
if(16 to 30)
src << "You're a long way off buddy"
if(31 to 60)
src << "Getting there..."
else
src << "You're already retired! Did you forget?"
Fixed. Stupid mistakes keep happening to me :(
Here is a nice basic CSS by Dark Campainger. It's easily modified and looks alot nicer than the basic white =)
Thanks!
No prob =D
Keep the post coming!
yea+

:D very nice