Using Cookies

On first run, or after the cookie has been erased, this program saves a random number converted to a string. On subsequent runs the program reads the saved string, converts it back to an integer, then compares it with the current random number. If the current number is greater then its string representation is saved, replacing the previous value of the cookie. (We erase the cookie when a random number exceeds 90). To try the example, compile the code and open the resultant HTML file in a browser that has cookies enabled. Refresh the browser repeatedly to see the cookie contents being retained, changed and erased.

You could store the highest score in a game similarly. If you have more data to store, you can use the methods described in the local storage demonstrations on the preceding pages to convert to and from a single string and store it as a cookie. You can also store several cookies with different identifiers.

See also the use of a cookie for passing data from a form-based application to a canvas program.

When using Version 3.0 of Smart Mobile Studio, change SmartCL.CookieStorage in the uses clause to SmartCL.Cookies, change the four other instances of Cookies to TW3Cookies and change the line Canvas.Font := '10pt verdana'; to Canvas.FontStyle := '10pt verdana';. You can download a trimmed-down project file from here. We found that some browsers enable the resultant cookies javascript code to function when run from our website but not when run directly from a folder on a PC.

Cookie Demo

If the program does not work, try another browser such as Chrome, Firefox or Edge. If you see no display at school, the security system might have blocked it. You can try instead this direct link to the program running on its own page.

unit Unit1;

interface

uses
  System.Types, SmartCL.System, SmartCL.Components, SmartCL.Application,
  SmartCL.Game, SmartCL.GameApp, SmartCL.Graphics, SmartCL.CookieStorage;

type
  TCanvasProject = class(TW3CustomGameApplication)
  private
    CurrentNum, HighestNum: Integer;
  protected
    procedure ApplicationStarting; override;
    procedure ApplicationClosing; override;
    procedure PaintView(Canvas: TW3Canvas); override;
  end;

implementation

procedure TCanvasProject.ApplicationStarting;
begin
  inherited;
  randomize;
  CurrentNum := RandomInt(100) + 1;
  var cookie :=  Cookies['CookieDemo1'];
  if cookie = '' then  // First run or after cookie has been erased
    begin
      HighestNum := CurrentNum;
      Cookies.WriteString('CookieDemo1', intToStr(CurrentNum), 50); // 50 days
    end
  else
    begin
      HighestNum := strToInt(cookie);
      if CurrentNum > HighestNum then
        begin
          Cookies.WriteString('CookieDemo1', intToStr(CurrentNum), 50);
          HighestNum := CurrentNum;
        end;
      if HighestNum > 90 then
        begin
          Cookies.Erase('CookieDemo1');
          HighestNum := 0;
          CurrentNum := 0;
        end;
    end;
  GameView.Delay := 1;
  GameView.StartSession(True);
end;

procedure TCanvasProject.ApplicationClosing;
begin
  GameView.EndSession;
  inherited;
end;

procedure TCanvasProject.PaintView(Canvas: TW3Canvas);
begin
  // Clear background
  Canvas.FillStyle := 'rgb(0, 0, 99)';
  Canvas.FillRectF(0, 0, GameView.Width, GameView.Height);
  // Display current and highest random number
  Canvas.Font := '10pt verdana';
  Canvas.FillStyle := 'rgb(255, 255, 255)';
  Canvas.FillTextF('Current number: ' + IntToStr(CurrentNum) +
                   '  Highest number: ' + IntToStr(HighestNum),  10, 20, MAX_INT);
end;

end.
    
Programming - a skill for life!

Storage and retrieval of data using local storage, cookies, HTTP and MySQL