use item to make object visible on different screen

Started by Jojowl80, Sun 05/07/2020 17:35:54

Previous topic - Next topic

Jojowl80

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.

Khris

-Set global bool variable when item is used
-in map room's before fadein event, check variable and show object

Jojowl80

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

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?

Matti

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.

Khris

Code: ags
function room_Load()
{
  if (Mrow == true) object[0].Visible = true; // two equal signs to compare, no semi-colon after condition
}

Jojowl80

so heres what I have in the global script

Code: ags


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.

Snarky

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

Jojowl80

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


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;
     } 
}}}


Khris

You need to fix your indentation, and you will see what the issue is.

This is the (broken) code you have, indented properly:
Code: ags
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
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;
  }
}

Jojowl80

thanks everything is working. I still do not fully understand indenting tho. Is there a good article to read about it?

Crimson Wizard

#10
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

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.

Khris

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

Cassiebsg

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

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
There are those who believe that life here began out there...

Jojowl80

Ohhhh I see it now! don't know why that was so hard to understand, thanks for all the help everyone!

SMF spam blocked by CleanTalk