[Example] Dynamic menu

Post your Construct-made creations!

Post » Wed Jan 21, 2009 10:40 am

This demo shows how a dynamic menu can be created using a hashtable. The hashtable contains menu items (such as Start Game, Load Game, Save Game, Exit Game etc.), which are read and used to build the menu with. The menu automatically expands and adds necessary amount of buttons as well as set them up to work with.

All this can be done in runtime; just change the hashtable entries. See the demo here:

Menu Demo cap

I am open for alternatives and optimization tips.

Bug: Also, in this demo the debugger bug is apparent. Run in debug mode, open and close menu, in the instances list in debugger the MenuButton shows as 0 instances, but it contains entries. Clicking one of them will cause a crash. MenuButton is a container object, contained with MenuButtonText, and is destroyed when MenuButtonText is destroyed. Debugger doesn't seem to register this and so refers to non-existing isntances.
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155

Post » Wed Jan 21, 2009 2:22 pm

thats pretty awesome. i think i might use this method for my game later on.
B
2
G
4
Posts: 43
Reputation: 1,136

Post » Wed Jan 21, 2009 2:27 pm

Thanks for the good bug report, the debugger thing is fixed in the next build :)
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Wed Jan 21, 2009 5:28 pm

This is very cool. I added a pause by setting the time scale to zero while the menu is up. Here is mipey's awesome cap with a pause added.

PS: Setting timescale to zero is a good way to make a pause right?
B
3
S
2
G
4
Posts: 271
Reputation: 2,017

Post » Wed Jan 21, 2009 5:38 pm

[quote="stainsor":3sleift2]PS: Setting timescale to zero is a good way to make a pause right?[/quote:3sleift2]
Yes, definitely!
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Wed Jan 21, 2009 8:03 pm

As long as menu operations don't make use of timedelta!
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155

Post » Wed Jan 21, 2009 8:41 pm

Nice example, and even though the amount of events would make this a pain to implement for a novice, the scripting should be relatively easy to understand.
Would be nice to see something like this as an object someday.
Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Post » Wed Jan 21, 2009 8:53 pm

[quote="Mipey":1jstk9ir]As long as menu operations don't make use of timedelta![/quote:1jstk9ir]
You actually can use timedelta while paused - you can set it and set it back over a section of events, like:

+ Always
-> Set timescale to 1
-> Set Opacity to Opacity + 100 * TimeDelta
-> Set timescale to 0
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Wed Jan 21, 2009 9:00 pm

All this can be dumped into its own event sheet and then pushed aside to be included in any layouts where necessary. A simple way to execute actions linked to buttons would be with a function call.

[code:2f5tekex]MouseKeyboard: On Left Either single or double clicked on Blue
> FunctionCall function Blue('MenuItem') (and Forget picked objects)[/code:2f5tekex]

As you can see, this calls a function with name taken from the menu button variable. The function can be stored anywhere, best on the layout event where menu was generated (assuming the button clicked has "exit" stored within MenuItem variable):

[code:2f5tekex]Function: On function "exit"
> System Close application[/code:2f5tekex]

That is all it takes. And when you need to disable the menu after clicking a button, just set the global variable 'MenuToggle' to 0 within the called function. Poof, menu gone. Hooray for clean code (at least within the layout sheets)!
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155


Return to Your Creations

Who is online

Users browsing this forum: No registered users and 1 guest