To complement and elaborate on what Khris said.
Excessive code, especially duplicating code, is often a source of mistakes.
If you are working with numbered sequential objects, usually this may be done using a loop over array.
For example:
Code: ags This will disable all hotspots between H3 and H11 (inclusive).
Another example:
Code: ags Above will add any inventory item with IDs in range from iTrinket2 to iTrinket12 (inclusive).
Similar solution may be used if you need to set state of some objects depending on other objects. If these objects are arranged in two corresponding sequences (like 10 hotspots and 10 objects, corresponding to each other) - in such case you may use relative index. For example, you have this:
Code: ags This may be redone simply as:
Code: ags
Then if you have an identical or almost identical block of commands repeating in a program, then this block may be moved to its own function. The differences can be solved by passing parameter(s) into this function.
For a basic example, you have a similar code repeated multiple times, depending on the clicked object:
Code: ags This may be done as a separate function
Code: ags And then called like
Code: ags
This ensures that each similar operation is performed in a uniform way.
Excessive code, especially duplicating code, is often a source of mistakes.
If you are working with numbered sequential objects, usually this may be done using a loop over array.
For example:
hH2.Enabled=true;
for (int i = hH3.ID; i <= hH11.ID; i++)
hotspot[i].Enabled = false;
Another example:
for (int i = iTrinket2.ID; i <= iTrinket12.ID; i++)
player.AddInventory(inventory[i]);
Similar solution may be used if you need to set state of some objects depending on other objects. If these objects are arranged in two corresponding sequences (like 10 hotspots and 10 objects, corresponding to each other) - in such case you may use relative index. For example, you have this:
if (oP2.Visible==true) hH2.Enabled=false;
if (oP3.Visible==true) hH3.Enabled=false;
if (oP4.Visible==true) hH4.Enabled=false;
...
for (int i = 0; i < 10; i++)
{
if (object[oP2.ID + i].Visible==true) hotspot[hH2.ID + i].Enabled=false;
}
Then if you have an identical or almost identical block of commands repeating in a program, then this block may be moved to its own function. The differences can be solved by passing parameter(s) into this function.
For a basic example, you have a similar code repeated multiple times, depending on the clicked object:
function oT2_Interact()
{
mouse.EnableMode(eModeUseinv);
player.ActiveInventory=iTrinket2;
mouse.Mode=eModeUseinv;
oT2.Visible=false;
}
function InteractWithTrinketObject(Object* obj)
{
int relativeObjID = obj.ID - oT2.ID; // relative index, starting with the oT2
mouse.EnableMode(eModeUseinv);
// it is possible to calculate matching inventory item's ID from object's ID,
// because they have 2 corresponding sequences
player.ActiveInventory=inventory[iTrinket2.ID + relativeObjID];
mouse.Mode=eModeUseinv;
obj.Visible=false;
}
function oT2_Interact()
{
InteractWithTrinketObject(oT2);
}
function oT3_Interact()
{
InteractWithTrinketObject(oT3);
}
// and so on
This ensures that each similar operation is performed in a uniform way.