As one or the other of you, I have also started to play a bit around with Lua (you might know why ^^). Actually I do have some background in coding… but that’s an awfully long time ago. So maybe I can share a bit what I learn, if that is interesting at all ;-).
Under this link you will find a translation into German.
For the beginning I had this scenario in my mind:
I want to control the behaviour of a setter. Based on a random value
(which I want to influence), the settler should have a tendency to do
something. This could be simply greeting or hugging another settler,
or something more complex.
As it is not yet known, how observing, actions, etc. will work, I have decided to focus on pure Lua and leave it open, how this could be connected to the game.
So the following code askes the user for 2 inputs. On one side, you have to tell the script with which probability you want an event to be executed. On the other side, you can tell the script how often it should run (this should simulate for me how often the settler meets someone else). After that, a random number is generated for each time “your settler is meeting someone else” (or simply for each time the responsible code is executed). Finally I count the times where we have a positive response (e.g. the settler greets or hugs someone)… just to see if the script is working correctly and the probability is met, kind of.
Here goes the code:
Here goes a short explanation of the lines.
Line 3: “print()” is a function which shows on the screen what is included in the brackets. Here it is used to tell the user to enter a number (probability of the event).
Line 4: “io.read()” is another function, which will wait for input from the user. Line 3 only prints the text, we need to use io.read() to get the input. The parameter “*n” allows that only numbers are entered. Finally the result of the input should be stored in a variable called “probability_plan”.
Line 6: Same as line 3, but other message to the user ^^. Here we ask for the time this event should happen.
Line 7: Same as line 4, but saved in another variable.
Line 9: A variable “probability_yes” is initialized with the value 0. We will later increase this value for each time the event happens.
Line 11: Another function… “math.randomseed()”. This one needs a bit explanation. There are things which do not belong together and one of them are computers and “randomness”. Simply, a computer cannot generate a random number. Instead, it is using some mathematical formulas and functions to simulate one. The problem with this is, that somewhere all this “algorithm” has to start and if the starting point is always the same, the “random” numbers you will get, will be always the same. Here the function “math.randomseed()” comes into play. It allows the programmer to decide what the starting point is. Clever as coders are, they feed this little creature with the system time, available in Lua via another function “os.time()”. So, as long as you do not have a machine to travel back in time, the chances are high, that this starting point is different every time you start the code, i.e. different random numbers will be generated.
Line 13: Now we enter into a loop. “for” is one of the ways how you can repeat a part of your code based on conditions. Here we have a counter (i), a maximum of how often the loop should be executed (amount_runs, which was filled by the user). The “1” at the end simply controls the steps by which the counter (i) will increase until he reaches the value stored in “amount_runs”).
Line 14: Simply starts the block of code which belongs to the for-loop.
Line 16: “math.random()” is now the function which generates random numbers. Remember, the starting point was defined in line 11. The function accepts some parameters, of which you see here two… “1” and “100”. This tells the function to generate a number which is between 1 and 100 (including both). The result is saved in a variable “random_number”.
This variable is a bit special as it is defined as “local”. Local variables are “deleted” again once you leave the “area” in which they are defined. In this case, the variable will no longer exist, once we leave the for-loop (which is after line 24).
Line 17: Now we know the random number and we know the probability with which we wanted the event to occur. This line should decide if the condition is met or not. Depending on the result, either one part of the code (could control one kind of behaviour of the settler) or another part (could control the other kind of behaviour of the settler) is executed.
We actually compare here simply the random number with the probability we would like to see. If the number is smaller than the probability, we consider this as a trigger of the behaviour we want to see.
Line 18: Here we land only if the condition is met, i.e. the event will be triggered. If the user has selected a probability of 33%, in average we will end up every third time in this part of the code.
The function “print()” is already known from the beginning of the code. What is different here are two things. We print not only a text, but also a varaible (you remember the counter called “i”). The two dots “…” are merging the variable with the text afterwards… and the variable “random_number” and the text again.
Line 19: The event was triggered, so we want to increase now the count of the variable we defined in line 9. This is for tracking reasons. We will use it a bit later. Pay attention that this variable is not defined as local… because “use it later” means outside the for-loop. So it cannot be defined local.
Line 20: Here starts the second block of code which will be executed if the condition in line 17 is not met.
Line 21: More or less the same as line 18.
Line 22: We end the “if” here.
Line 24: We end the “for” here.
Line 26: By applying some math, we calculate the percentage telling us how often the event happened.
Line 27: … and we show it on the screen.
So far a first look into Lua by myself. I hope this is kind of interesting / useful.
Update: Sorry, the initial link to the picture was wrong and did not show up :-/.
Update: Here the “copy & paste”-version of the code:
-- Random Numbers print("enter probability as a number (0-100%):") probability_plan=io.read("*n") print("enter amount of runs a number:") amount_runs=io.read("*n") probability_yes=0 math.randomseed(os.time()) for i=1, amount_runs, 1 do local random_number=math.random(0,100) if (random_number<probability_plan) then print(i .. " " .. random_number .. " Yes") probability_yes=probability_yes + 1 else print(i .. " " .. random_number .. " No") end end probability_actual=100/amount_runs*probability_yes print("probability: " .. probability_actual)