Discord
Login
Community
DARK THEME

how do I open uis using microscript 2.0

so I'm trying to make a prestige mechanic but I need to be able to open the UI this is the code I have

          if b.id == "6" and mouse.press then
            screen.drawSprite("prestige", 0, 0, 48, 32)
          end

What exactly are you trying to do? Do you want to have that button and then, when you press it, open a menu?

exactly

Well, what I would do is create an object that contains all the menu information, and it should have three functions:

  • init: This will be used to set the initial values ​​when opening the menu (including resetting values) and this function also serves to indicate that this is the current menu in use
  • update: It covers all the actions the menu will perform when in use, such as detecting button clicks, etc
  • draw: This will be used to specify everything you want the menu to display, the buttons, etc

And you will also need to create the variable menu which you will use to indicate which menu is currently in use (which is a global variable)

menu = false

MenuTest = object
  init = function()
    // This changes the "menu" variable, which is above, to the "MenuTest" object
    global.menu = this
  end
  update = function()
  end
  draw = function()
  end
end

While in your initial update and draw functions you should place the following:

update = function()
  if menu then menu.update() end
end
draw = function()
  screen.clear()
  if menu then menu.draw() end
end

Now, when you want to set up a menu, you simply need to call the init function of the menu you want, I mean:

MenuTest.init()

To remove the menu, simply do the following:

menu = false

Now, you just need to put what you want the menu to do when it's used in its update and what you want to be drawn in its draw. Here's a script to help you understand better; you can activate and deactivate the menu using the M key:

MenuTest = object
  coins = 0
  size = 0
  init = function()
    global.menu = this
    size = 0
  end
  update = function()
    if mouse.press then
      coins += 1
      size = 4
    end
    size *= 0.94
  end
  draw = function()
    local s = 10+size
    screen.drawText("Click on the screen to receive coins!",0,0,s,"rgb(255,255,255)")
    screen.drawText(coins,0,-10,s,"rgb(255,255,255)")
  end
end

init = function()
  menu = false
end

update = function()
  if keyboard.press.M then
    if menu then
      menu = false
    else
      MenuTest.init()
    end
  end
  if menu then menu.update() end
end

draw = function()
  screen.clear()
  if menu then menu.draw() end
end

Now, if you want to have multiple menus, you could do the same, only instead of using a variable, you could use an array, in which all the menus being used will go, and with a for loop check each menu and use its update and draw from each one

your code is not working very well for me maybe if you see my current code it would work

init = function()
//loading system -------------------------------------------------------
  Points = storage.get("point")
  BaseUPG1 = storage.get("Base_UPG1")
  ValeurClic = storage.get("Point_Gain")
  BaseBoost = storage.get("formula_v1")
  MultiBoost = storage.get("formula_v2")
  MultiUPG1 = storage.get("Multi_UPG1")
  MultiUPG1Lvl = storage.get("Multi_UPG1Lvl")
  MultiUPG2Lvl = storage.get("MultiUPG2_Lvl")
  MultiUPG2 = storage.get("MultiUPG2")
  ExpondentalBoost = storage.get("formula_v3")
 
  
  if Points == undefined then Points = 0 end
  if BaseUPG1 == undefined then BaseUPG1 = 10 end
  if ValeurClic == undefined then ValeurClic = 1 end
  if MultiUPG1 == undefined then MultiUPG1 = 250 end
  if MultiUPG1Lvl == undefined then MultiUPG1Lvl = 0 end
  if BaseBoost == undefined then BaseBoost = 0 end
  if MultiBoost == undefined then MultiBoost = 1 end
  if MultiUPG2 == undefined then MultiUPG2 = 10000 end
  if MultiUPG2Lvl == undefined then MultiUPG2Lvl = 0 end
  if ExpondentalBoost == undefined then ExpondentalBoost = 1 end
// defining importent values---------------------------------------------  
  menu = []
  msg = ""
  msgTimer = 0

  Button = class
    constructor = function(x, y, width, height, id)
      this.x = x
      this.y = y
      this.width = width
      this.height = height
      this.id = id
      this.oldW = width
      this.oldH = height
    end
  end
  
  buttons = []
  buttons.push(new Button(-175, -75, 45, 45, "1"))
  buttons.push(new Button(175, 70, 45, 45, "2"))
  buttons.push(new Button(150, -75, 100, 40, "3")) 
  buttons.push(new Button(150, -30, 100, 40, "4")) 
  buttons.push(new Button(150, 15, 100, 40, "5"))
  buttons.push(new Button(-175, 40, 40, 40, "6"))
  buttons.push(new Button(-120, -75, 45, 45, "7"))
end
//main code--------------------------------------------------------------
update = function()  
  if msgTimer > 0 then msgTimer -= 1 end
  
//point formula(sub) 
  ValeurClic = floor((1 + BaseBoost) * MultiBoost) ^ ExpondentalBoost
//button functions(sub)  
  for b in buttons
    local targetW = b.width
    local targetH = b.height
    
    if checkRectMouseHover(b.x, b.y, b.width, b.height) then
      targetW = b.width + 15
      targetH = b.height + 15
      
      if b.id == "2" and mouse.press then 
        Points += ValeurClic 
      end
      
      if b.id == "1" and mouse.press then
        storage.set("point", Points)
        storage.set("Base_UPG1", BaseUPG1)
        storage.set("Point_Gain", ValeurClic)
        storage.set("Multi_UPG1", MultiUPG1)
        storage.set("Multi_UPG1Lvl", MultiUPG1Lvl)
        storage.set("formula_v1", BaseBoost)
        storage.set("formula_v2", MultiBoost)
        storage.set("MultiUPG2_Lvl", MultiUPG2Lvl)
        storage.set("MultiUPG2", MultiUPG2)
        storage.set("formula_v3", ExpondentalBoost)
        msg = "GAME SAVED!"
        msgTimer = 120
      end

      if b.id == "3" and mouse.press then 
        if Points >= BaseUPG1 then
          Points -= BaseUPG1
          BaseBoost += 1
          BaseUPG1 = floor(BaseUPG1 * 1.25)
          msg = "UPGRADE BOUGHT!"
          msgTimer = 120
        end
      end
      
       if b.id == "4" and mouse.press then
        if Points >= MultiUPG1 and MultiUPG1Lvl < 50 then
          Points -= MultiUPG1
          MultiUPG1Lvl += 1
          MultiBoost = (MultiBoost * 1.5)
          MultiUPG1 = floor(MultiUPG1 * 2)
      end
    end
        if b.id == "5" and mouse.press then
          if Points >= MultiUPG2 and MultiUPG2Lvl < 10 then
            Points -= MultiUPG2
            MultiUPG2Lvl += 1
            MultiUPG2 *= 10
            MultiBoost = (MultiBoost * 2)
          end
        end

          if b.id == "6" and mouse.press then
            menu = [1]
            screen.clear("Black")
            print("Button 1 pressed!") // Output to the console
        end
          
          if b.id == "7" and mouse.press then
        storage.set("point", 0)
        storage.set("Base_UPG1", 0)
        storage.set("Point_Gain", 0)
        storage.set("Multi_UPG1", 0)
        storage.set("Multi_UPG1Lvl", 0)
        storage.set("formula_v1", 0)
        storage.set("formula_v2", 1)
        storage.set("MultiUPG2_Lvl", 0)
        storage.set("MultiUPG2", 0)
        storage.set("formula_v3", 0)
        msg = "GAME RESET!"
        msgTimer = 120
      end
    end
      
  
//button visuals(sub)
    if b.oldW < targetW then b.oldW += 1
    elsif b.oldW > targetW then b.oldW -= 1
    end
    
    if b.oldH < targetH then b.oldH += 1
    elsif b.oldH > targetH then b.oldH -= 1
    end
  end
end
  
  if menu == [0] then end
    draw = function() 
      screen.clear("rgb(85,198,255)")
      screen.drawText("POINTS: " + abbreviate(Points),  -150, 75, 20, "Black")
   
      
      
  if msgTimer > 0 then
    screen.drawText(msg, 0, 60, 15, "rgb(255,255,255)")
  end
  
  for b in buttons
    if b.id == "2" then
      screen.drawSprite("point_upgrades", b.x, b.y, b.oldW, b.oldH)
    elsif b.id == "1" then
      screen.drawSprite("save", b.x, b.y, b.oldW, b.oldH)
    elsif b.id == "3" then
      screen.fillRect(b.x, b.y, b.oldW, b.oldH, "orange")
      screen.drawRect(b.x, b.y, b.oldW, b.oldH, "white")
      screen.drawText("+1 BaseGain: " + abbreviate(BaseUPG1), b.x, b.y, 12, "white")
    elsif b.id == "4" and BaseUPG1 >= 78 and MultiUPG1Lvl < 50 then
      screen.fillRect(b.x, b.y, b.oldW, b.oldH, "orange")
      screen.drawRect(b.x, b.y, b.oldW, b.oldH, "white")
      screen.drawText("1.5x Multiplier: " + abbreviate(MultiUPG1), b.x, b.y, 12, "white")
    elsif b.id == "5" and MultiUPG1 >= 1000 and MultiUPG2Lvl < 10 then
      screen.fillRect(b.x, b.y, b.oldW, b.oldH, "orange")
      screen.drawRect(b.x, b.y, b.oldW, b.oldH, "white")
      screen.drawText("2x Multiplier:" + abbreviate(MultiUPG2), b.x, b.y, 12, "white")
    elsif b.id == "6" and MultiUPG2 >= 100000 then
      screen.drawSprite("prestige_menu", b.x, b.y, b.oldW, b.oldH)
    elsif b.id == "7" then
      screen.drawSprite("reset", b.x, b.y, b.oldW, b.oldH)
    end
  end
end

Oh, there's a very simple method you can use. For example, create a variable for your mode and define what we draw if we're not in that mode. I'll give you an example.

init = function()
  stade = ""
  color = "rgb(0,170,255)"
  color2 = "rgb(0,98,147)"
end

update = function()
  if keyboard.press.A then
    stade = "prestige_mode"
  elsif keyboard.press.B then
    stade = "main"
  end
  if stade == "prestige_mode" then
    color = "rgb(0,255,85)"
    color2 = "rgb(0,147,49)"
  elsif stade == "main" then
    color = "rgb(0,170,255)"
    color2 = "rgb(0,98,147)"
  end
end

draw = function()
  screen.clear(color)
  local text = ""
  if stade == "prestige_mode" then
    text = "we are in the prestige mode"
  elsif stade == "main" then
    text = "we are in the main"
  else
    text = "I don't know where we are"
  end
  screen.drawText(text,0,0,12,color2)
end

Arch_1 yes, it can work too; you just need to create the Menu object in another file, and then at the end of the update and draw functions do the following:

update = function()
  // The other things...
  if menu then menu.update() end
end
draw = function()
  // The other things...
  if menu then menu.draw() end
end

And the variable menu (withlowercase) must be false

When you want to activate the menu you just use Menu.init(), keeping in mind that Menu is the object that will hold the menu information, that I mentioned earlier

for some strange reason instead of it working every tick when I update the folder it changes the screen its very strange because the update function is in there it just only regesters when I update the code

the rest of the system works I just need it to regester it instantly instead of me manually updating the code slightly for it to regester

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community