Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: arj0n on Fri 19/03/2010 09:27:15

Title: [SOLVED] show random rooms in a loop with a certain time per room - slider
Post by: arj0n on Fri 19/03/2010 09:27:15
I would like to load a random room with a certain time to show this room and then switch to another random room and repeat this as in a loop. The rooms have no specific connection with each ther and there is no player character.
I'm sure there needs to be more code, cause the "certain time" is also missing...

player.ChangeRoom(Random(5) + 1); //Changes room to a random room between 1 and 6

Title: Re: show random rooms in a loop with a certain time per room
Post by: Khris on Fri 19/03/2010 10:03:29
Use on_event and a timer:

void on_event(EventType event, int data) {
  if (event == eEventEnterRoomBeforeFadein) {
    SetTimer(1, 400);  // ten seconds
  }
}

void repeatedly_execute() {
  if (IsTimerExpired(1)) {
    player.ChangeRoom(Random(5) + 1);
  }
}
Title: Re: show random rooms in a loop with a certain time per room
Post by: arj0n on Fri 19/03/2010 10:13:31
Thanx Khris, will test it when i'm back home again.  :)
Title: Re: show random rooms in a loop with a certain time per room
Post by: arj0n on Fri 19/03/2010 13:15:40
I'd like to use this code when the game starts, so I guess it should be in "function room_FirstLoad()"
Is that true and how do I change it to make it work?
Title: Re: show random rooms in a loop with a certain time per room
Post by: tzachs on Sat 20/03/2010 16:59:22
The code khris gave you will already work from the game start (since a new timer is started on each room load), with the exception of the first room which will not be randomized.

I don't think that there is a way to randomize the first room, but you can 'cheat' by adding a dummy room that the character will start in (you can make it the room of the logo & start new game button).
Add a boolean global variable, name it ChangingRoomsEnabled or something similar, when pressing on the new game button you can set the variable to true and change the character to a random room, by writing:

ChangingRoomsEnabled = true;
player.ChangeRoom(Random(5) + 1);


and modify Khris's code a bit to only start the timer if the global variable was enabled:

void on_event(EventType event, int data) {
 if (event == eEventEnterRoomBeforeFadein && ChangingRoomsEnabled) {
   SetTimer(1, 400);  // ten seconds
 }
}

Title: Re: show random rooms in a loop with a certain time per room
Post by: monkey0506 on Sat 20/03/2010 18:32:26
Well technically there's no need for a dummy room at all. Say in the editor your player is set to start in room 1. Then you could do this:

// room1.asc

function room_FirstLoad() {
 player.ChangeRoom(Random(5) + 1);
}

// GlobalScript.asc

function on_event(EventType event, int data) {
 if (event == eEventEnterRoomBeforeFadein) {
   if ((data >= 1) && (data <= 6)) { // only set the timer for the rooms that are going to use it
     SetTimer(1, GetGameSpeed() * 10); // 10 seconds the proper way :P
   }
   else SetTimer(1, 0); // disable the timer for other rooms
 }
}

function repeatedly_execute() {
 if (IsTimerExpired(1)) player.ChangeRoom(Random(5) + 1);
}


Edit: Reading the whole post helps too..I added Khris' global script code to my code.

Edit 2: Duh! The FirstLoad function will only be called on the first load so there's no need to even check the PreviousRoom property.

Edit 3: Added check to prevent the timer running in every room..this would have caused problems once you tried to go to other rooms outside the loop.
Title: Re: show random rooms in a loop with a certain time per room
Post by: tzachs on Sat 20/03/2010 20:35:45
One small fix, monkey...
Don't do it on the first_load, do it on room_Load.
The reason for this is that the name first_load is misleading, it actually happens after fade_in, so this will not look good..
Title: Re: show random rooms in a loop with a certain time per room
Post by: monkey0506 on Sat 20/03/2010 20:40:17
I forgot the order they got called in..so you got me there. :P

However, it should then be like this:

// room1.asc

function room_Load() {
  if (player.PreviousRoom == -1) { // first time player enters room 1 only
    player.ChangeRoom(Random(5) + 1); // randomize starting room
  }
}
Title: [Solved] show random rooms in a loop with a certain time per room
Post by: arj0n on Mon 22/03/2010 08:03:20
Khris, tzachs & monkey_05_06: thanx!
Works like a charm  ;)
Title: Re: show random rooms in a loop with a certain time per room
Post by: arj0n on Tue 23/03/2010 08:07:16
Quote from: monkey_05_06 on Sat 20/03/2010 18:32:26
Well technically there's no need for a dummy room at all. Say in the editor your player is set to start in room 1. Then you could do this:

// room1.asc

function room_FirstLoad() {
 player.ChangeRoom(Random(5) + 1);
}

// GlobalScript.asc

function on_event(EventType event, int data) {
 if (event == eEventEnterRoomBeforeFadein) {
   if ((data >= 1) && (data <= 6)) { // only set the timer for the rooms that are going to use it
     SetTimer(1, GetGameSpeed() * 10); // 10 seconds the proper way :P
   }
   else SetTimer(1, 0); // disable the timer for other rooms
 }
}

function repeatedly_execute() {
 if (IsTimerExpired(1)) player.ChangeRoom(Random(5) + 1);
}


Monkey [or someone else], is it posible to use a slider, like the music slider with the "SetMusicMasterVolume(sldMusic.Value); function",  to be able to manually change the seconds from a static "*10" into something between 1 and 15 for example?
Title: Re: [SOLVED] show random rooms in a loop with a certain time per room
Post by: monkey0506 on Tue 23/03/2010 12:00:21
Sure, you could set the slider (sldSpeed) to have a minimum value of 1 and maximum value of 15 and then do:

      SetTimer(1, GetGameSpeed() * sldSpeed.Value);

Of course if you want to actually modify the number of frames displayed per second (which could drastically alter any animations or timing of any on-screen effects) then you would want to use SetGameSpeed in conjunction with your slider..but it seems that this (the above) is probably better suited to what you were looking for.
Title: Re: [SOLVED] show random rooms in a loop with a certain time per room
Post by: arj0n on Tue 23/03/2010 13:16:40
Thanx again monkey, this does suit to what I was looking for  ;)


Title: Re: [SOLVED] show random rooms in a loop with a certain time per room
Post by: arj0n on Tue 23/03/2010 20:01:43
But, when changing the game speed this way by slider and resuming the game has as result that the randomizing-the-rooms doesn't work anymore...
Title: Re: [SOLVED] show random rooms in a loop with a certain time per room
Post by: monkey0506 on Wed 24/03/2010 00:33:37
You're saying that you're restoring a save game? Or you mean just turning off the GUI?
Title: Re: [SOLVED] show random rooms in a loop with a certain time per room
Post by: arj0n on Wed 24/03/2010 08:05:48
No, the only thing that should happen for now is showing random rooms immediately
from the start of "the game". This works fine until I go to the gPanel GUI and change
the game speed slider followed by clicking Resume, the random showing rooms stops...
Title: Re: [SOLVED] show random rooms in a loop with a certain time per room
Post by: Khris on Wed 24/03/2010 09:34:49
Try resetting the Timer in Resume_OnClick().
Title: Re: [SOLVED] show random rooms in a loop with a certain time per room
Post by: arj0n on Thu 25/03/2010 19:50:45
That works except that the first 2 lines + the "else" line are giving an error: "undifined symbol 'event'":
---
if (event == eEventEnterRoomBeforeFadein) {
    if ((data >= 1) && (data <= 6)) { // only set the timer for the rooms that are going to use it
      SetTimer(1, GetGameSpeed() * sldSpeed.Value);
    }
    else SetTimer(1, 0); // disable the timer for other rooms
}
---

So if I remove the room check lines, having this:
---
SetTimer(1, GetGameSpeed() * sldSpeed.Value);
    }
---
it does works until it reach a room that doesn't exist, because it just counts up until it reach such non-existing room....

*help needed again*  :(
Title: Re: [SOLVED] show random rooms in a loop with a certain time per room
Post by: Khris on Fri 26/03/2010 19:07:19
Inside Resume_OnClick() there's no local event variable.
So yes, no room check necessary.
Resetting the timer this way should work; if it tries to load up a non-existing room, the code after IsTimerExpired in the rep_ex is faulty. Could you post that?
Title: Re: [SOLVED] show random rooms in a loop with a certain time per room
Post by: arj0n on Sun 28/03/2010 10:23:34
Khris:

function repeatedly_execute() {
 
if (IsTimerExpired(1)) player.ChangeRoom(Random(10) + 1);
if (IsGamePaused() == 1) return;

}
Title: Re: [not SOLVED] show random rooms in a loop with a certain time per room - slider
Post by: Khris on Tue 30/03/2010 10:05:00
This will send the player to one of the rooms 1 to 11, is that what it's supposed to do?
Title: Re: [not SOLVED] show random rooms in a loop with a certain time per room - slider
Post by: arj0n on Tue 30/03/2010 10:23:46
It does send the player to one of those rooms but it doesn't randomly loop anymore.
It just shows about 3 rooms and then stops... which I do not understand.

When I don't change the game speed [and so the random speed] by using the slider, the randomising loops like it should...

Edit:
Or, when there are only 10 rooms and the player would be send to not existing room eleven, it might break the loop?
But then, I guess, AGS would give an error...
Anyway, I'll have to check that one...
Title: Re: [not SOLVED] show random rooms in a loop with a certain time per room - slider
Post by: Gilbert on Tue 30/03/2010 10:48:46
Maybe this is what breaks your game?

if (event == eEventEnterRoomBeforeFadein) {
    if ((data >= 1) && (data <= 6)) { // only set the timer for the rooms that are going to use it
      SetTimer(1, GetGameSpeed() * sldSpeed.Value);
    }
    else SetTimer(1, 0); // disable the timer for other rooms
}

Unless you have already changed it appropriately to match the possible range of rooms to teleport to.
Title: Re: [SOLVED] show random rooms in a loop with a certain time per room - slider
Post by: arj0n on Wed 31/03/2010 19:56:19
Gilbet: that was the problem, thanx!
And thanx guys for your help  ;)