ARGolfStatsDisplay

From Blue Mars Developer Guidebook

Jump to: navigation, search
There are security restrictions on this article

Contents

Overview: Using a Flash Material to display recent Golf games

Image:flashMtlEx_inGame.png

This example demonstrates setting up and controlling a Flash Material, and downloading and displaying XML results from a server. The Lua script downloads XML results via a PHP script on an external server by using Web API; the downloaded file is saved to the user directory. The results are read into a Lua script which controls the Flash Material. Included on this page:


Flash & Material Editor setup

Assigning Flash Material in the Material Editor

To assign the Flash Material in the Material Editor:

  1. the Diffuse texture references the .swf file (levels/ar/common/libs/ui/golf/golf_clubhouse_recentgames.swf)
  2. the TexType is set to Auto 2D-Map


Image:flashMtlEx_mtlEditor.png

The complete setup for this example includes:

  • the display model placed in the level as a GeomEntity with the golf_display_games.mtl material assigned. Pictured above, the front sub-material contains the Flash and the sides sub-material is a simple black diffuse texture.
  • the ARGolfStatsDisplay entity placed in the level
  • an ARAvatarTrigger placed in the level to activate the entity, which is
    • launched with the trigger's ScriptCommand EntityNamed("ARGolfStatsDisplay1"):Start()
    • set to Idle with the trigger's ScriptCommandLeave EntityNamed("ARGolfStatsDisplay1"):Stop()


Flash file

This zip file contains the .fla and the .swf files for reference: File:RecentGolfGames.zip.

The simple .fla file contains some Dynamic Text fields and graphics; embedded characters for the Dynamic Text fields are pictured below, along with other settings.

Image:flashMtlEx_dynTextEmbed.png

More details including Publish Settings can be found on the HUD page.


ActionScript

The ActionScript can be viewed in the sample file by right-clicking on the first keyframe (with the "a" in it) and selecting Actions.

A variable is declared for each Dynamic Text field, so it can be called in the Lua script with MaterialFlashSetVariable. The clearDisplay function is defined, which is invoked in the Lua script with MaterialFlashInvoke.

var game1;
var name1_1;
var score1_1;
var name1_2;
var score1_2;
var name1_3;
var score1_3;
var name1_4;
var score1_4;
var game2;
var name2_1;
//etc...

_global.clearDisplay = function()
{ 
	for (var i = 1; i<=5; i += 1) {
		this["game" + i] = "";
		for (var j = 1; j<=4; j += 1) {
			this["name" + i + "_" + j] = "";
			this["score" + i + "_" + j] = "";
		}
	}	
	trace("GolfDisplay:clearDisplay");
};


Scripts

Lua script

The Lua script is launched as described above, then:

  • the GetData state starts the download with ARSubmitGet, and moves on when the download is complete, checking with Game.IsDownloading
  • the ReadAndDisplayXMLScores state then parses the XML into a Lua table with ARLoadXML and the definition file
  • the clearDisplay ActionScript function is invoked with MaterialFlashInvoke, and the Dynamic Text fields are filled with MaterialFlashSetVariable according to the data in the Lua table
  • the timer is set according to the fUpdateAfterSeconds Property, upon which the GetData state is entered again
ARGolfStatsDisplay =
{
  Properties = 
  {
    limit = 5,
    recentgamesdef = "Levels/AR/Common/Libs/UI/Golf/def_recentgames.xml",
    fUpdateAfterSeconds = 60,
  },

  States = 
  {
    "GetData",
    "ReadAndDisplayXMLScores",
    "Idle",
  },

  Section = 
  {
    "The Malihini",
    "Seaside", -- (Holes 1-3)
    "The Sands", -- (Holes 4-6)
    "Shadyside", -- (Holes 7-9)
    "The Front Nine", -- (Holes 1-9)
  },
  
  SectionPar = {"3", "13", "12", "11", "36"},
  
  Hud = 
  {
    recentGames = "Levels/AR/Common/Libs/UI/Golf/golf_clubhouse_recentGames.swf",
  },

  Editor=
  {
    Icon="AR_Default.bmp",
    ShowBounds=0,
  },
  
  xmlresults = "ARGolfStatsGetResults.html",
  displayxml = "http://member.bluemars.com/gameui/golf/leaderboard/showrecentgolfgamesxml.php",
  xmltable = {},
  golf_display = {},
  REFRESH_TIMERID = 100,
}

function ARGolfStatsDisplay:Start() --enter av trigger in club house 
  System.Log("ARGolfStatsDisplay:Start()");
  self:Activate(1);
  ARDebug(3);
  self.golf_display = System.GetEntityByName("golf_games_board");
  self:GotoState("GetData");
end

function ARGolfStatsDisplay:Stop() --leave av trigger in club house  
  System.Log("ARGolfStatsDisplay:Stop()");
  self:Activate(0);
  self:GotoState("Idle");
end

function ARGolfStatsDisplay:OnReset()
  self:GotoState("Idle");
end

-- states --

ARGolfStatsDisplay.GetData =
{
  OnBeginState = function(self)
    ARSubmitGet(self.displayxml, self.xmlresults, {limit=self.Properties.limit});
  end,
  OnUpdate = function(self,time)
    if Game.IsDownloading() then
      System.Log("ARGolfStatsDisplay.GetData Downloading");
    else
      System.Log("ARGolfStatsDisplay.GetData Retrieved scores");
      self:GotoState("ReadAndDisplayXMLScores");
    end
  end,
}

ARGolfStatsDisplay.ReadAndDisplayXMLScores =
{
  OnBeginState = function(self)
    System.Log("ARGolfStatsDisplay.ReadAndDisplayXMLScores");
    self.xmltable = ARLoadXML(self.Properties.recentgamesdef,self.xmlresults);                                     
    self.golf_display:MaterialFlashInvoke(0,0,0,"clearDisplay"); 

    for i,game in ipairs (self.xmltable.games) do
      local year, month, day, time = string.match(game.time, "(%d%d%d%d)%-(%d%d)%-(%d%d)%s(%d%d%:%d%d)");
      local date = time .." " .. month .. "/" .. day;
      self.golf_display:MaterialFlashSetVariable(0,0,0,"game".. i, "Game #".. i .."   ".. date .."   ".. self.Section[game.section] .."   Par ".. self.SectionPar[game.section]);

      for j,player in ipairs (game.players) do
        self.golf_display:MaterialFlashSetVariable(0,0,0,"name".. i .."_".. j, player.name);
        self.golf_display:MaterialFlashSetVariable(0,0,0,"score".. i .."_".. j, player.totalscore);
      end 
    end           
    self:SetTimer(self.REFRESH_TIMERID, self.Properties.fUpdateAfterSeconds*1000);        
  end,
  OnTimer = function(self, timerId)
    if (timerId == self.REFRESH_TIMERID) then
      self:GotoState("GetData");
    end
  end,
  OnEndState = function(self)
    self:KillTimer(self.REFRESH_TIMERID);
  end,
}

ARGolfStatsDisplay.Idle =
{
  OnBeginState = function(self)
    System.Log("ARGolfStatsDisplay.Idle");
  end,
}


XML Definition File

The definition file is the first argument passed into ARLoadXML. The ARLoadXML Definition Files section contains further information.

<?xml version="1.0"?>
<Definition root="golf_games_recent">
  <Array name="games" elementName="game">
    <Property name="id" type="int"/>
    <Property name="section" type="int"/>
    <Property name="time" type="string"/>
    <Array name="players" elementName="player">
      <Property name="name" type="string"/>
      <Property name="totalscore" type="int"/>
    </Array>
  </Array>
</Definition>


Sample downloaded dataFile

The dataFile is the result of the ARSubmitGet download, and is then parsed with ARLoadXML. It is saved to the user directory (in this case My Documents/My Games/BlueMars/argolfstatsgetresults.html), a location specified by the self.xmlresults variable in the Lua script. Here's a sample dataFile:

<golf_games_recent>
<games>
  <game id="1620" section="1" time="2010-06-30 16:14:17">
    <players>
      <player name="mag1" totalscore="4"/>
    </players>
  </game>
  <game id="1619" section="2" time="2010-06-25 19:21:35">
    <players>
      <player name="mag1" totalscore="18"/>
      <player name="tim" totalscore="24"/>
      <player name="bryce" totalscore="25"/>
    </players>
  </game>
  <game id="1618" section="1" time="2010-06-23 16:23:38">
    <players>
      <player name="mag4" totalscore="12"/>
    </players>
  </game>
  <game id="1617" section="1" time="2010-06-20 20:48:30">
    <players>
      <player name="Abjab" totalscore="8"/>
    </players>
  </game> 
  <game id="1616" section="2" time="2010-06-20 14:34:41">
    <players>
      <player name="Amanda_Nieuport" totalscore="14"/>
    </players>
  </game>
</games>
</golf_games_recent>


PHP script

Below is the PHP script which queries the Golf database and puts together the XML for the dataFile. It is referenced with self.displayxml in the Lua script (showrecentgolfgamesxml.php).

See the more elaborate High Score Server example for further information.


Image:flashMtlEx_PHP.png

Problems with this wiki page? Contact us either by: Support Email or Support Ticket System

Blue Mars Guidebook Privacy Policy
Blue Mars Guidebook Community Guidelines

Personal tools