CODE
Game->Counter[CR_RUPEES] += 1;
EDIT:OK, I should stop posting my questions here because I'll have quite a couple.
Edited by Banjo7J, 23 October 2009 - 12:16 PM.
Posted 23 October 2009 - 11:24 AM
Edited by Banjo7J, 23 October 2009 - 12:16 PM.
Posted 29 November 2010 - 02:59 AM
Posted 14 December 2010 - 06:13 PM
User-defined functions and script arguments
<return type> <name>(<arguments>)
{
<body>
}A simple example, since that's easier to parse:float Add(float addend1, float addend2)
{
return addend1 + addend2;
}The first line is called the header. This is the same as you've seen in zscript.txt and std.txt: first the return type, then the name, then the arguments in parentheses. The parentheses are needed even if the function takes no arguments.// This function returns true if x is greater than y; otherwise, it returns false
bool IsGreater(float x, float y)
{
if(x > y)
{
return true;
}
else
{
return false;
}
}
// This function returns true if the given combo is a damage combo
bool IsDamageCombo(int index)
{
int type;
// Input validation is generally a good idea
if(index < 0 || index > 175)
{
// Out of range
return false;
}
// Read the combo type and compare it against every damage type in std_constants.zh
type = Screen->ComboT[index];
return type == CT_DAMAGE1 || type == CT_DAMAGE2 ||
type == CT_DAMAGE3 || type == CT_DAMAGE4 ||
type == CT_DAMAGE5 || type == CT_DAMAGE6 ||
type == CT_DAMAGE7;
}
// The return type specified must match what is actually returned.
bool Add(float addend1, float addend2)
{
return addend1 + addend2; // ERROR: This is a number, not a bool
}As seen in the first example above, return may be used conditionally. It is possible to create a situation in which no return statement is ever reached. The compiler will not catch this, but it should be considered an error.// A slightly different version...
bool IsGreater(float x, float y)
{
// If x and y are equal, neither return statement will be executed.
if(x > y)
{
return true;
}
else if (x < y)
{
return false;
}
}That doesn't apply to void functions, though. Void functions don't return anything, and it will cause an error if you try. A void function doesn't need a return at all, but you can use it to end the function early. If you do this in run(), it's the same as calling Quit().// This function will kill Link
void KillLink()
{
Link->HP = 0;
// Nothing is returned
}
// This function doesn't do anything
void DoNothing()
{
return;
Link->HP = 0; // This line will never run
}Functions can be defined either globally or within a script.void GlobalFunction()
{
// A function defined outside of the script is global, which means it can
// be used by any script. Be sure to give these unique names.
}
ffc script Script
{
void run()
{
// Gotta have run()...
}
void ScriptFunction()
{
// A function defined inside a script can only be used within the script.
}
}Like global variables and constants, global functions can conflict, so it's usually best to define functions within scripts when possible. Generally, you only need to create global functions if you're making a header file to be used in lots of different scripts (like std.zh) or a global script (discussed later).ffc script SquareNumbers
{
void run()
{
// This will print 1, 4, and 9 to allegro.log
Trace(Square(1));
Trace(Square(2));
Trace(Square(3));
}
int Square(int num)
{
return num * num;
}
}Numbers and bools passed to functions as arguments will still be the same afterward.ffc script Arguments
{
void run()
{
int num = 10;
SetToFive(num);
Trace(num); // The number printed will be 10
}
// This function effectively does nothing
void SetToFive(int x)
{
x = 5;
}
}User-defined pointers are covered in the advanced tutorial, but there's a special case that needs to be addressed here. The special pointer this isn't automatically defined in functions other than run(). If you want to use it in a function, the function needs to take it as an argument. In FFC scripts, its type is ffc; in item scripts, it's itemdata.ffc script LeftMover
{
void run()
{
while(true)
{
MoveLeft(this, 2);
Waitframe();
}
}
void MoveLeft(ffc this, float step)
{
this->X -= step;
}
}Finally, there's the matter of handling script arguments. This is quite simple: they're arguments to run(). run() can take up to eight arguments, which can be either numbers or bools. The first argument corresponds to D0, the second to D1, etc.ffc script Args
{
void run(int number, bool trueOrFalse)
{
Trace(number); // Traces whatever was entered as D0
TraceB(trueOrFalse); // Traces false if D1 was 0, true otherwise
}
}In the case of item scripts, the same arguments are passed to both pickup and action scripts. If both need arguments, it may be necessary to have one of them take the first few as dummies, only actually making use of the higher numbered arguments.if(LinkInRange())
{
DoFireballAttack();
}
else
{
JumpAround();
}Just from the function names, you can get a pretty good idea of what that's meant to accomplish. Such code is called "self-documenting," because the code itself clearly explains what it does.global script CustomGlobal
{
void run()
{
InitIceArrows();
while(true)
{
DoCompassSound();
UpdateIceArrows1();
Waitdraw();
DrawStatusIndicator();
UpdateIceArrows2();
Waitframe();
}
}
}Writing functions for the global script is not always straightforward. You'll often need to use global variables to track data from one frame to the next. For instance, you may need to keep two or three of them to determine if Link's moved since the previous frame. You might need some sort of global counter to track where you are in an ongoing process. It's unfortunate that such things are necessary, but when writing scripts for general use, ease of use should be a high priority.Posted 15 December 2010 - 10:25 PM
Edited by Schwa, 15 December 2010 - 10:36 PM.
Posted 28 July 2011 - 12:51 PM
0 members, 1 guests, 0 anonymous users