Duplicate coding in a script?
In reading through many scripts, and especially those requiring the player to make a game changing choice, there seems to be a habit of including duplicate steps in all options of the choice. In my mind, it would make more sense to have each option only deal with the things it changes, then have them all drop through to a common code block for the bits that are the same, regardless of which choice was made.
The following excerpt has been taken from a script here, and I have followed it with how I would rewrite it based on my assumption above. If there is some reason why my rewrite would function differently than the original, I would appreciate someone pointing it out for me.
{
...
choice_end()
if (&result == 1)
{
&wimp = 1;
show_bmp("graphics\easy.bmp", 0, 0);
freeze(1);
script_attach(1000);
wait(500);
fade_down();
&player_map = 4;
sp_x(1, 310);
sp_y(1, 245);
sp_dir(1, 8);
load_screen();
draw_screen();
draw_status();
fade_up();
kill_this_task();
}
if (&result == 2)
{
&wimp = 2;
show_bmp("graphics\hard.bmp", 0, 0);
freeze(1);
script_attach(1000);
wait(500);
fade_down();
&player_map = 4;
sp_x(1, 310);
sp_y(1, 245);
sp_dir(1, 8);
load_screen();
draw_screen();
draw_status();
fade_up();
kill_this_task();
}
}
I would rather script it this way, unless there is a compelling reason not to.
{
...
choice_end()
if (&result == 1)
{
&wimp = 1;
show_bmp("graphics\easy.bmp", 0, 0);
}
if (&result == 2)
{
&wimp = 2;
show_bmp("graphics\hard.bmp", 0, 0);
}
freeze(1);
script_attach(1000);
wait(500);
fade_down();
&player_map = 4;
sp_x(1, 310);
sp_y(1, 245);
sp_dir(1, 8);
load_screen();
draw_screen();
draw_status();
fade_up();
kill_this_task();
}
Tia,
Mm.
The following excerpt has been taken from a script here, and I have followed it with how I would rewrite it based on my assumption above. If there is some reason why my rewrite would function differently than the original, I would appreciate someone pointing it out for me.
{
...
choice_end()
if (&result == 1)
{
&wimp = 1;
show_bmp("graphics\easy.bmp", 0, 0);
freeze(1);
script_attach(1000);
wait(500);
fade_down();
&player_map = 4;
sp_x(1, 310);
sp_y(1, 245);
sp_dir(1, 8);
load_screen();
draw_screen();
draw_status();
fade_up();
kill_this_task();
}
if (&result == 2)
{
&wimp = 2;
show_bmp("graphics\hard.bmp", 0, 0);
freeze(1);
script_attach(1000);
wait(500);
fade_down();
&player_map = 4;
sp_x(1, 310);
sp_y(1, 245);
sp_dir(1, 8);
load_screen();
draw_screen();
draw_status();
fade_up();
kill_this_task();
}
}
I would rather script it this way, unless there is a compelling reason not to.
{
...
choice_end()
if (&result == 1)
{
&wimp = 1;
show_bmp("graphics\easy.bmp", 0, 0);
}
if (&result == 2)
{
&wimp = 2;
show_bmp("graphics\hard.bmp", 0, 0);
}
freeze(1);
script_attach(1000);
wait(500);
fade_down();
&player_map = 4;
sp_x(1, 310);
sp_y(1, 245);
sp_dir(1, 8);
load_screen();
draw_screen();
draw_status();
fade_up();
kill_this_task();
}
Tia,
Mm.
A lot of people, particularly those from non-programming backgrounds tend to do stuff like that simply because they don't care or due to bad habit (or no habit of refactoring). I dealt with this when fixing up some of Skurn's scripts where he did that sort of thing sometimes, but even I do it sometimes without realising it.