Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Jojowl80 on Sun 05/07/2020 17:35:54

Title: use item to make object visible on different screen
Post by: Jojowl80 on Sun 05/07/2020 17:35:54
I was wondering if it was possible If I were to use an item in any room in my game, to make objects appear on my map screen. essentially I want locations to not appear on the map until the condition is met.
Title: Re: use item to make object visible on different screen
Post by: Khris on Sun 05/07/2020 18:01:14
-Set global bool variable when item is used
-in map room's before fadein event, check variable and show object
Title: Re: use item to make object visible on different screen
Post by: Jojowl80 on Sun 05/07/2020 19:04:12
okay not sure if I am on the right track or not
I set the Bool "Mrow" in the global Variables to false.

I have this on my Map

Code (ags) Select

function room_Load()
{
if (Mrow = true);
object[0].Visible = true;
}


I'm not sure where to put the function to use the item, I imagine it would have to go in Global.Asc? but how would I call it there?
Title: Re: use item to make object visible on different screen
Post by: Matti on Sun 05/07/2020 19:17:15
You create these functions by going to the properties-panel of an item, object, hotspot, character.. below the project tree. There you click the thunderbolt-icon and then create functions by clicking the [...]-button. Those functions will then appear in the global script or the room script.
Title: Re: use item to make object visible on different screen
Post by: Khris on Sun 05/07/2020 20:39:12
Code (ags) Select
function room_Load()
{
  if (Mrow == true) object[0].Visible = true; // two equal signs to compare, no semi-colon after condition
}
Title: Re: use item to make object visible on different screen
Post by: Jojowl80 on Sun 05/07/2020 20:48:20
so heres what I have in the global script

Code (ags) Select


function Arcus_UseInv()
{
  if (player.ActiveInventory == iCcupfull) {
    if (player.HasInventory(iCcupfull)) {
      player.Say("I thought you'd never ask.");
       player.LoseInventory(iCcupfull);
      player.AddInventory(iCcup);
   
   
    if(player.ActiveInventory == iMrow) {
     if (player.HasInventory(iMrow)) {
      player.Say("The Mrownomitor is picking up some locations on the map!");
      Mrow = true;
   
  }
}}}}



Not sure how to set up use of different items. can only ever seem to get one working at a time.
Title: Re: use item to make object visible on different screen
Post by: Snarky on Sun 05/07/2020 21:23:25
You gotta close the first if-block (with an end-curly-bracket, '}' )  before the second if-block. Otherwise it only happens if player.ActiveInventory == iCcupfull AND player.ActiveInventory == iMrow, which is impossible. Proper indentation is very useful here to make the logical structure of the program visibleâ€"you seem to be trying, but getting it wrong. (Basically, when you indent something, it's to show that it's "inside" the line before the indentation.)
Title: Re: use item to make object visible on different screen
Post by: Jojowl80 on Sun 05/07/2020 21:51:08
hmmm. I closed the first if block off, but it didn't seem to change anything. he still doesn't use the item.

Code (ags) Select


function Arcus_UseInv()
{
  if (player.ActiveInventory == iCcupfull) {
    if (player.HasInventory(iCcupfull)) {
      player.Say("I thought you'd never ask.");
     player.LoseInventory(iCcupfull);
    player.AddInventory(iCcup);
    }
  if(player.ActiveInventory == iMrow) {
     if (player.HasInventory(iMrow)) {
      player.Say("The Mrownomitor is picking up some locations on the map!");
      Mrow = true;
     }
}}}

Title: Re: use item to make object visible on different screen
Post by: Khris on Sun 05/07/2020 21:55:49
You need to fix your indentation, and you will see what the issue is.

This is the (broken) code you have, indented properly:
function Arcus_UseInv() {
  if (player.ActiveInventory == iCcupfull) {
    if (player.HasInventory(iCcupfull)) {
      player.Say("I thought you'd never ask.");
      player.LoseInventory(iCcupfull);
      player.AddInventory(iCcup);
    }
    if (player.ActiveInventory == iMrow) {
      if (player.HasInventory(iMrow)) {
        player.Say("The Mrownomitor is picking up some locations on the map!");
        Mrow = true;
      }
    }
  }
}

The problem is now clearly visible: the iMrow check is inside the iCcupfull check.

Here's the fixed code:
Code (ags) Select
function Arcus_UseInv() {
  if (player.ActiveInventory == iCcupfull) {
    player.Say("I thought you'd never ask.");
    player.LoseInventory(iCcupfull);
    player.AddInventory(iCcup);
  }
 
  if (player.ActiveInventory == iMrow) {
    player.Say("The Mrownomitor is picking up some locations on the map!");
    Mrow = true;
  }
}
Title: Re: use item to make object visible on different screen
Post by: Jojowl80 on Sun 05/07/2020 22:18:58
thanks everything is working. I still do not fully understand indenting tho. Is there a good article to read about it?
Title: Re: use item to make object visible on different screen
Post by: Crimson Wizard on Sun 05/07/2020 22:26:34
Quote from: Jojowl80 on Sun 05/07/2020 22:18:58
thanks everything is working. I still do not fully understand indenting tho. Is there a good article to read about it?

Idea is simply to add few extra spaces or tabs for each internal "block" of statements.

Code (ags) Select

function {
    if (something) {
        actions
        if (something else) {
            more actions
        }
    }
}


This way you can clearly see 1) which code is executed under condition and which is not;  2) which ifs have extra/missing brackets.


EDIT: Also, I think I mentioned this before, but just in case, if you test for ActiveInventory, there's no need to test HasInventory with same item, because ActiveInventory is always something that player has.
Title: Re: use item to make object visible on different screen
Post by: Khris on Sun 05/07/2020 22:28:15
When you open a block using a {, you indent its contents by 2 additional spaces. When you end the block, you indent the closing } by two spaces less.
AGS even does this for your automatically whenever you press enter after a { or }.
Title: Re: use item to make object visible on different screen
Post by: Cassiebsg on Sun 05/07/2020 22:38:04
I like to align my brackets { } as it allows me to see the start and end more clearly.

So using CWs code, mine would look like this:

Code (ags) Select

function
{
    if (something)
    {
        actions
        if (something else)
        {
            more actions
        }
    }
}


This allows to spot the blocks and let me visually see that all brackets are in the right place. If I miss one, then it's easy to spot the error.  ;) AGS also adds a vertical dotted line | under each bracket, so you can easily follow it.
Hope it helps
Title: Re: use item to make object visible on different screen
Post by: Jojowl80 on Sun 05/07/2020 23:12:25
Ohhhh I see it now! don't know why that was so hard to understand, thanks for all the help everyone!