How do I check if a slice collection is opened or closed?

From OHRRPGCE-Wiki
Jump to navigation Jump to search

Just like menus, slices collections state needs to be checked before actually closing them. This is to prevent an error message called 'invalid slice handle"

It's really easy to get this error if you don't check it correctly! The main reason is that close it directly without using the if function.


Let's see how to prevent that. Let's say that you have your own designed armor shop. It is a slice collection with a menu rectangle slices and a portrait of the merchant.

You made it using slice collection (12). As you know, a slice collection is manipulated though global variables (just like menus by the way).

To make appear your shop on screen you'll write :

global variable (14, shop sl collection handle)

shop sl collection handle:= load slice collection (12) #armor shop opens

And when you want to close your slice collection, you'll write

free slice (shop sl collection handle) # armor shop closes

That's generally when the error message pops up on screen.

The trick is just like with the menus, to make disappear our slice collection we need to be sure that it is already on screen when the line free slice is read by Hspeak.exe.


How to do that? You use an if function.


As you you know the value of a global variable can return whether true or false, that value 0 or take value 1. So you may think that you need to write

  if (shop sl collection handle== false)
or
  if (shop sl collection handle== 0)

when you want to make appear your slice collection and its reverse


  if (shop sl collection handle== true)
or
  if (shop sl collection handle== 1)

when you want to make it disappear... but you would be wrong.


Your code would be recognised by Hspeak.exe and it would compile. It may even work the first time you test but it would be the first time only. Why is that? Well because the variable used for slice handling keeps its values once and for all.

You can check this by yourself. Give this script to a separate NPC.

#---------------------------------------------------
plotscript, slice collection state, begin 

if (shop sl collection handle==0) then, begin
show value (2)
end

if (shop sl collection handle==1) then, begin
show value (3)
end

end #end for the script
#-------------------------------------------------

Make your slice collection pop up and then make it disappear. Go and speak to the NPC. He/she will give you the value (3) What does that means? It means that when the slice collection disappears its value is still one (that's why you get a 3 as an answer under the form of a value on your screenplay)! Or to be able to trigger the shop again, you need a 0!


You make wonder, I've already used a global variable like a tag to check a condition for the menus and it worked! The plotscript dictionary itself says a value in global variable can return true or false and you would be right.

That's the trick here. To be able to check if a slice collection is on screen we cannot use the variable used to put it on the screen itself! We need to use another one.

Let's take the example of the armor shop again. But this time we'll use two variables.

global variable (shop sl collection handle)
global variable (armor shop on screen)

#----------------------------------------------
script, armor shop, begin 

suspend player
suspend npcs 

if (armor shop on screen == true) then, begin
shop sl collection handle:=load slice collection (12)
#your other stuff here

armor shop on screen:= false
wait (2)
end #end for the if shop sl collection handle

resume player
resume npcs

end# end for the script

#-----------------------------------------------------
script, quit armor shop, begin

suspend player
suspend npcs 

if (shop sl collection handle== false) then, begin
free slice (shop sl collection handle)
#your other stuff here

armor shop on screen:= true
wait (2)

end #end for the if shop sl collection handle

resume player
resume npcs
end #end for the script

#-----------------------------------------------------


You can test by yourself and see that it works perfectly.

As global variable are precious because they are limited, you may not want to use it for something like that but may want replace them by a tag to get the same effect

global variable (shop sl collection handle)


#----------------------------------------------
script, armor shop, begin 

suspend player
suspend npcs 

if (check tag (tag: armor shop on screen)== on) then, begin
shop sl collection handle:=load slice collection (12)

# your other stuff here
set tag (tag: armor shop on screen, off)
end #end for the if check tag

resume player
resume npcs

end# end for the script

#-----------------------------------------------------
script, quit armor shop, begin

suspend player
suspend npcs 

if (check tag (tag: armor shop on screen)== off) then, begin
free slice (shop sl collection handle)
# your other stuff here

set tag (tag: armor shop on screen, off)
end #end for the if check tag

resume player
resume npcs
end #end for the script

#-----------------------------------------------------


So in conclusion, we can say that slice collection like menus can be tricky to manipulate. Especially when you think that you could get an information but if fact for some reasons you don't. If you still have error message while closing your slice collections, feel to ask questions on the discord servor or on the slime salad forum