[SOLVED] show random rooms in a loop with a certain time per room - slider

Started by arj0n, Fri 19/03/2010 09:27:15

Previous topic - Next topic

arj0n

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


Khris

Use on_event and a timer:

Code: ags
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);
  }
}

arj0n

Thanx Khris, will test it when i'm back home again.  :)

arj0n

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?

tzachs

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:
Code: ags

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:
Code: ags

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


monkey0506

#5
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:

Code: ags
// 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.

tzachs

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..

monkey0506

I forgot the order they got called in..so you got me there. :P

However, it should then be like this:

Code: ags
// room1.asc

function room_Load() {
  if (player.PreviousRoom == -1) { // first time player enters room 1 only
    player.ChangeRoom(Random(5) + 1); // randomize starting room
  }
}

arj0n

Khris, tzachs & monkey_05_06: thanx!
Works like a charm  ;)

arj0n

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:

Code: ags
// 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?

monkey0506

Sure, you could set the slider (sldSpeed) to have a minimum value of 1 and maximum value of 15 and then do:

Code: ags
      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.

arj0n

Thanx again monkey, this does suit to what I was looking for  ;)



arj0n

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...

monkey0506

You're saying that you're restoring a save game? Or you mean just turning off the GUI?

arj0n

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...

Khris


arj0n

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*  :(

Khris

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?

arj0n

Khris:

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

}

Khris

This will send the player to one of the rooms 1 to 11, is that what it's supposed to do?

SMF spam blocked by CleanTalk