News: Welcome back to Bullworth! If you haven't already, you will need to reset your password..


Author Topic: [LUA TUTORIAL] Making a basic menu  (Read 5883 times)

0 Members and 1 Guest are viewing this topic.

Offline Unknownsoldier

  • Hero Member
  • ****
  • Posts: 2,773
    • View Profile
[LUA TUTORIAL] Making a basic menu
« on: February 05, 2015, 08:14:53 PM »
Making a menu in Bully is very easy. It can be done in a matter of seconds to minutes, depending on how big your menu table is.
Today I will show how to make a simple, basic menu.

First off, you want to setup your table. It doesn't matter what script you are using. Both STimeCycle and ArcRace1 should work fine. Just make sure the names aren't already used, which you normally won't have to worry about. To setup your table, you can either do it all in the MissionSetup(or main), or create your own function that just holds the menu table. In this tutorial, we will create a new function(NOT a new script). Go anywhere in your script. Make sure you are not creating a new function, inside another function. Like this:
Code: [Select]
F_MenuTableSetup()

end

Now, we want to create the table itself. To create a table, add this to your new function:
Code: [Select]
new_table = {}
^ This will create a table named "new_table". You can change the name to any name you want. However, do NOT name it "table", or it will cause problems, due to it already being a reserved variable as I said in an earlier post.

Now that we have the table created, we need to insert contents into our new table. To do this, we can use: table.insert
It can also be defined in a local, but for this tutorial, we will just use it directly. Note the table names will not be shown in game, unless you use TextPrintString, and put the name.

Code: [Select]
F_MenuTableSetup()
  new_table = {}
  table.insert(new_table, {Text1 = "one", sec = 2})
  table.insert(new_table, {Text1 = "two", sec = 4})
  table.insert(new_table, {Text1 = "three", sec = 5})
  table.insert(new_table, {Text1 = "four", sec = 3})
end

You can edit any of the values here, and add more. Here are the parimeters of the table.insert function:
table.insert(Table_Name, {Variable1 = value here, Var2 = val here}) -- And it can go on and on.

Remember that ALL table variables, can be equal to anything. Not just strings. It can be equal to 1 and 2 and so on.

To make the table work, you MUST call it in a main function. So just put F_MenuTableSetup() in a function that only runs once.

We have gotten the table/menu setup now, we just need to make the menu navigator itself. Before we can even make the menu able to navigate, we need to create a new variable.

I like to make it at the very top of my scripts, so it looks neater. Make sure you don't repeat the variable, or the menu won't be able to navigate.

So to do this, we can go to the top of the script(NOT IN A FUNCTION), and make a variable. I will call mine "l_0_0" since it is easy to remember.

When you do that, it should look similar to this:
Code: [Select]
local l_0_0 = 1

function MissionSetup()
  AreaTransitionXYZ(0,270,-110,7)
  PlayerSetHealth(1000)
end

function MissionCleanup()
  gMissionRunning = false
  PlayerSetHealth(PedGetMaxHealth(gPlayer))
  PlayerSetControl(1)
  PedSetAITree(gPlayer, "/Global/PlayerAI", "Act/PlayerAI.act")
  collectgarbage()
end

main = function()
  gMissionRunning = true
  WeaponRequestModel({
  396,
  395,
  394,
  393,
  392,
  391,
  390})
  while gMissionRunning do
    Wait(0)
   end
end

This script^ is NOT for use, it is just showing how the variable should be placed in your script.

It is probably best to keep it equal to 1, so it will be realistic.  We can finally, move on to the buttons.

This is the final part in the script. It will be a bit harder to explain, and a bit more complicated to understand.

Instead of making every button binded for 1 local, we will use a shortcut that I learned from DaBOSS.

Now, you should have 1 repeat loop in your main function. If not, there should be a "while loop" there. Nomatter what it is. Under "repeat" or "while ___ do", put the name of the buttons function.
You can name it whatever you want, but I will be calling it "TL_Buttons".

In the function, you need to think of the 2 buttons you want to use for navigation. And for some people, a select button. I will use the arrow keys.

I really don't know how to explain this part, so I will give you an example of the function.

Code: [Select]
TL_Buttons = function()
  if IsButtonBeingPressed(0, 0) then
    l_0_0 = l_0_0 - 1 -- l_0_0 is equal to 1 number lower than it currently is
if l_0_0 < 1 then -- if l_0_0 is less than 1
  l_0_0 = table.getn(new_table) -- table.getn gets the length of a table
end
  elseif IsButtonBeingPressed(1, 0) then
    l_0_0 = l_0_0 + 1 -- l_0_0 is equal to 1 number above itself
if l_0_0 < table.getn(new_table) then -- if l_0_0 is higher than the length of table (This will prevent possible menu stops)
  l_0_0 = 1 -- makes l_0_0 equal to 1
end
  elseif IsButtonBeingPressed(3, 0) then
    -- Here, you put what you want to happen
TextPrintString(new_table[l_0_0].Text1, 5, 1)
end
To get something from a table, you need to select it. like TABLENAME[VARIABLE].OPTION

That is how you make a mod menu.  If you need any help, leave comments, and I will try to answer.

Sorry if I misspelled anything.

Thanks to DaBOSS54320 for helping on making menus