Need some help with enums

B

bleeze

Guest
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.
 
H

Homunculus

Guest
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?
 
B

bleeze

Guest
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?
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?
 

samspade

Member
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?
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.
 
B

bleeze

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

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
 
Top