|
Master of Puppets
Join Date: May 2009
Location: MN
Posts: 1,114
Current Game: Chrono Trigger
|
Thought I'd drop in and say hi with a script to look at.
This mostly came from me looking over some HK factory scripts and realizing just how inefficient they were.
I had these three separate scripts
a_disthk51
Code:
void main(){
object oHk51cs = GetObjectByTag("hk51cs", 0);
string string1 = "hk51_1";
int int3 = 0;
object object1 = GetObjectByTag(string1, int3);
if(GetGlobalNumber("299TEL_HK51") < 2){
if(GetGlobalNumber("299TEL_HK51_Active") < 2){
SetGlobalNumber("299TEL_HK51_Active", 3);
}
if(GetGlobalNumber("299TEL_HK51_Core") == 1){
while ((int3 < 21)) {
SignalEvent(object1, EventUserDefined(1003));
(int3++);
AssignCommand(object1, ActionPlayAnimation(0,1.0));
object1 = GetObjectByTag(string1, int3);
}
AssignCommand(oHk51cs, ActionPlayAnimation(0,1.0));
ExecuteScript("a_move51line", GetArea(OBJECT_SELF), 0xFFFFFFFF);
}
else{
int3 = 0;
while ((int3 < 21)) {
if(int3 < 17){
ChangeToStandardFaction(object1, 3);
SignalEvent(object1, EventUserDefined(1003));
}
(int3++);
AssignCommand(object1, ActionPlayAnimation(0,1.0));
object1 = GetObjectByTag(string1, int3);
}
AssignCommand(oHk51cs, ActionPlayAnimation(0,1.0));
ExecuteScript("a_move51disp", GetArea(OBJECT_SELF), 0xFFFFFFFF);
}
}
}
a_move51line
Code:
void main() {
int int1 = 0;
string string1 = "hk51_1";
object object1 = GetObjectByTag(string1, int1);
string sWay1 = IntToString(int1);
while ( (object1 != OBJECT_INVALID) ) {
AssignCommand(object1, JumpToObject(GetObjectByTag("WP_HK51_LINE_" + sWay1, 0), 1));
(int1++);
object1 = GetObjectByTag(string1, int1);
sWay1 = IntToString(int1);
}
object object3 = GetObjectByTag("hk51cs", 0);
AssignCommand(object3, JumpToObject(GetObjectByTag("WP_HK51_LINE_L", 0), 1));
}
a_move51disp
Code:
void main() {
int int1 = 0;
string string1 = "hk51_1";
object object1 = GetObjectByTag(string1, int1);
string sWay1 = IntToString(int1);
while ( (object1 != OBJECT_INVALID) ) {
AssignCommand(object1, JumpToObject(GetObjectByTag("WP_HK51_NME_" + sWay1), 1));
(int1++);
object1 = GetObjectByTag(string1, int1);
sWay1 = IntToString(int1);
}
object object3 = GetObjectByTag("hk51cs", 0);
AssignCommand(object3, JumpToObject(GetObjectByTag("WP_HK51_NME_L", 0), 1));
}
to distribute some HK51 units around the HK factory based on their behavior cores.
I just had to rewrite the code and I came up with this:
I put some comments in too to make it user-friendly. 
Shows off some nice examples of how to use while loops and methods.
Code:
/*If you're defining any methods, be sure you declare them up here first
*before using them in the main code.
*You can put all of the code up here if you want, but I find it easier
*to put it down below.
*/
string factWP();
//Any variables that are going to be used in more than one method, declare them here, but initialize in the method.
int nGlobal;
void main() {
/*Declare all of your variables at the top up here
*It doesn't matter what order you do them in, but
*it helps to have them in a certain order, especially
*if some are dependent on other type variables
*/
//Integer variables
int nObjectNum = 0;
int nGlobal = GetGlobalNumber("299TEL_HK51_Core");
//String variables
string sTag = "hk51_1";
string sWPName;
//Notice how sWPNum is dependent on nObjectNum existing
string sWPNum = IntToString(nObjectNum);
object oHk51cs = GetObjectByTag("hk51cs", 0);
object oHK51Object = GetObjectByTag(sTag, 0);
/*This runs a loop and keeps running through all of the objects
*in the area that fit the oHK51Object specifications.
*/
while (oHK51Object != OBJECT_INVALID){
//Always ClearAllActions() before trying to assign new actions
AssignCommand(oHK51Object, ClearAllActions());
//DelayCommands are essential to the sequencing process
//If you try to assign commands all at once, they most likely won't fire
DelayCommand(1.0, AssignCommand(oHK51Object, ActionJumpToObject(GetObjectByTag(factWP() + sWPNum, 0))));
DelayCommand(2.5, AssignCommand(oHK51Object, ActionPlayAnimation(19, 1.0, 0.0)));
/*It's often helpful to send debug information to your feedback screen so you know what's going on*/
SendMessageToPC(GetFirstPC(), factWP() + sWPNum + " is what's showing.");
//Any type of loop needs to be incremented if you want a possibility of ever exiting the loop
(nObjectNum++);
oHK51Object = GetObjectByTag(sTag, nObjectNum);
sWPNum = IntToString(nObjectNum);
//Then the loop starts over!
}
AssignCommand(oHk51cs, JumpToObject(GetObjectByTag(factWP() + "L", 0), 1));
DelayCommand(1.0, AssignCommand(oHk51cs, ActionPlayAnimation(19, 1.0, 0.0)));
}
string factWP(){
if(nGlobal == 1){
return "WP_HK51_LINE_";
}
else{
return "WP_HK51_NME_";
}
}
What's my point behind this? Well, the point is the more you know about scripting/programming, the more efficient your scripts can be. Same functionality, WAAAAY more concise.
Expect me to drop in more often 
|