In my quest to get better at scripting I thought I ask all of you who are really good at this: How does one do a script like this more compact and not so convoluted? I’m sure there is a way and I’d like to learn. The script works perfectly as it is ingame, and I find it easy to follow, but there ought to be a way of making it half as long.
whenever i see repetitive instances of the same thing, i try to find out if putting them in a loop might be clearer. also, i like to describe my actions and group them together that way, which suggests using subroutines. so here’s my entry into the fray :
void SetEnemy(int i, object oPC)
{
string tag = "bandit" + IntToString(i);
object oBandit = GetNearestObjectByTag(tag);
if (!GetIsObjectValid(oBandit)) {
PrintString("ERROR in SetEnemy: no bandit with tag " + tag);
return;
}
ChangeToStandardFaction(oBandit, STANDARD_FACTION_HOSTILE);
SetIsTemporaryEnemy(oPC, oBandit);
AssignCommand(oBandit, ActionAttack(oPC));
}
void main()
{
object oPC = GetEnteringObject();
if (!GetIsPC(oPC))
return;
if (GetLocalInt(OBJECT_SELF, GetTag(OBJECT_SELF)))
return;
SetLocalInt(OBJECT_SELF, GetTag(OBJECT_SELF), TRUE);
int i;
for (i = 1; i <= 7; i++)
SetEnemy(i, oPC);
}
notice that you don’t need nw_i0_generic, and the construct if (_condition_ == TRUE)... is redundant [you need only specify if (_condition_) ... ]. finally, in most cases, ActionAttack() will make a call to DetermineCombatRound() ; was it not working for you w/o the additional call ?
Thanks for your input xorbaxian! I still only know the basics of scripting, having a hard time grasping everything still. I’m slowly getting better, but my mind doesn’t seem to be made for understanding this fully. I’m apparently an extremely slow learner when it comes to this. So thanks for being understanding.
For this script I copied and pasted from another similar one that I had done in the past (probably using a stock script as base, or using the Script Generator) so I didn’t look that much into if I needed that include in the first place. I don’t think I’ve ever used “for” in a script before…I guess it must be something similar to “while”?
I like your way of doing scripting, even though I have to say I have an easier way (at the moment) following what is happening in Clangeddin’s script.
exactly. for is useful when you know the bounds of a set of data you’re iterating over and you can refer to those data in integral steps, because you can set up a counter that you reference each time through the loop [in this case, i’m using the variable ‘i’]. while is a more general construct and will loop over your data as long as the condition you specify inside the while (...) is true. [more info here.]
definitely. in that script everything is right there in the main loop. as you noticed, one of the great advantages of this approach is that it’s easier to follow. it will start to become cumbersome if you decide later on that you want to do something fancier with your bandits than just having them attack. if you’re able to organise your code into sub-routines, grouping together sets of actions that accomplish a specific goal, you’ll have an easier time later on if you decide to elaborate. but i agree, for what you’re doing at the moment, clangeddin’s script will definitely do what you want w/o all that eye-strain.
Thanks travus! More compact indeed. Before doing the script I showed here, I actually made a script that was a bit similar to yours (but long and convoluted), with using the StandardAttack and copying from the stock script ga_attack. It behaved strangely though (the PC took no damage), so I went to my other script that I had here and then there were no more problems. I have erased that script so I don’t know exactly how I wrote it but I think it was something like this:
Comparing it to yours this uses ginc_param_const, which may have something to do with it behaving strangely perhaps? As I said, I just copied from the stock script ga_attack and that uses ginc_param_const.
Edit: Turns out, this script actually works (even if it’s convoluted). I had missed that I placed a trigger where the bandits stood which forced a cutscene conversation. That’s why my PC all of a sudden became immortal and I couldn’t access the inventory.
I will test each of your scripts now just for fun…I’ll decide then which of them I’ll use in my module.
Thanks everyone for posting and sharing your knowledge! One day I may understand at least half of what you do so easily here.
An alternative method is to use a delayed loop instead of a standard one, while more convoluted on appearence, an advantage of this method is to prevent lag when the loop involves lots of iterations.
This may not be your case, however, If you experience a bit of lag when the trigger is called in game, try giving this a shot, it should (not tested) make it smoother.