Scripting only works in first two instances

Started by Jordanowen42, Tue 08/10/2024 06:55:38

Previous topic - Next topic

Jordanowen42

Hello all-

Recently I wrote in asking for advice on a 3x3 block puzzle I was making. The question there concerned a set of nine holes in a grid that had to be filled with the appropriate order of boxes. My original question was how to make the nine different blocks appear in all the different holes as the player tries different combinations. The solution was to have an invisible object in each box that would be triggered when the player placed a block object on the corresponding hotspot. The trigger would cause a different image (blocks in this case) to appear as the visual for the object in question.

Below is the code I wrote for each hotspot (copy pasted with the relative numbers update for each hotspot.) Somehow this script works for the first and second block images (graphics 227 and 228) but not the rest. I'm not sure how identical code can only work in two of nine instances.

Thoughts?

Code: ags
function hHotspot1_UseInv()


{
if (player.ActiveInventory == iTomb)
{object[1].Graphic = 227;
player.LoseInventory(iTomb);
return;}

{if (player.ActiveInventory == iHoldingOrb)
object[1].Graphic = 228;
player.LoseInventory(iHoldingOrb);
return;}

{if (player.ActiveInventory == iAngelCity)
object[1].Graphic = 229;
player.LoseInventory(iAngelCity);
return;}

{if (player.ActiveInventory == iGauntlet)
object[1].Graphic = 235;
player.LoseInventory(iGauntlet);
return;}

{if (player.ActiveInventory == iAngelHandshake)
object[1].Graphic = 230;
player.LoseInventory(iAngelHandshake);
return;}

{if (player.ActiveInventory == iCrucifix)
object[1].Graphic = 231;
player.LoseInventory(iCrucifix);
return;}

{if (player.ActiveInventory == iAngelInHead)
object[1].Graphic = 232;
player.LoseInventory(iAngelInHead);
return;}

{if (player.ActiveInventory == iReceivingBlock)
object[1].Graphic = 233;
player.LoseInventory(iReceivingBlock);
return;}


{if (player.ActiveInventory == iDancing)
object[1].Graphic = 234;
player.LoseInventory(iDancing);
return;}
}

Khris

#1
99% of the time, the answer is the usual one: the handler function isn't linked in the events table.

You copy-pasted the code, and I assume you also changed the function's name (hHotspot2_UseInv, hHotspot3_UseInv, etc.) but that code never runs unless you also put the new name next to the hotspot's respective event in the table.

There's also the issue of misplaced curly braces. The syntax is:

Code: ags
  if (condition)
  {
    statement1;
    statement2;
    etc;
  }
The braces are used to group the statements together so they all run conditionally.

In your code you did this correctly for iTomb but not the other items. It still compiles because without the braces only the statement right after the if() runs conditionally.

Finally, your approach creates a ton of duplicate code. Make sure the IDs of the inv items, hotspots and objects all have the same sequence, and you can simply add a number to the inv item's ID to get to the sprite slot. You can also use a single function for all nine hotspots.
Since AGS 3.6.1. the hotspot is passed to the handler, which means you can do:

Code: ags
function hole_UseInv(Hotspot *theHotspot, CursorMode mode) {
  int offset = 227 - iTomb.ID;
  if (player.ActiveInventory.ID >= iTomb.ID && player.ActiveInventory.ID <= iDancing.ID) {
    object[theHotspot.ID - 1].Graphic = player.ActiveInventory.ID + offset;
    player.LoseInventory(player.ActiveInventory);
  }
}

Say iTomb is inv item #14 and iDancing is #22.
The first hole's hotspot is #1 and uses object #0.
The second hole's hotspot is #2 and uses object #1.
And so on.
Assuming the sprites have the correct order, the offset between item ID and slot is 213. Adding this to for instance iHoldingOrb.ID (15) gives 228, and so on.

Since the hotspot is passed to the handler, we can use its ID to find the appropriate object ID (by subtracting 1 in my example) and the object[] array to address the room object.

If you follow all this, you can simply paste "hole_UseInv" into each hotspot's event table and you're done.

SMF spam blocked by CleanTalk