Jump to content

  •  





Crash Course: Scripting 101-B

Posted by Mumbles, in Guides 28 January 2014 · 1249 views

scripting guide
I know it's been a while, but hopefully I'll be pushing these out more frequently. This is a continuation to Crash Course: Scripting 101-A, which I wrote back in November. Normally, I would have written it all in one huge post; however, due to complications with our forum software deleting my original post twice, I decided to break down the course in parts. This format should help me space out the time between posts, and focus more intensively on specific areas that might otherwise be undermined.

Anyway, let's get on with it, shall we?


III: Expressions
Logical expressions are often used in scripting to determine a set of rules or prerequisites for a specific event or scenario to happen. These expressions can be used to determine numerous things during gameplay, and can be regularly found in quest NPCs or anything that works around special conditions in order to operate.

Sample script:
mes "[Random Guy]";
mes "Hello there! What's your name?";
next;

input .@my_name$;

mes "[Random Guy]";
if (strcharinfo(0) != .@my_name$) {
	mes "Well, that's odd! Your nametag...hey, come back here!";
} else {
	mes "That's a nice name you've got there!";
}

close;

Here's a situation where some fellow is asking for a player's name. Players are prompted with the input command, storing their answer in a temporary NPC string variable called .@my_name$. In the script, we present the player with a logical expression that determines whether or not they told us the truth by comparing strcharinfo(0) against .@my_name$:
if (strcharinfo(0) != .@my_name$) {

If the if expression is found to be true, the script will proceed to include the code within the curly braces { } after the expression; otherwise, it will run the code in the curly braces after else in its place. However, no matter what message is displayed, a close will be executed afterwards, as it is impartial to the condition set by the if...else expression. Note that it is not required to use else, but it is useful when substituting text or code. Before we move onto the next section, here are some things you might find useful to know:
  • strcharinfo(0) returns the name of the attached player in a string format; if a player's name is Player A, the name will be returned as "Player A".
  • strings are any sequence of characters (letters, numbers, and symbols) that are enclosed in quotation marks.

IV: Concatenation
In formal language theory and computer programming, string concatenation is the operation of joining two character strings end-to-end; in English, this process is commonly known as "sequencing". In *Athena scripting, concatenation is used to piece words and information together dynamically - and by dynamically, I mean with the ability to change with little or no intervention from the scripter.

Sample script:
mes "[Random Guy]";
mes "Hello there! What's your name?";
next;

input .@my_name$;

mes "[Random Guy]";
if (strcharinfo(0) != .@my_name$) {
	mes "Well, that's odd! Your nametag doesn't say "+ .@my_name$ +"!";
} else {
	mes "That's a nice name you've got there, "+ strcharinfo(0) +"!";
}

close;

Let's use the script from earlier as an example. In order to concatenate a string, a plus sign + must trail a closing quotation mark, followed by the string you want to include. If the player input his name as Player B, .@my_name$ will be the equivalent of "Player B" - quotation marks included. Comparatively, the message in the if expression would say:
mes "Well, that's odd! Your nametag doesn't say "+ "Player A" +"!";

Think of concatenating as adding onto an existing string using variables or functions. In this example, strcharinfo(0), is being used to return the player's name as it is stored on the server; if the player's name is Player A, the function will return the string "Player A". Always keep in mind that you are concatenating strings, which must be enclosed in quotation marks to be read by the script parser properly.




Nice one , Keep it up , maybe this will reduce the ammount of " i need aTCG script for my Donations".....

How about to include compact if else statement on this tutorial?

mes "[Random Guy]";
mes "Hello there! What's your name?";
next;

input .@my_name$;

mes "[Random Guy]";
mes "" +(strcharinfo(0)!=.@my_name$?"Well, that's odd! Your nametag doesn't say "+ .@my_name$ +"!":"That's a nice name you've got there, "+ strcharinfo(0) +"!")+ "";
close;

How about to include compact if else statement on this tutorial?

mes "[Random Guy]";
mes "Hello there! What's your name?";
next;

input .@my_name$;

mes "[Random Guy]";
mes "" +(strcharinfo(0)!=.@my_name$?"Well, that's odd! Your nametag doesn't say "+ .@my_name$ +"!":"That's a nice name you've got there, "+ strcharinfo(0) +"!")+ "";
close;

 

This tutorial is aimed at beginners with minimal understanding of code logic; I would find it inappropriate to push them into shortcut ?: operations. My goal is not only to teach basic scripting, but to teach good coding habits as well. You may have noticed that I don't shortcut one-line if statement enclosures (i.e. if (true) end;), nor have I covered goto, custom labels, if-true-else-false statements, etc.; these are all bad coding habits (in my opinion), so I will not teach them.

 

And honestly, your example of a compact if-else statement is hardly compact at all. If anything, it's confusing and cluttered. I would only use a ?: operation when switching between something simple, like day/night, male/female, singular/plural, or other scenarios where a quick switch is more convenient than writing out the same sentence out twice with a minor difference.

 

With that in mind, the difference in the if-else statement's enclosures shown in the example is significant enough to use a normal if-else rather than a ?: operator.

 

Lastly, even if we were to use a ?: operation, we wouldn't need the extra quotation marks or the parentheses enclosure.

 

Even more "compact":

mes strcharinfo(0) != .@my_name$ ? "Well, that's odd! Your nametag doesn't say "+ .@my_name$ +"!" : "That's a nice name you've got there, "+ strcharinfo(0) +"!";

Scripts for Tips

Saved a couple hours? Tips are appreciated! (:

Posted Image

Categories

Work in Progress

Crash Course: Scripting 101
Not sure how long it's going to take me, but I plan to cover the basics of scripting in an extensive guide. I'll be breaking it down into several different parts, so check back every now and then to see if I've written anything new! However, it's entirely up to you to make use of what I write here, so get busy and be creative!

A: Structure and Formatting, Variables
B: Expressions, Concatenation
C: Loops, Arrays
D: Functions
E: (to be determined)