Play.Video only once upon entering a room

Started by vision00, Tue 13/04/2010 20:10:58

Previous topic - Next topic

vision00

I want to play a video upon entering a room for the first time, but then not have it play upon returning to the room again. Doing this with the "First Time Enters Room" event does so after the fade in and it doesn't look right. I need it before the fade in, but using "Enters Room Before Fade-In" event looks right but does it everytime.  I've tried my hand at a script under "Enters Room Before Fade-In" that doesn't contain errors, but the video still plays every time.

function room_Load()
{
int intro; {
if (intro < 1)
PlayVideo("Video.mpg", 11,  1);
}
{
intro = 1;
}
}

I would think that setting Intro = 1, that upon returning it wouldn't play the video again. I guess the question is, when I leave the room and come back is intro being reset to 0, and that causes the video to play again? I've also tried the if statement: if (intro == 0) but get the same thing.

Matti

Look up the DoOnceOnly command in the manual. Or simply create a variable and turn it off when the video was played once.

Dualnames

Quote
static bool Game.DoOnceOnly(const string token)

This function gives you an easy way of making some code run only the first time that the player encounters it. It is commonly used for awarding points.

The token parameter is an arbitrary string. You can pass whatever you like in for this, but IT MUST BE UNIQUE. It is this string that allows AGS to determine whether this section of code has been run before, therefore you should make sure that you do not use the same token string in two different places in your game.

Returns true the first time that it is called with this token, and false thereafter.

Code: ags

if (Game.DoOnceOnly("playvideompg")) {
 PlayVideo("Video.mpg", 11,  1); 
}


If not totally obvious the statement Game.DoOnceOnly with the token "playvideompg", will only return true ONCE therefore video or any functions you put inside those brackets will only be processed once.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

vision00

Thanks for the quick reply. DoOnceOnly was what I needed. It was as simple as this:

if (Game.DoOnceOnly("player.ChangeRoom")) {
 PlayVideo("Video.mpg", 11,  1);

Khris

Just for reference, you can use any string you want as long as you don't use it twice.
Using "player.ChangeRoom" is fine but a bit weird.

Also, the correct way to do this without DoOnceOnly is as follows:

Code: ags
int intro;  // declare outside any function so it isn't reset

function room_Load() {
  if (intro == 0) {
    PlayVideo("Video.mpg", 11,  1);
    intro = 1;
  }
}

monkey0506

To explain the reason why Khris' example includes the variable definition outside of the function, we must consider variable scope.

To put it simply, you can think of variable scope as the lifespan of the variable. If you define a variable outside of any functions it is said to have "global" scope. Don't confuse this with a "global variable" though the same concept applies.

What "global scope" means is that a variable is accessible from every function within a given script (specifically, the script in which the variable is defined), after the point at which it is defined. If you try to use a variable before it's defined then you'll get an "Undefined symbol" error. For example:

Code: ags
int x; // variable 'x' is created with global scope

function game_start() {
  x = 5;
}

function repeatedly_execute() {
  x++; // increase value of 'x'
}


A "global variable" can then be thought of as a variable with global scope in every script (after which it is defined, including dialog and room scripts). If you're using the import/export route it is important to note that the import is considered a "declaration" and not a definition. So although you can put the import in whatever script you'd like, you can't use the variable until in and after the script in which it is defined.

The counterpart of global scope is referred to as "local scope". This means indicates that the scope is more limited. From a game-wide perspective a variable with global scope which is not defined as a global variable could be said to have "local scope" within the script in which it is defined. Another type of local scope is the scope of variables defined inside of a function. Any variables which are defined inside of a function go "out of scope", or cease to exist, at the end of the function. For example:

Code: ags
function game_start() {
  int x = 5; // variable 'x' is created with local scope within the game_start function
} // at the end of the game_start function, variables with local scope are destroyed

function repeatedly_execute() {
  x++; // error, undefined symbol 'x'
  // variable 'x' was only defined inside of the game_start function and no longer exists!
}


The line inside the repeatedly_execute function would here cause an error because, as the comments indicate, the variable was created with local scope to the game_start function. As soon as that function was closed out, the variables defined within its scope get destroyed. Any value they contained would also be lost permanently.

The purpose of local scope is for variables that you need for short-term use. For example you might use a simple integer as an iterator for a while loop. You have no real need to persist the value of that variable since you'll want to run the entire loop the next time you call the function.

Global scope comes into play when you want to use variables in multiple functions, or just persist the value of a variable across multiple function calls. To better explain what I mean:

Code: ags
function some_function() {
  int x; // when this function is called, a new variable named 'x' is created (local scope), value 0
  if (x == 0) x = 5; // value will always be 0 and changed to 5
  Display("x before: %d", x);
  x++; // value will always be changed to 6
  Display("x after: %d", x);
} // variable 'x' is destroyed

function on_key_press(eKeyCode keycode) {
  if (keycode == 'A') some_function();
}


This will display the values of 5 and then 6 every time you press the letter 'A' on the keyboard. The reasoning for this is since variable 'x' is being destroyed at the end of some_function every time it is called, the value of 'x' is not persisting. To make the value persist across the function calls, just move the definition outside of the function:

Code: ags
int x; // variable 'x' is only created once (global scope)

function some_function() {
  if (x == 0) x = 5; // value will only be 0 the first time this function is called
  Display("x before: %d", x);
  x++; // value increases
  Display("x after: %d", x);
} // variable 'x' persists its value due to global scope

function on_key_press(eKeyCode keycode) {
  if (keycode == 'A') some_function();
}


And hopefully that's enough rambling about scope to get the point across. ::)

SMF spam blocked by CleanTalk