Badwrong

GMS2 Simple Menu

Nov 21st, 2021 (edited)
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Very simple menu with no sub-menus
  2.  
  3. // Struct
  4. function MenuItem(_label, _action) constructor
  5. {
  6.     Label   = _label;
  7.     Execute = _action;
  8.    
  9.     function Draw(_x, _y, _c)
  10.     {
  11.         draw_text_color(_x, _y, Label, _c, _c, _c, _c, 1);
  12.     }
  13. }
  14.  
  15. // Create
  16. MenuChoice = 0;
  17. Menu =
  18. [
  19.     new MenuItem("Start Game", function() { room_goto(RMGame)}),
  20.     new MenuItem("Exit Game", game_end )
  21. ];
  22.  
  23. // Step
  24. MenuChoice += (keyboard_check_pressed(vk_up) - keyboard_check_pressed(vk_down));
  25. var _length = array_length(Menu);
  26. MenuChoice = (_length + MenuChoice) mod _length; // Looping clamp
  27.  
  28. if keyboard_check_pressed(vk_space) Menu[MenuChoice].Execute();
  29.  
  30. // Draw
  31. for (var _i = 0, _h = string_height("H"), _length = array_length(Menu); _i < _length; _i++)
  32.     Menu[_i].Draw(x, y + _i * _h, c_white);
  33.  
  34. Menu[MenuChoice].Draw(x, y + MenuChoice * _h, c_yellow);
Add Comment
Please, Sign In to add comment