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 statementFirstly the if statement. It's pretty basic really... here's the syntax:
if
expression then
code to runend
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.randommath.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:
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...
if PlayerGetHealth() < 10 then
PedSetActionNode(gPlayer,"/Global/Player","ActPlayer.act")
end