So I've been working my way through Saffith's tutorials (again and again, always catching things I've missed), and this is my first 'useful' script, intended to add a durability mechanic to the wooden sword:
int S1_usecount = 0; int S1_maxuse = 19; // Total number of uses less 1. float breakthresh = 0.75; // Breaking threshold. // When the usecount reaches this percentage of the maxuse, SFX_BREAKING will play on use. const int SFX_BREAKING = 57; const int SFX_BROKEN = 58; // The tapping SFX have been used as placeholders. item script Stick { void run() { if(S1_usecount < S1_maxuse) // Increase counter every swing. { S1_usecount += 1; if(S1_usecount > S1_maxuse * breakthresh) // Play SFX after breaking threshold. { Game->PlaySound(SFX_BREAKING); Waitframe(); } Waitframe(); } else if(S1_usecount >= S1_maxuse) { S1_usecount = 0; Game->PlaySound(SFX_BROKEN); Link->Item[I_SWORD1] = false; Waitframe(); } } }
I've improved the script a few times, namely by nesting the second if statement, which used to be its own else if.
I also tried editing the script by integrating int usecount and int maxuse as local variables so that the script could be applied to any item and customized via D0, D1, etc. But I got turned around when it came to Link->Item[I_Sword1] = false;, since I didn't know what to replace I_SWORD1 with in order to make the script 'generic', simply removing the item running the script. I guess I could do this with D2, but is it possible to make this automatic?
I'm curious also whether there are any code improvements that I could make. I'm not asking for improvements to what the script does, but rather to how it's written. Since I've already improved it a couple of times as I've figured more out, help would be appreciated in this regard.
I had one further question: There's a point in Saffith's beginner tutorial where he asks you to make a script that reduces a value by 25%. I accomplished this by writing Link->HP *= 0.75;, but he did it by writing Link->HP -= Link->HP * 0.25;. I like that my code is simpler, but I like that his code retains the original phrasing, so to speak; in order to write my code, I first had to do a mental calculation that he lets the compiler do. After thinking about this for a while, I wonder whether there is any instance where his code would be the objectively better option or whether it will always come down to preference.
I don't have internet at home right now, so responses may be delayed. Thanks.