Game Rules scripting entry point

Get technical support about the C++ source code and about Lua scripts for maps, entities, GUIs, the console, materials, etc. Also covered are the Cafu libraries and APIs, as well as compiling, linking, and the build system.
SoulRider
Posts:95
Joined:2014-04-06, 00:16
Re: Game Rules scripting entry point

Post by SoulRider » 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:

Code: Select all

TypeSys::VarT<bool>        m_GameStarted;           /// Has the game started? Returns True or False.
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:

Code: Select all

dofile("Games/Duel/Scripts/FileLoader.lua")
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() :

Code: Select all

Utility = {}

function Utility:GetTime()
    return os.time()
end
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:

Code: Select all

local GameType = ...   -- Retrieve the ComponentScriptT instance that is responsible for this script.
local Entity = GameType:GetEntity()
local GameState = Entity:GetComponent("GameType")
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:

Code: Select all

-- GameType Variables
GameType.kGameStartTime = nil
GameType.kGameLoadTime = Utility.GetTime()
GameType.kGameStartDelay = 15
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.

Code: Select all

-- Returns a boolean value to indicate if game has started
function GameType:GetGameState()
    return GameState:get("GameStarted")
end
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...

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
Then we need a condition to test the state change. This we place in the Think() function.

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
The final function isn't used yet, but it is ready for deployment...

Code: Select all

-- Reset player scores, entities and respawn players
function GameType:ResetGame()
end
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()
Attachments
CompGameType.hpp
(1.93KiB)Downloaded 424 times
CompGameType.cpp
(2.41KiB)Downloaded 399 times
GameType.lua
(1.74KiB)Downloaded 397 times
User avatar
Carsten
Site Admin
Posts:2170
Joined:2004-08-19, 13:46
Location:Germany
Contact:

Re: Game Rules scripting entry point

Post by Carsten » 2014-09-10, 09:57

Thanks for the detailed description! :-D

As a minor stylistic nitpick, I would recommend to not name methods that deal with simple booleans GetGameState() and SetGameState(), because the "State" seems to imply much more than a plain boolean. Better choose a name that reflects the underlying boolean type, and matches the underlying variable name. For example, IsGameRunning() or IsGameStarted() were my choice for the "get" method, and StartGame(bool) for the "set" method. ;-)
Best regards,
Carsten
SoulRider
Posts:95
Joined:2014-04-06, 00:16

Re: Game Rules scripting entry point

Post by SoulRider » 2014-09-10, 16:14

Sound advice, and actually, something that I would normally have done myself :D

I have updated the functions on my side, although I'll leave the posted files as are. I thought it was time for me to give a little something back, to save people from having to ask you as many questions as I have!! :D
User avatar
Carsten
Site Admin
Posts:2170
Joined:2004-08-19, 13:46
Location:Germany
Contact:

Re: Game Rules scripting entry point

Post by Carsten » 2014-09-12, 17:04

SoulRider wrote:I thought it was time for me to give a little something back, to save people from having to ask you as many questions as I have!! :D
:-D :thanks:
Best regards,
Carsten
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests