Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: RadarG on Mon 07/12/2015 10:11:32

Title: Changing the background
Post by: RadarG on Mon 07/12/2015 10:11:32
Hi, everyone.

I'm having problems trying to change the backgroung. I'll explain this.

My game has two playable characters and I want to change the background when I change the characer. The change of the characters is made with GUI so when I click in that GUI the character changes and I want the background to change too.

This is the code I've got

Code (ags) Select


function gGui1_OnClick(GUI *theGui, MouseButton button)
{
cRubber.SetAsPlayer();
cMaggie.StopMoving();
SetBackgroundFrame(0);
}

function gGui2_OnClick(GUI *theGui, MouseButton button)
{
cMaggie.SetAsPlayer();
cRubber.StopMoving();
SetBackgroundFrame(Background 1);
}


But I don't know which is the frame of the second background. When I import the background in the room it appears as "Background 1" and the previous backroung is "Main Background" So which is the frame?

Thank you.
Title: Re: Changing the background
Post by: Crimson Wizard on Mon 07/12/2015 10:38:01
E: Ok, since you added script example,

Code (ags) Select

SetBackgroundFrame(1);


0 - it is main background
1 - background 1,
etc.
Title: Re: Changing the background
Post by: RadarG on Mon 07/12/2015 10:39:38
Ye sorry, the background I want to change is the room's background
Title: Re: Changing the background
Post by: RadarG on Mon 07/12/2015 10:41:21
I put the 0 for main and 1 for second background and it gives me this error

SetBackgroundFrame: Invalid frame number specified


(http://puu.sh/lMxm8/fe14e45529.png)

I have 2 backgrounds that's an image of the room
Title: Re: Changing the background
Post by: RadarG on Mon 07/12/2015 10:51:23
So that worked, but just in the room that has 2 backgrounds which is logic but when I click in that GUI in another room the error appears. I have assume that is becoause the background changing is set in the GUI script. So is there another way to do it?

Also is there any way that the backgrounds dont change continuosly, I've set the delay at a very high number but doesn't seem to work.
Title: Re: Changing the background
Post by: Crimson Wizard on Mon 07/12/2015 10:53:24
Could you elaborate on what are you trying to achieve with this? Perhaps there is an easier way to do what you wanted, without adding multiple backgrounds to every room.
Title: Re: Changing the background
Post by: RadarG on Mon 07/12/2015 10:59:22
I dont understand that, what do you mean?

My objective is to change the background when I change the character because the idea of the game is that the characters have diferent points of view.
Like one is older than the other and has a mature view of environment while the other has more childish view, the backgraound should have the possibily to be changed in all the rooms because the player can change in all the rooms the character.
Title: Re: Changing the background
Post by: Crimson Wizard on Mon 07/12/2015 11:07:35
I do not remember if that's possible to check if room has enough backgrounds... :-/
Perhaps you will just have to import enough backgrounds everywhere.
Or explicitly specify rooms where you do not want to change backgrounds.

The latter is possible to do with the use of Custom Properties. If you create a custom property for the room called, for example, "MustSwitchBackgrounds", then you can do:
Code (ags) Select

if (GetRoomProperty("MustSwitchBackgrounds") == 1)
    SetBackgroundFrame(1);



To disable animation you need to call SetBackgroundFrame early when the room is loaded, for example in "before fade-in event". Since you have to do this for every room, I suggest writing an "on_event" function in Global Script:

Like:
Code (ags) Select

function on_event (EventType event, int data)
{
    if (event == eEventEnterRoomBeforeFadein)
    {
        if (player == cMaggie)
            SetBackgroundFrame(1);
    }
}

Title: Re: Changing the background
Post by: RadarG on Mon 07/12/2015 11:27:42
I've tried both codes, to be honest I didn't understood very well the first one and the second one works but only when I'm playing with cMaggie when I change to play with the other character (cRubber) the backgrounds start to change again, so I tried to stop that with:
Code (ags) Select

function on_event (EventType event, int data)
{
    if (event == eEventEnterRoomBeforeFadein)
    {
        if (player == cMaggie)
            SetBackgroundFrame(0);
        else
            SetBackgroundFrame(1);
    }
}



but didn't worked. Also when in the Function on_event appears (EvenType event , int data) here data is the room number? and what is the event?
thank you so much
Title: Re: Changing the background
Post by: Monsieur OUXX on Mon 07/12/2015 13:17:42
Hello RadarG,

It didn't work with the other character because, indeed, CW's code snippet only deals with cMaggie (it might be on purpose because CW probably takes for granted that the background is reset to 0 every time the player re-enters the room). Therefore, your updated version of his script should work.

HOEWEVER...

I think it doesn't work because it's conflicting with CW's first piece of script (the one with the room property). CW specifically mentionned that you should use that first script only if some rooms don't have two backgrounds. You've been trying to be too smart for your own good.

- If all rooms are meant to have two backgrounds, then remove that piece of script that mentions the room property and I bet you that it will work (provided  you don't click on your gui in an unfinished room that still has only one background, or it will crash, as you can expect).

- If not all rooms are meant to have two backgrounds, then simply mix the first and second piece of script, to make it all work smoothly together:
Code (ags) Select

function on_event (EventType event, int data)
{
    if (event == eEventEnterRoomBeforeFadein)
    {
        if (GetRoomProperty("MustSwitchBackgrounds") == 1) //hey we're entering a room that has more than one background!
        {
            if (player == cMaggie)
                SetBackgroundFrame(0);
            else
                SetBackgroundFrame(1);
         }
         else //we're entering a room that only has one background
         {
             SetBackgroundFrame(0); //set it to zero. Probably unnecessary, but... just to be sure.
          }
    }
}



==========

About on_event: the meaning of "data" really depends on the actual event that triggered that function. All the meanings of "data" are explained in AGS' manual, in the page called "Predefined global script functions" (I found it by typing "on_event" in the search field) :

Quote
eEventEnterRoomBeforeFadein
      called just before room Player Enters Room event is run.
      DATA = new room number


By the way, CrimsonWizard, in my AGS 3.4, eEventEnterRoomBeforeFadein is missing in the "Built-in enumerated types" article. It should be added.
Title: Re: Changing the background
Post by: Khris on Mon 07/12/2015 13:24:40
Let's look at it like that:
is the final game going to have only rooms with (at least) two backgrounds? If so, simply add a second, placeholder background to each new room so you don't get crashes while testing the game.
Title: Re: Changing the background
Post by: Monsieur OUXX on Mon 07/12/2015 13:34:03
Quote from: Monsieur OUXX on Mon 07/12/2015 13:17:42
HOEWEVER...
Genuine typo. I'm leaving it here because it's just too funny
Title: Re: Changing the background
Post by: RadarG on Mon 07/12/2015 14:39:00
Monsieur OUXX I tried your code but it gives me an error in the GetRoomProperty it says this:

GetProperty: no such property found in schema.
Title: Re: Changing the background
Post by: Snarky on Mon 07/12/2015 15:34:21
Notice what CW told you earlier

Quote from: Crimson Wizard on Mon 07/12/2015 11:07:35
If you create a custom property for the room called, for example, "MustSwitchBackgrounds", then you can do:
Code (ags) Select

if (GetRoomProperty("MustSwitchBackgrounds") == 1)

You have to create that custom property first. Look up Custom Properties in the manual.
Title: Re: Changing the background
Post by: Monsieur OUXX on Mon 07/12/2015 16:05:51
Quote from: Snarky on Mon 07/12/2015 15:34:21
You have to create that custom property first. Look up Custom Properties in the manual.

RadarG so that means that you had not been trying this piece of code before? So I'm lost : are all rooms meant to have exactly two backgrounds, or not? I have a feeling that you read only half the posts ;)
Title: Re: Changing the background
Post by: RadarG on Tue 08/12/2015 16:47:46
yes, all the rooms have 2 backgrounds, 1 background for 1 of the characters point of view and the other background for the other character.

Title: Re: Changing the background
Post by: Monsieur OUXX on Wed 09/12/2015 14:42:48
In that case, as said previously, you don't need the room property.

Only this :
Code (ags) Select

function on_event (EventType event, int data)
{
    if (event == eEventEnterRoomBeforeFadein)
    {
        if (player == cMaggie)
            SetBackgroundFrame(0);
        else
            SetBackgroundFrame(1);
    }
}