It would be best if the script just continued if the recipe is invalid. Calling the script again, as you tried could have worked, but probably didn't because you didn't include a wait, so the script immediately tests the recipe again because it sees Enter is still down, calls itself again, etc, getting into an infinite loop that's stopped by the engine. However just adding a wait isn't great, because holding down Enter will still cause it to loop, and if it repeats too many times (128) the script interpreter will stop the script. You could hit that if you make a number of wrong guesses, but adding a longer wait (e.g "wait(6)") would make it pretty unlikely. The best solution would be move everything into a "while(true) do(...)" loop, and "exit script" when the player tries to quit or gets a recipe right, but it's not strictly necessary.
Anyway, even if you resume the script after a wrong recipe you're still going to need to iterate over the NPCs and return all items to the inventory if the player wants to leave the crafting board. I suggest having a single script to check for an NPC somewhere, and delete it if it exists. Like your "deletion" script, but taking x and y as arguments:
Code:
script, delete from board, x, y, begin
variable(npcref, id)
npcref := npc at spot(x, y) # get the NPC reference
id := npc id at spot(x, y)
if (npcref == 0) then (exit script)
destroy npc (npcref)
# Figure out which item to return
if (id == 0) then (get item (item: iron))
else if (id == 1) then (get item (item: wood))
# ... etc
end
variable(npcref, id)
npcref := npc at spot(x, y) # get the NPC reference
id := npc id at spot(x, y)
if (npcref == 0) then (exit script)
destroy npc (npcref)
# Figure out which item to return
if (id == 0) then (get item (item: iron))
else if (id == 1) then (get item (item: wood))
# ... etc
end
Now you can use this script everywhere. When you add a new placeable item you don't want to have to update multiple scripts everywhere.
Now you can use this to delete every item on the board. Lets set the board is from x = 4 to 7 (inclusive) and y = 2 to 5. Just use two loops to delete every item:
Code:
variable (x, y)
for (x, 4, 8) do (
for (y, 2, 5) do (
delete from board(x, y)
)
)
variable (x, y)
for (x, 4, 8) do (
for (y, 2, 5) do (
delete from board(x, y)
)
)
I hope these scripts are self explanatory and also show you how to use for loops and how to split things into multiple scripts (I'm expecting you to learn from this!)
However if you wanted you could iterate over all the npcs on the map, using "npc reference" and "npc copy count" commands, and delete them that way. I'm just suggesting that if you do that, that you can make your scripts a lot cleaner and shorter by having a single script to delete an npc and return the corresponding item to the inventory.




