Recalling previous selected item in a menu (SOLVED)

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
KutKu
Red Slime
Posts: 31
Joined: Sun Jan 01, 2017 11:17 pm

Recalling previous selected item in a menu (SOLVED)

Post by KutKu »

By default game.exe will always remember the last slot you picked from a previous menu when returning to it.

I'm trying to replicate that and it has been challenging. The reason why I'm doing this is I am creating a menu system that doesn't use the drop down list style that OHR uses.

Basically instead of selecting slots in a menu this highlights windows that will open actual custom.exe menus. It runs on scripts. Two specifically, and one other function but that's not relevant.

1. Runs script that opens and edits a custom.exe menu
2. Populates the custom.exe menu list based on how many of X you have
3. Sets each item to open a script that allows editing of X
4. Said script opens a custom slice menu
5. An option in the slice menu is to go back to the previous custom.exe menu that also runs the 1st step script

Step five is where I'm stuck.

A few things I've tested and tried

Add a new argument to the first script

So for the script in step 1, I'll just add an argument that is used
to remember the last selected item when getting to step five.

I did that. I used a global so the value is always constant in game.exe's memory. However! If I did this...

Code: Select all

plotscript, custom menu, remember, begin

	#stuff
end

script, my custom slice menu, begin

	#stuff
	#option to close slice menu and return to custom menu
	custom menu (last selected slot)
	
end
Now correct me if I'm wrong, Hspeak creates and destroys variables from "begin" to "end." Whenever a new script is placed before an "end" command that script will do its business and the previous script will just wait until the last one is done.

At least that's what I observed in the script debugger. I didn't find anything in the dictionary that would let the script continue after a new one is called.

In conclusion, this did work but not in any way that I want it to. It's due to the fact that the player can leave many scripts hanging, potentially causing numerous errors with global variables or cause a lot of memory to be tied up. Plus it's an ugly solution.

My second attempt was to create a dummy menu with the bit "Allow gameplay and scripts" and have it run (custom menu) on close then insert that menu into (my custom slice menu) and close it on there.

Code: Select all

plotscript, custom menu, remember, begin

	#stuff
end

script, my custom slice menu, begin

	#stuff
	#option to close slice menu and return to custom menu
	dummy menu := open menu (2, false)
	close menu (dummy menu)
	
end
This didn't work. My guess is that "On-close script: script" only works on player inputs; it ignores script commands like "close menu (menu handle)."

Anyway, I'm taking a break from this but if anyone feels likes helping or throwing an obvious solution I missed at my head be my guest.
Last edited by KutKu on Sun Feb 04, 2018 6:02 am, edited 3 times in total.
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Re: Recalling the previous selected item in a menu

Post by sheamkennedy »

I think whatever you're trying to achieve is beyond me however I did see something you might want to look into. You said:
KutKu wrote:I didn't find anything in the dictionary that would let the script continue after a new one is called.
If I understand correctly using a timer could achieve the frequent re-triggering of a script every game tick. I can't say for sure but this might help you. Sometimes I have constantly running timers in my game that contain a series of tag checks. Thus the timers don't do anything until the tags equal true, or if a counter reaches a certain value.

I'd wait for other responses. There's probably a more straight forward solution to your problem.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
� C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
KutKu
Red Slime
Posts: 31
Joined: Sun Jan 01, 2017 11:17 pm

Re: Recalling the previous selected item in a menu

Post by KutKu »

sheamkennedy wrote: If I understand correctly using a timer could achieve the frequent re-triggering of a script every game tick.
I just tried something like this before I read this message. In fact I've tried a few other things in between...

Like "use menu item (menu item handle)" and "exit returning (value)". I honestly thought I was onto something... but oh well, the scripts still lingers. Once a new script starts, the one before is always suspended and still in memory unless game.exe can get to the "end" command.

It's really odd and not to seem dismayed but I just don't think I can do this; at least not the way I have it set up right now, as two separate scripts.

I'd personally would not want to have any scripts larger than 100 lines. But if I have to I'll have to lump the blocks together.
Last edited by KutKu on Mon Jan 22, 2018 6:11 am, edited 1 time in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

You don't want timers for this. You are correct that triggering a script causes all other scripts to be suspended, as seen in the slice debugger. (Plus, if one script calls another directly, then the calling script will of course have to wait for the script it called) So what you need to do is make sure you don't run the same script twice.

I didn't understand the list of steps you posted: is the menu in step 1 closed when opening a submenu? And the problem is that you want the script controlling that first menu to end when the submenu script starts, so that the first menu script can write the selected menu item to a global variable on the way out? Well, you could always write the global elsewhere (eg immediately before calling the submenu script).
Or if the first menu script remains running the whole time, and the submenu scripts have ended, then it can recreate the menu when control returns to it.

If that first menu (from step 1) is a normal menu defined in the Menu Editor, then the selected menu item will be remembered for it. But you wrote that you populate the menu by creating menu items when it's opened? Well, that builtin option won't work then.
My guess is that "On-close script: script" only works on player inputs; it ignores script commands like "close menu (menu handle)."
You're correct. I didn't even know that! But if it did, it would be basically equivalent to you calling the correct on-close script immediately after "close menu".
KutKu
Red Slime
Posts: 31
Joined: Sun Jan 01, 2017 11:17 pm

Post by KutKu »

TMC wrote:I didn't understand the list of steps you posted: is the menu in step 1 closed when opening a submenu?
Right now I'm choosing to close the menu from script 1 when script 2 runs via this block.

Code: Select all

	close menu (top menu)

# this is a slice collection for displaying stats

	if (unitdataslice >> 0) then(
		free slice (unitdataslice)
		unitdataslice := NONE
	)
This doesn't affect the menu script 2 creates because it's all just a collection of slices. I chose to close script 1's menu because even when I ran the command "suspend player" game.exe still accepted inputs on that menu. Meaning that a different item slot would be highlighted based on whatever arbitrary inputs the player put in.
TMC wrote:And the problem is that you want the script controlling that first menu to end when the submenu script starts, so that the first menu script can write the selected menu item to a global variable on the way out?
Not quite, script 1 creates the data for script 2. I want script 2 to have an option to go back to script 1. But the engine doesn't allow the option to end a script while a new one is running (or I haven't found the command). So 1 & 2 can potentially be called a bunch of times and that'll probably cause a whole buncha problems.
TMC wrote: Well, you could always write the global elsewhere (eg immediately before calling the submenu script).
Or if the first menu script remains running the whole time, and the submenu scripts have ended, then it can recreate the menu when control returns to it.

If that first menu (from step 1) is a normal menu defined in the Menu Editor, then the selected menu item will be remembered for it. But you wrote that you populate the menu by creating menu items when it's opened? Well, that builtin option won't work then.
Honestly I'm just looking for the laziest (i.e. smartest!) way to do this.

I'm planning on keeping script 1's menu open but inserting a new block of code that locks that data while script 2 is running. So far that seems like the simplest solution.
KutKu
Red Slime
Posts: 31
Joined: Sun Jan 01, 2017 11:17 pm

Post by KutKu »

Yay! I've got this solved... kinda...

O.K., so here's what I did

At the top block of script 2 I set these static variables. They get the data that script 1 created, the menu data specifically.

Code: Select all

	variable (old)
	old := find menu ID (2)
	variable (lock)
	lock := selected menu item (old)
I used a while loop to basically interpret and reflect the correct player choice in script two. And at the end I used "select menu item" to always run at the end of that loop and chose the same option from the menu script 1 created. I also set the bitset "Disable cancel button" on for the menu script 1.

Code: Select all

while (TRUE) do(

# stuff

		wait for key
		highlight := reflect player input (input)
		select menu item (lock)
)
end
And no problems... except that menus appear over slices. Well hopefully the dictionary or custom.exe bitsets have something for me.
Last edited by KutKu on Tue Jan 23, 2018 12:51 am, edited 1 time in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

I chose to close script 1's menu because even when I ran the command "suspend player" game.exe still accepted inputs on that menu.
suspendplayer doesn't affect controls on menus and textboxes (including choiceboxes). You have to suspend those separately, using "set menu bit(topmenu, menubit:no controls, on)" and "suspend box advance" respectively.
KutKu wrote:And no problems... except that menus appear over slices. Well hopefully the dictionary or custom.exe bitsets have something for me.
Unfortunately menus always appear above everything else. Because we haven't yet converted menus to slices, so they have to be drawn separately from the slice tree. We're working on doing that!
But the engine doesn't allow the option to end a script while a new one is running (or I haven't found the command).
There isn't one; it's not possible yet. Working on that too...

BTW, you can now write < and > instead of << and >>.
KutKu
Red Slime
Posts: 31
Joined: Sun Jan 01, 2017 11:17 pm

Post by KutKu »

TMC wrote:
I chose to close script 1's menu because even when I ran the command "suspend player" game.exe still accepted inputs on that menu.
suspendplayer doesn't affect controls on menus and textboxes (including choiceboxes). You have to suspend those separately, using "set menu bit(topmenu, menubit:no controls, on)" and "suspend box advance" respectively.
Gee, coding sure can be an incredibly complex and subtle thing.

... At least that's assuming if I never bother reading the documentation. Thanks TMC, where would I be without you guys?
TMC wrote:
KutKu wrote:And no problems... except that menus appear over slices. Well hopefully the dictionary or custom.exe bitsets have something for me.
Unfortunately menus always appear above everything else. Because we haven't yet converted menus to slices, so they have to be drawn separately from the slice tree. We're working on doing that!
Eh, I'll just shoo the menu off to the side in the mean time and bring it back when I'm done.
TMC wrote:
But the engine doesn't allow the option to end a script while a new one is running (or I haven't found the command).
There isn't one; it's not possible yet. Working on that too...
I'll definitely keep this in mind from now on.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

... At least that's assuming if I never bother reading the documentation. Thanks TMC, where would I be without you guys?
I might appear to know everything off the top of my head, but I actually look up a lot of stuff in the plotdictionary or elsewhere! Even really simple commands, just to doublecheck my memory.
Post Reply