How to get the selected item of a menu?i

Make games! Discuss those games here.

Moderators: Bob the Hamster, marionline, SDHawk

Post Reply
User avatar
marionline
Metal Slime
Posts: 673
Joined: Sat Feb 26, 2011 9:23 pm

How to get the selected item of a menu?i

Post by marionline »

Hello!

I'm wondering if it is possible to get the currently selected item from a menu.
And not get it after it is choosen (that can be done by tags etc.), but just when it is selected.
I'd like to have the player choose something and display all the options on the screen.
The items are displayed as slices in a row, the selected item should move up and back down.

Plotscripting dictionary say about open menu fuction:
open menu (ID, allow duplicate)
Opens the menu specified by ID. You can also use the constants defined in your HSI file in the form menu:name. The menu will be opened on top of any menus that are already open. The optional second argument allow duplicates can be true if you want to be able to open more than one copy of the same menu at the same time. The return value is a menu handle that you should store in a variable for use with other menu-related commands.
1) How can I get to know this retun value?
2) Is there a better way to get the selected (not yet choosen) menu?

Code: Select all

#need this menu's return value and don't know how to get it! :(
	#PetMenu := open menu (3,0)
	
	variable(starfish)
	variable(jellyfish)
	variable(orangefish)
	variable(octopus)
	variable(guy)

    if (menu is open(PetMenu))
		(
		#number,palette
		starfish := load walkabout sprite (33, 41)
		jellyfish := load walkabout sprite (34, 42)
		orangefish := load walkabout sprite (35, 43)
		octopus := load walkabout sprite (36, 44)
		guy := load walkabout sprite (13, 1)
	
		put sprite(starfish, 20, 20)
		put sprite(jellyfish, 50, 20)
		put sprite(orangefish, 80, 20)
		put sprite(octopus, 110, 20)
		put sprite(guy, 140, 20)
		
		ChooseStar:= first menu item(PetMenu)
		ChooseJelly:= next menu item(PetMenu)
		ChooseOrange:= next menu item(PetMenu)
		ChooseOcto:= next menu item(PetMenu)
		ChooseGuy:= next menu item(PetMenu)
				
		if (selected menu item(PetMenu) == ChooseStar) then
			(
				#moves the slice 10 up
				move slice to (starfish, 10, 20,7)
				wait (3)
				move slice to (starfish, 20, 20,14)
				wait (3)
			)
		else if 	
		(selected menu item(PetMenu) == ChooseJelly) then
			(
				#moves the slice 10 up
				move slice to (jellyfish, 40, 20,7)
				wait (3)
				move slice to (jellyfish, 50, 20,14)
				wait (3)
			)
		# add others here
		
		)
	else 
		    
		free sprite (starfish) 
		free sprite (jellyfish) 
		free sprite (orangefish) 
		free sprite (octopus) 
		free sprite (guy) 


Thanks for you help! =D
If you have advice on how to 'handle' this better, please let me know. ;)
Last edited by marionline on Sun Jan 18, 2015 5:26 am, edited 2 times in total.
User avatar
Gizmog
Metal King Slime
Posts: 2615
Joined: Tue Feb 19, 2008 5:41 am

Post by Gizmog »

I think your script is already correct? When you say

Code: Select all

PetMenu := OpenMenu (3,0)
What you're really saying is

Code: Select all

SetVariable ( PetMenu,OpenMenu (3,0) )
The variable PetMenu will be that returned value.

EDIT: As for the second part of the question, I have no idea how menus work and will leave that answer to someone better qualified.
Last edited by Gizmog on Sun Jan 18, 2015 5:29 am, edited 1 time in total.
TMC
Metal King Slime
Posts: 4101
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

As Giz said, your script looks correct as long as you include "PetMenu := open menu (3,0)".

The way that you used "first menu item" and "next menu item" to check the selection is fine (provided that no menu items disappear based on tags!). But you're wondering, in order to check whether the selected menu item is the 3rd one, write:
menu item true slot (selected menu item (PetMenu) ) == 2
(You can also use "menu item slot" instead of "menu item true slot" as long as no menu items disappear based on tags. The "true" slot is the slot number as shown in the menu editor, while "menu item slot" returns the number of the menu item in the visible in-game menu)

You're missing the '(' or 'begin' after the 'else' at the bottom of your script.
Last edited by TMC on Sun Jan 18, 2015 6:08 am, edited 1 time in total.
User avatar
marionline
Metal Slime
Posts: 673
Joined: Sat Feb 26, 2011 9:23 pm

Post by marionline »

Thanks for your answers. :D

When I try using the "PetMenu := open menu (3,0)" part, the compiler says it's not recognized. (Not a script, variable, etc.)
By writing it that way A := B, it would be clear that A is a variable, right?

I added the missing begin to else as well. :)
User avatar
Gizmog
Metal King Slime
Posts: 2615
Joined: Tue Feb 19, 2008 5:41 am

Post by Gizmog »

Yes, A should be a variable. If you set up PetMenu somewhere as a variable, you'll be (probably) be right as rain.
User avatar
marionline
Metal Slime
Posts: 673
Joined: Sat Feb 26, 2011 9:23 pm

Post by marionline »

It seems like I am not setting up a variable.

Code: Select all

PetM := 0
I renamed PetMenu to PetM, so the word menu isn't used, just in case that could cause trouble.

I tried the script from Giz for setting a variable, and i tried this above from the plot scripting dictionary.
I still get the error message. :???:
User avatar
Gizmog
Metal King Slime
Posts: 2615
Joined: Tue Feb 19, 2008 5:41 am

Post by Gizmog »

Look at what you're doing for Starfish, Jellyfish, etc. They're not throwing errors, but they're doing the same

Code: Select all

Starfish := loadwalkaboutsprite (33,41)
stuff, right? It's trying to set the value of the variable Starfish to the number returned by the LoadWalkaboutSprite command. Notice how you declared that Starfish is the name of a variable with your

Code: Select all

variable (Starfish)
You might try throwing a

Code: Select all

variable (PetMenu)
in there, so that the game knows what PetMenu is.
User avatar
marionline
Metal Slime
Posts: 673
Joined: Sat Feb 26, 2011 9:23 pm

Post by marionline »

Thanks Giz!
Now the script is compiling.

I do get an error when trying to start the script:
next menu item: invalid menu handle -1 :???:

So, I have to ask for help again.
TMC
Metal King Slime
Posts: 4101
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Oh, I missed that.

Code: Select all

 ChooseJelly:= next menu item(PetMenu) 
should be

Code: Select all

 ChooseJelly:= next menu item(ChooseStar) 
and so on.

Also, rather than using 'PetMenu' everywhere you could use 'topmenu' instead, which is a handle for the topmost menu.
Post Reply