What this loop does, to explain abstractly, is, it initially checks to see if the condition is met if it is, procceed from the first subsequent line, changing the player's gold by 1 print i reduce i by the progression we chose. But beware, you want to redefine the variable AFTER everything in the code that uses it (like the print() function above it in this last code), otherwise you'll receive the modified i, not the original that prompted the loop. Since it isn't a loop, if statements are read in order until the 'end' statement and really end there. The very first one right at Line #08, the lowest of the ends, is closing the if statement ( Line #06). The second one at Line #09 closes the second loop ( Line #04), the numerical 'for', it is reached as soon as all plots from the city (defined in the first loop) are accounted for. The third one ( Line #10) - as already exposed - closes the first loop ( Line #03), it is only reached as soon as the 'for' loop reaches a 'nil' value, which means it'll keep looping until ALL your cities were accounted for, then it ends his participation. The last one, present at Line #11, or the 'higher' one is closing the whole function, as soon as you reach it, the function is over. Bringing it to our case we have 4 ends, being: Now, the 'end' statement serves to end a chunk (this is the actual name for it) of a code. If pPlot:GetPlotType() -] = PlotTypes.PLOT_MOUNTAIN then We use 'i' to indicate an 'integer', some people use it before a variable to indicate that variable will be an integer - this convention is called Hungarian Notation - that identifies a plot. This time we are also creating a variable, in this line's case, i. For each plot in pCity (defined in the first loop), do something, it is very straigthforward. What we see here is a loop through ALL plots that city has. progression is the amount by which the startValue will be increased until it reaches the endValue this increase happens each time you reach the end of that part of the chunk, in this case, riiiight before the third 'end' statement. endValue As said above, this loop stops when it reaches a value, the endValue represents that. startValue Loops increase incrementaly until they reach a value, they must start from somewhere, this is the somewhere. Remember of the reserved Lua keywords¹ and earlier exposition about Identifier. The Identifier is the name that will identify the variable you will call later. Here it is:įor Identifier = startValue, endValue, progression do In this case, we want to iterate through numbers. Yes, I did, but the prior was to get through an array of items. In our case, we look at a table called "Players" that has information on which players is which based on their ID, and we have the Player whose turn just began because the hook gave it to us (iPlayer)! So it'll compare to the list it already haves and return the correct pointer to the Player we want.Ĭlick to expand.Now, imagined reader, you say: But wait! You already told us about 'for' loops! Here you'll tell the game: "Hey game, go get this information for me". I don't know if ' function' is the proper use here, I don't study Lua nor programming, but for the sake of this post, it is now. It is also good to avoid identifiers starting with underscores that also follow with uppercase letters, like _VERSION. Although, they cannot start with a digit, be one of the reserved Lua keywords¹. Again: The Identifier is the name that will, well, identify your variable. In our case here, pPlayer will be a valid variable until before the very last 'end', which means it'll be usable anywhere in this function. local means it will only be used by anything that goes below, until it's own 'end' statement. (' local' OR nothing) Identifier = function ( Thank you LeeS) Ok, so, looking at the code line by line independently we get:Ĭlick to expand.This is how you define a variable. They're shown so that it will be easier to understand which individual line is being discussed. Note that the Red-colored line numbers aren't anything you'll ever put into your actual code. They are found in that first site I told you about, but there are a few more here (I don't know if there is an exaustive list anywhere though, sorry). That is a 'hook', something to tell the game when to run your code. And look down there! There is a new element we didn't knew before. See, now everything was defined earlier than it was used (rightly so). Line #06 if pPlot:GetPlotType() = PlotTypes.PLOT_MOUNTAIN then Line #05 local pPlot = pCity:GetCityIndexPlot(i) Line #04 for i = 0, pCity:GetNumCityPlots() - 1, 1 do Line #03 for pCity in pPlayer:Cities() do Line #01 function CheckCityMountains(iPlayer)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |