Re: Game Rules scripting entry point
Posted: 2014-09-10, 06:19
I have noticed my threads very rarely end with my posting the results of solving my problems, so in a rare case of getting back on track, I am going to post details of my GameType component which is used as an entry point for the game rules and lua code.
First up is the CompGameType. This is a simple component with only one variable, a bool called GameStarted:
The components are fairly simple to make, I wanted to focus a bit more on the lua script. The first element of the script, the dofile:
At this point in time, this is not absolutely neccesary, but I am building it in for future proofing. This loads and runs the contents of FileLoader.lua file. FileLoader contains scripts that will need to be accessed by a lot of different files, so they are called globally. The contents of FileLoader is only a dofile for Utility.lua at the moment. In Utility.lua I have a wrapper for os.time() :
This is more for future proofing and to provide an easy example of how lua can be used.
Now we construct the script version of the component:
Here we initiate the script instance for GameType, the will essentially be the 'class' we are using when creating functions and variables in our script.
We retrieve the entity we are attached to. This is the map placed entity with the GameType component added to it.
We retrieve the GameType component and name it GameState. This can be whatever you want, but this is the 'class' you will call to get() and set() the component member variables.
Next up are the variables:
kGameStartTime is defined explicitly as nil, as this will hold a time value, but we don't want any value placed in it yet.
kGameLoadTime is a time value that is being recorded from the moment the entity loads the game.
kGameStartDelay is the time in secs we are going to delay before changing the GameStarted component variable from false to true, so the game can start.
The script (GameType) is returning the value of the (GameState) component member variable ("GameStarted").
The next section of code is so well commented I don't need to explain any further...
Then we need a condition to test the state change. This we place in the Think() function.
The final function isn't used yet, but it is ready for deployment...
I have attached the Component and Script files for this to the post, although I haven't included the FileLoader and Utlilty lua files, you can work around that easily enough by changing Utility.GetTime() to os.time()
First up is the CompGameType. This is a simple component with only one variable, a bool called GameStarted:
Code: Select all
TypeSys::VarT<bool> m_GameStarted; /// Has the game started? Returns True or False.
Code: Select all
dofile("Games/Duel/Scripts/FileLoader.lua")
Code: Select all
Utility = {}
function Utility:GetTime()
return os.time()
end
Now we construct the script version of the component:
Code: Select all
local GameType = ... -- Retrieve the ComponentScriptT instance that is responsible for this script.
local Entity = GameType:GetEntity()
local GameState = Entity:GetComponent("GameType")
We retrieve the entity we are attached to. This is the map placed entity with the GameType component added to it.
We retrieve the GameType component and name it GameState. This can be whatever you want, but this is the 'class' you will call to get() and set() the component member variables.
Next up are the variables:
Code: Select all
-- GameType Variables
GameType.kGameStartTime = nil
GameType.kGameLoadTime = Utility.GetTime()
GameType.kGameStartDelay = 15
kGameLoadTime is a time value that is being recorded from the moment the entity loads the game.
kGameStartDelay is the time in secs we are going to delay before changing the GameStarted component variable from false to true, so the game can start.
Code: Select all
-- Returns a boolean value to indicate if game has started
function GameType:GetGameState()
return GameState:get("GameStarted")
end
The next section of code is so well commented I don't need to explain any further...
Code: Select all
-- Sets the bool value of GameStarted.
function GameType:SetGameState(state)
-- if the state sent doesn't match the current game state
if state ~= GameType.GetGameState() then
-- set the current game state to the new state
GameState:set("GameStarted", state)
-- if game has changed to started
if GameType:GetGameState() then
-- set the game start time
GameType.kGameStartTime = Utility.GetTime()
self:ResetGame()
end
end
end
Code: Select all
-- Run the update logic for the entity from Think()
function GameType:Think(FrameTime)
-- if the game isn't running yet
if not self:GetGameState() then
-- if current time is after game start delay
if Utility.GetTime() > (self.kGameLoadTime + self.kGameStartDelay) then
-- start the game
self:SetGameState(true)
-- if the game has started
if self:GetGameState() then
-- post the message in console
Console.Print("GameType:Think(), Game has started\n")
end
end
end
end
Code: Select all
-- Reset player scores, entities and respawn players
function GameType:ResetGame()
end