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


Author Topic: Finding Random Integers [LUA Tutorial]  (Read 2543 times)

0 Members and 1 Guest are viewing this topic.

Offline Unknownsoldier

  • Hero Member
  • ****
  • Posts: 2,773
    • View Profile
Finding Random Integers [LUA Tutorial]
« on: November 25, 2014, 07:34:31 PM »
math.random can be used for many things in Lua. For example, finding a random option in a table, or any random number between 2 numbers. To find a random number between 2 other digits, type math.random(lower, upper). Replace "lower" with the lower number, and upper with the greater value/number. table.getn CAN also be used in here. But it's place in math.random all depends on the length of the specified table.

Only problem is if you just type:
Code: [Select]
VAR = math.random(1,19)

And not put it under a repeat/while loop, it will always be equal to the same value. If you want it to be a different random number each time, you can do it any of these 2 ways:

1:
Code: [Select]
  repeat
    R_Value = math.random(0, 17)
2:
Code: [Select]
  while true do -- true can be replaced with ANY running/true boolean.
    R_Val = math.random(0, 17)

If you're doing this whilst modding Bully, you should also include the other stuff below it like:

Wait(0)

and an end or with a repeat loop use an until.

Either way, the variables will still work the same way, and equal a different number everytime it's used. I also like to use this for grapples, along with a grapple table, then just make it so it finds a random line on the table, and perform that node hehe.

Here is an example of it in use:
Code: [Select]
local Table = {}

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

function MissionCleanup()
  gMissionRunning = false
  WeatherRelease()
  PlayerSetControl(1)
  EnablePOI()
  collectgarbage()
end

function main()
  gMissionRunning = true
  T_Setup() -- function to set up our table
  while gMissionRunning do
    local R = math.random(0,table.getn(Table))
    if IsButtonBeingPressed(3,0) then
  TextPrintString(Table[R].Text, Table[R].Time, 1)
end
    Wait(0)
  end
end

function T_Setup()
  table.insert(Table,{Text = "yo dawg", Time = 2})
  table.insert(Table,{Text = "Hello", Time = 3})
  table.insert(Table,{Text = "u wot m8", Time = 4})
end
I do prefer to use while loops, rather than repeat, but repeat does work too:
Code: [Select]
local Table = {}

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

function MissionCleanup()
  gMissionRunning = false
  WeatherRelease()
  PlayerSetControl(1)
  EnablePOI()
  collectgarbage()
end

function main()
  gMissionRunning = true
  T_Setup() -- function to set up our table
  repeat
    local R = math.random(0,table.getn(Table))
    if IsButtonBeingPressed(3,0) then
  TextPrintString(Table[R].Text, Table[R].Time, 1)
end
    Wait(0)
  until not gMissionRunning
end

function T_Setup()
  table.insert(Table,{Text = "yo dawg", Time = 2})
  table.insert(Table,{Text = "Hello", Time = 3})
  table.insert(Table,{Text = "u wot m8", Time = 4})
end

The texts I inserted are completely random.  :laugh: But I am pretty sure these 2 examples will actually work the way they are setup.
Also, in the repeat loop, you can also do:
Code: [Select]
  until gMissionRunning ~= true

That will just repeat, until gMissionRunning equals ANYTHING other than true. Not just false.

I apologize if this is not very informative, but if you have any questions about this, please reply explaining what you don't understand or just generally have questions.  8)

Also, on another note, NEVER name your table with the name "table". If you do, put a capital "T", UNLESS you are 100% sure you know exactly what you're doing.
Time also is a reserved function. if you just type "time" it may be another color, which means it was reserved for something else. Don't put a small "T" unless you are also sure you are using it right, since Lua is case sensitive.
« Last Edit: February 05, 2015, 07:37:28 PM by Unknownsoldier »