LUA script variables

danguru

Chieftain
Joined
Dec 30, 2011
Messages
25
I have a LUA script I use and it would be convenient if a flag or 2 or 3 ... gets saved when a game is saved and then loaded when the game is loaded from a save file.

How can I make this happen?
Or just point me to a mod that does this and I can look their LUA code.

Thanks,
Dan
 
Code:
Game:GetProperty(PropertyTextName)
Game:SetProperty(PropertyTextName, PropertyValue)

It sounds like you need these. The Get/SetProperty functions are available for Game, City, Player, Unit, and Plot objects I believe. They will be saved and loaded with the game. The BlackDeath Scenario has examples and I have seen other threads on here about them.
 
For a player, you can set and then later retrieve as like
Code:
Players[PlayerID]:SetProperty("HasEatenChesseburgers", 1)
..........
if Players[PlayerID]:GetProperty("HasEatenChesseburgers") ~= nil then
     print("This player has already feasted on a Cheeseburger")
else
     print("This player has had no cheeseburger -- and is famished")
end
The first argument in all types of "SetProperty" and "GetProperty" is a text-string you assign. The second argument in all "SetProperty" uses is the value for that property, and can be any reasonable setting such as an integer value, another text string value, or a boolean value. "GetProperty" methods only ever use the one argument.

"GetProperty" is valid in both GameplayScripts and User Interface scripts, and will access the same data across both these sides of the lua system so you can use it to send data from the Gameplay side of the lua system to the user interface side of the system.

"SetProperty" can only ever be commanded in a GameplayScript, regardless of whether the game object the property is being attached to is the game itself (Game:SetProperty) or an object within the game such as a player (Player:SetProperty) or a city (City:SetProperty) or a unit (Unit:SetProperty) or a plot (Plot:SetProperty).

Once attached to an object the Property and its last assigned value, as mentioned, is automatically saved and persisted across quitting and reloading a saved game.
 
Top Bottom