Help using Global Variables

This forum is currently in read-only mode.
From the Asset Store
Globals 2.0
$3.99 USD
Globals 2.0 stores and group variables. You can also load and save data (variables) from/to JSON files.
  • I am currently trying to create an inventory system.

    I have an inventory image that is in a group that contains a 'on key press' option to pull up the image.

    I have a crown item, and when the player sprite collides with it, 1 is added to a global variable called 'Crown'.

    I am trying to make it so when you collide with the crown item and 1 is added to the Global Variable 'Crown', the crown will appear in the inventory image, I have been trying to make this work but I can't seem to make any 'if' statements that seem to work. Using the 'Compare' values doesn't seem to work for me either. If anyone can point me to a tutorial or page that can explain this sort of problem I would appreciate it. I have never really worked with code before, so I am unsure on some processes.

    Sorry for the strange layout of the message, I am tired!

    Peace!

    <img src="http://ofhell.com/code1.png">

    <img src="http://ofhell.com/code2.png">

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • showus the code

  • I'm not sure what kind of inventory you are making or how you want it to work. Code would help. Heck, you could even make a picture of how you want it to look to help too.

  • Code added, sorry about that.

    What the inventory looks like doesn't matter to me right now. It's just a gradient image that comes up which will have images of items appear on it as you receive them. For example:

    <img src="http://ofhell.com/example1.png">

  • When you pick up an item is it always going to be in the same spot in the inventory? Like, if you pick up a crown later in the game, will it put it in that same spot or can that spot be used by something else? I'm trying to determine how dynamic you need your inventory to be.

  • When you pick up an item is it always going to be in the same spot in the inventory? Like, if you pick up a crown later in the game, will it put it in that same spot or can that spot be used by something else? I'm trying to determine how dynamic you need your inventory to be.

    Not very dynamic, it will appear in the same spot all the time. I will probably have a silhouette of the item in the inventory and just fill it in with the item when you get it.

  • > When you pick up an item is it always going to be in the same spot in the inventory? Like, if you pick up a crown later in the game, will it put it in that same spot or can that spot be used by something else? I'm trying to determine how dynamic you need your inventory to be.

    >

    Not very dynamic, it will appear in the same spot all the time. I will probably have a silhouette of the item in the inventory and just fill it in with the item when you get it.

    GREAT! That is MUCH easier.

    As for your code, it looks like it would set the crown global variable to 1, but then the global variable would only tell it to make your INVENTORY visible. The "Crown Equal to 1" event is using your Inventory Gradient.

    I'm not exactly sure how you want your Inventory to open, but here's something:

    Make a new layer and name it "Inventory." Create a new Global Value named "showInventory" or whatever will help your remember what it does. You should create an event saying that if "showInventory" = 1, set Layer "Inventory" Visible. Then use Else or If "showInventory" = 0 to have it set Layer "Inventory" to Invisible. That layer should hold your gradient background for your inventory menu and whatever items go in your inventory.

    Next, change your "Crown = 1" event to use the actual Crown Sprite instead of the Inventory. If you want your inventory to open when you pause the game, you can use Time Scale and stuff along with Enter Key is pressed (Mouse/Keyboard Object).

    --

    That isn't a complete tutorial or anything, but you can search around the forum more for the different parts of that (pausing, etc). I'm sure someone else will come along and help too, lol. I'm too tired.

  • >

    > > When you pick up an item is it always going to be in the same spot in the inventory? Like, if you pick up a crown later in the game, will it put it in that same spot or can that spot be used by something else? I'm trying to determine how dynamic you need your inventory to be.

    > >

    >

    > Not very dynamic, it will appear in the same spot all the time. I will probably have a silhouette of the item in the inventory and just fill it in with the item when you get it.

    >

    GREAT! That is MUCH easier.

    As for your code, it looks like it would set the crown global variable to 1, but then the global variable would only tell it to make your INVENTORY visible. The "Crown Equal to 1" event is using your Inventory Gradient.

    I'm not exactly sure how you want your Inventory to open, but here's something:

    Make a new layer and name it "Inventory." Create a new Global Value named "showInventory" or whatever will help your remember what it does. You should create an event saying that if "showInventory" = 1, set Layer "Inventory" Visible. Then use Else or If "showInventory" = 0 to have it set Layer "Inventory" to Invisible. That layer should hold your gradient background for your inventory menu and whatever items go in your inventory.

    Next, change your "Crown = 1" event to use the actual Crown Sprite instead of the Inventory. If you want your inventory to open when you pause the game, you can use Time Scale and stuff along with Enter Key is pressed (Mouse/Keyboard Object).

    --

    That isn't a complete tutorial or anything, but you can search around the forum more for the different parts of that (pausing, etc). I'm sure someone else will come along and help too, lol. I'm too tired.

    Awesome! Thanks a bunch. I've gotten the system partially working and tomorrow I will elaborate on it and post in this thread if any more problems arise. Thanks again!

  • One more problem I encountered.

    If the inventory screen is visible and you pick up the crown, it will not show until you close and open the inventory again. I am still figuring out how to fix this but any fresh eyes will help! Thanks again.

    <img src="http://ofhell.com/help.png">

  • Oye again, just another quick problem I am having a hard time resolving:

    I need to have the controls set up so when you press '1' it disables 'Ghostcrown' and sets everything it disabled back to enabled as well as enabling 'Ghostcrown' at the end so you can repeat. The problem I come to is that when '1' is pressed, it both activates and de-activates everything at once.

    <img src="http://ofhell.com/help4.png">

    <img src="http://ofhell.com/help2.png">

    <img src="http://ofhell.com/help3.png">

  • [quote:1eigazkr]The problem I come to is that when '1' is pressed, it both activates and de-activates everything at once.

    Both of your "On key 1 pressed" triggers are triggering in the same tick.

    Example:

    + On key X pressed
    + "Foo" is equal to 1 
        -> Set "Foo" to 2
    
    + On key X pressed
    + "Foo" is equal to 2
        -> Set "Foo" to 1
    [/code:1eigazkr]
    
    You might think this would toggle the value back and forth between 1 and 2 with each keypress.  But no.  Practically speaking, Foo will never equal 2.  If you press X then both of the "X pressed" triggers will run.  In the first, you set Foo to 2.  Since it's 2 now, then when you reach the second trigger Construct says "Oh, the X key has been pressed... and Foo is equal to 2 so let's run the event.  And it sets it right back to 1.
    
    Looking at the post before that one it appears that you have just made a simple logic error and if you look at it again I'm sure you'll see it pretty easily
  • Thank you, I have fixed the issue regarding the crown showing up while the inventory is visible.

    I am still unsure as to how to fix the issue of pressing the same key to activate the ghostcrown and take it off.

  • I had something like that happen before. I'd use:

    + On key X pressed
    ++ "Foo" is equal to 1 
        -> Set "Foo" to 2
    
    ++Else
        -> Set "Foo" to 1
    [/code:23wuk6gp]
    (++ means subevent.)
    Here's how it works: When X is pressed, it checks the first subevent. If the first subevet it true, it will trigger the actions in the subevent. It will see the "Else" in the next event and will not even do anything else with that subevent. If the first subevent is NOT true, it will see the "Else" and will check if the second subevent is true. The second subevent needs no other conditions because it has two IMPLIED conditions: "On X key is pressed" and "'Foo' is NOT 1" ("Foo" can only equal 1 or 2 so this is the same as "'Foo' is 2"). This will work together as a toggle between the two values of "Foo" when X is pressed.
  • I had something like that happen before. I'd use:

    > + On key X pressed
    ++ "Foo" is equal to 1 
        -> Set "Foo" to 2
    
    ++Else
        -> Set "Foo" to 1
    [/code:14j1r9b7]
    (++ means subevent.)
    Here's how it works: When X is pressed, it checks the first subevent. If the first subevet it true, it will trigger the actions in the subevent. It will see the "Else" in the next event and will not even do anything else with that subevent. If the first subevent is NOT true, it will see the "Else" and will check if the second subevent is true. The second subevent needs no other conditions because it has two IMPLIED conditions: "On X key is pressed" and "'Foo' is NOT 1" ("Foo" can only equal 1 or 2 so this is the same as "'Foo' is 2"). This will work together as a toggle between the two values of "Foo" when X is pressed.
    

    Thank you very much! Everyone's been helpful here.

  • Alternatively:

    + Key X is pressed

    Foo = 1 - Foo

    A simple trick - if Foo is 0, you get 1, if Foo is 1, you get 0. It switches back and forth.

    Then you can make your inventory event trigger once:

    + Foo = 1

    + Trigger Once While True

    Stuff

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)