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


Author Topic: do random function?  (Read 1726 times)

0 Members and 1 Guest are viewing this topic.

Offline embas10

  • Jr. Member
  • **
  • Posts: 27
  • Gender: Male
    • View Profile
do random function?
« on: August 25, 2015, 07:17:03 AM »
Hey all, i have a question.
1. If i press this, gPlayer will use node1, and sometimes if we press that to, gPlayer will do node2
2. If gplayer health less then 10, gplayer will do node1 or node2
On that above, only example.
I mean, if gplayer bla..bla..bla.. Then he will do 1 function from 2 functions i gave
If you know what i mean, reply and give me a example code.
Thx

Offline DaBOSS54320

  • Hero Member
  • ****
  • Posts: 3,398
  • Gender: Female
    • View Profile
Re: do random function?
« Reply #1 on: August 25, 2015, 04:57:25 PM »
Well you'll need to learn 2 things for this, one how the if statement works, and 2 how the math.random function works.

If statement
Firstly the if statement. It's pretty basic really... here's the syntax:
if expression then
  code to run
end

expression is some expression that should evaluate to true/false, or it can be a constant value as well. For example you could put true (a constant value) and the code would run. You could put false (a constant value) and the code would not run. You could also put something like x < 7 and if x is less than 7, that expression (the expression being x < 7) will evaluate to true, so the code will run. There are more relational operators you can use as well, such as == which returns true only if the 2 values specified are equal. For example x == 6 would only evaluate to true if x was 6. You can also put a variable there and it's value will be used. For example if you put x after if, the code would run if x was equal to true, and would not run if it was equal to false. Then lastly you can also call a function, and the value returned by the function will be used. For example if you put PedIsDead(pete) the code would if pete (a ped) was dead, because that function returns true/false depending on if the ped specified is dead.

math.random
math.random is a function provided by the lua language itself. It's pretty basic. It takes 2 arguments, a minimum and maximum value, and it returns a random value in between the 2. (or one of the 2 as it is inclusive).
So here's an example of how you could call the function... x = math.random(2,9). After calling that the variable x will contain the value returned by our call to math.random so x will be a random number between 2 and 9.

So... how do I do my random action node?
Well if we combine those 2 ideas above, we can play a random action node. Basically when we press the button we want to play the node, we'll use math.random to determine which node we should play. So we'd put the following code in our script's main loop:

Code: [Select]
if IsButtonBeingPressed(6,0) then
  if math.random(1,2) == 1 then
    PedSetActionNode(gPlayer,"/Global/Player","Act/Player.act")
  else
    PedSetActionNode(gPlayer,"/Global/Player","Act/Player.act")
  end
end

How do I do something if the player's health is below 10?
Well that should be pretty simple too. Basically we've been over everything you need to know for it already...

Code: [Select]
if PlayerGetHealth() < 10 then
  PedSetActionNode(gPlayer,"/Global/Player","ActPlayer.act")
end

Offline embas10

  • Jr. Member
  • **
  • Posts: 27
  • Gender: Male
    • View Profile
Re: do random function?
« Reply #2 on: August 25, 2015, 08:53:19 PM »
I know about the other things,i just ask about that math.random.
Anyway thanks man.