1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Need some help with enums

Discussion in 'Programming' started by bleeze, Sep 21, 2019.

  1. bleeze

    bleeze Member

    Joined:
    Sep 12, 2019
    Posts:
    8
    So i've made a 2d array with enum variables for my items in my game. One of the enums is for amount, which tracks how many you have of each item. The problem is when i remove an item, with the simple code:

    itemdef [ itemtype, itemprop.amount ] - = 1;

    It jumps straigth to -1, even if you have several of an item. But get this, if i keep removing items it works properly, going from -1 to -2 and so on. I think when i call it the first time, it goes from x amount to it's original amount which is 0. Have i misunderstood the use of enums? Is there anyway to do this with enums, or do I need to change it to something else? Any advice is appreciated.
     
  2. Homunculus

    Homunculus Member

    Joined:
    Jun 20, 2016
    Posts:
    856
    It doesn’t look like a problem with enums honestly, try to output the value of the array before you decrease it. Is it possible you are resetting it to 0 somewhere else in your code?
     
    ParodyKnaveBob likes this.
  3. bleeze

    bleeze Member

    Joined:
    Sep 12, 2019
    Posts:
    8
    Yes i tried and figured out the problem, My variables reset when i move to next room, so now i have another question instead lol. How do i carry my enums to next room?
     
  4. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,230
    enums are constants and never change and never can be changed. Do you mean the variable at that location in the 2D array? Also variables are never 'reset' by going to another room they either continue (because they are global or part of a persistent object) or are removed from the game.

    So it depends on where that array is. If it is in a non persistent object and not global it is being deleted. If it can still be referenced then it means you are recreating an instance of the object each room and so it is a new instance of the object and its values are whatever they are initialized to. If is a persistent object or global variable then the values would not be reset unless you had code that reset them (such as in a room start event).

    My guess is that it is the first problem. That this array is in some non-persistent object that you create at the start of the room (or certain rooms). If that is the case, then there are a couple solutions. My preferred solution would be to separate out the values which need to be persistent (e.g. things like player health, inventories, etc.) into persistent objects. But we probably need to know more about the specifics of your project in order to answer.
     
    ParodyKnaveBob likes this.
  5. bleeze

    bleeze Member

    Joined:
    Sep 12, 2019
    Posts:
    8

    Sorry for my sloppy explanation, I'll properly explain better in the future. But i found what the problem was, It was that the script where i defined the my array, was running from a create event, which was called in every room, when the object was created. So now the script runs from the Game Start event, which happens only once, so now it works as intended! thanks for the help
     
    ParodyKnaveBob likes this.

Share This Page