IMPORTANT: If you are doing a "blind copy and paste" of the code in my post above, you just want the 3rd and 5th block!
Wow, thanks a lot, whoward!
Don't forget bane_ ... I just edited their initial code!
- tBuildToPolicy is an two-dimensional array filled with GameInfoTypes.Buildings and GameInfoTypes.Policies.
tBuildToPolicy is a ONE dimensional array, that is
a TWO dimensional array would be
A one-dimensional array is the classic list, a two-dimensional array is the classic table.
Humans typically think of lists/tables with ascending numerical indices - "first I do this, second I do that ..." or "the treasure is in grid 20 across and 35 up". Lua doesn't care what the index is - it can be a number or a string, think a list of rooms in your house by name and the colour you're going to paint them "Hall: pale green, Kitchen: Cream, Living Room: Lilac" (I should probably never be given a job as an interior designer!) Lua arrays can also contain "holes", think of an array that indexes racing number with F1 driver, so f1Name[44] = "Lewis Hamilton", we don't have all the values of f1Name[1] to f1Name[43], only some of them. You can also mix number and string indices in the same array - we could create f1Drivers[44] = "Lewis Hamilton" and f1Drivers["Lewis Hamilton"] = 44
- We give the variables iBuildingType and iPlayer to the function BelzhorashOwesSomeoneABear().
> I understand that the Script gets iPlayer from the global "environment". But where does iBuildingType come from? Is it an global variable, too? And what Building is meant by iBuildingType?
No, both iPlayer and iBuildingType are passed from within the game core DLL (C++ code) to the Lua script. iPlayer will be ANY valid player (human and AI) in the game, and not just the current player. iBuildingType will be what the game core DLL is trying to work out if the player can construct that building type.
> Is IsHasPolicy a global function?
Not a global function, but a method on the player object. You will see
Code:
local pPlayer = Players[iPlayer]
a lot. iPlayer is the integer id of the player under consideration. Players[] is a global array of all players in the game. pPlayer is then the "thing" (an object in programming terms) that represents that player.
> What is the trigger to start the script? GameEvents.PlayerCanConstruct.Add(BelzhorashOwesSomeoneABear) is the action taken, but where do we tell the engine to fire off?
As the game starts up, all Lua scripts (provided you have set them up properly, see the 3rd link in my sig) are executed. As the
Code:
GameEvents.PlayerCanConstruct.Add(BelzhorashOwesMeaBeer)
line is NOT within a function it executes and "hooks" the event, so that every time the event "fires" (ie the game core DLL wants to know if it can possibly construct a building) the BelzhorashOwesMeaBeer(iPlayer, iBuildingType) function (technically an event handler) is executed.
HTH (but it probably just adds more questions)
W