Launch a game from another game – ShiVa Engine

Launch a game from another game

This can be used in several situations:
– To create your own startup pack on a web page, to replace the default one.
– You have a big game, and you want to display something to the user quickly, to make the loading less annoying, with a custom loading progress bar or with animations.
– You have introduction movie(s), and you want to externalize them from your game to avoid the introduction management in script

Setup

First of all, create a string variable named sFileToLoadURI in your AIModel.
Setup its value in the onInit of the AIModel:

this.sFileToLoadURI ( "http://www.mywebsite.com/mypack.stk" )

Of course, replace the URL with the game to load .
Create 2 variables in the AIModel which will manage the download of the pack:
bStarted: this variable indicate if the download has started, its default value is false
bFinished: false by default , it will indicate if the download has ended.
Create now 2 handlers:
onStartDownload , call it to start the download
onStart call it when the download is complete to load the downloaded pack.
In this tutorial, we will start the downloading right after the  start and launch the game immediately after the complete download.
So in the onInit,  call the onStartDownload handler:

this.sendEvent ( "onStartDownload" )

In this handler, you just have to toggle the bStarted variable true:

this.bStarted ( true )

Now, in a onEnterFrame handler, you have to manage the download.
This management has to be only if the download can start (it means the onStartDownload handler has been called) and of course if the download is not finished:

Download process

if ( this.bStarted ( ) and not this.bFinished ( ) )
then

Then, check if the pack is installed (= the download of the pack has ended). If it is the case, set the bFinished variable to true and call the onStart handler to launch the pack.

local sURI = this.sFileToLoadURI  ( )
if ( system.isInstalled ( sURI ) )
then
this.bFinished ( true )
this.postEvent ( 0, "onStart" )

In the other case, check the installation status. If the status is negative, it means the download of the pack hasn’t been started yet, or there was an error during the download, you have to start the download (once again). If the status is greater or equals to 0, it means the download is pending, nothing to do, just to wait and check again at the next frame.

else
log.message ( system.getInstallationStatus ( sURI ) )

This log can check the state of installation
if -1 : download not started
if -2 : no stk at this URI
if between 0 and 1 : download in process
if 1 : download finished

      if ( system.getInstallationStatus ( sURI ) < 0 )
      then
         system.install ( sURI )
      end
   end
end

Note you can display a progress bar and update its value according to the installation status of the pack.

Launch the pack

When the download is over, call the onStart handler, which contains the script to launch the downloaded pack:

system.launch ( this.sFileToLoadURI  ( ), "" )

At this point, the current game closes and the downloaded one starts.
Comment : you need to push your project on your web page or to your phone to be able to try it properly


  • slackBanner