- Home /
Trying to figure out the best way to have a series of ints that are dynamically numbered in sequence
I want to have a set of named ints (not a list, because adding ints to a list and then changing the value of the item in the list doesn't change the original) that, when the appropriate function is triggered, will be given a sequential numbering (0, 1, 2, 3, etc). Of course, that's easy enough to just do by hand, but what I want is to be able to add new variables or remove existing ones, and still have them be sequentially numbered. So like if the set is "ExampleIntBlue, ExampleIntRed, ExampleIntYellow;" then ExampleIntBlue would be 0, ExampleIntRed would be 1, and ExampleIntYellow would be 2. Then if I added ExampleIntGreen in between red and yellow (and then triggered the numbering function again), I would want ExampleIntGreen to be 2 and ExampleIntYellow to be 3. If I then removed ExampleIntRed and ran the function again, green would be 1 and yellow would be 2.
Is there any way to do that, that would be easier than just renumbering them manually every time? (The list won't be super-long, but it might be like 20 or 30 items.) The only thing I can think of is doing them as GameObjects with a string to define the name of the resulting int, and then having the parent of those objects run through the loop of all the children (since doing that goes in the order they're listed in the hierarchy). But I figure there's gotta be an easier way to do it than that.
Answer by highpockets · May 25, 2019 at 01:46 PM
I think this would be a good place to use the insert method of List. List.Insert(index, item); But if Count == Capacity I believe the item gets added to the end of the list. If the list always stays the same size, just remove the item that needs the index change and then insert it to the desired index.
List.Remove(“green”);
List.Insert(3, “green”);
I don't think I can use a List at all, though, because I want to change the original variables. I want other scripts to be able to refer back to ExampleIntGreen (based on the value that green dynamically has at any given moment through this method), not ExampleList[3]. If you add ExampleIntGreen to a list, it just copies the current value of that int and doesn't make any further changes to the original.
Answer by troien · May 25, 2019 at 03:54 PM
This part of your question
(not a list, because adding ints to a list and then changing the value of the item in the list doesn't change the original)
Although this is true for an integer, that has nothing to do with the list. If you place a 'reference' type item in the list (a.k.a. a class) then modifying it will change the original. If you place a 'value' type in the list (a.k.a. struct) (int, bool, float, etc. are all value types) then modifying it will only modify the value in said list. See also this answer on stackoverflow
It doesn't matter where you store the value or reference (variable, Array, List, LinkedList, etc.) the above statement holds. Meaning the 'list' has nothing to do with the fact whether it updates the original or not, the original type decides that.
Now i'm not sure what your intent is here with these integers. And why you put then in a list in the first place, and then still want to acces them outside your list. So i'll just write a very general 'wrap it in a class' solution here so you can change the value in a list while at the same time changing the original...
public class Example
{
public int Value { get; set; }
public Example(int value)
{
Value = value;
}
}
Usage of this class would be like this:
List<Example> test = new List<Example>();
Example test1 = new Example(0);
Example test2 = new Example(1);
test.Add(test1);
test.Add(test2);
test[0].Value = 10;
Debug.Log(test1.Value); // Will return 10 instead of 0
Now i'm not sure what your intent is here with these integers. And why you put then in a list in the first place, and then still want to acces them outside your list.
$$anonymous$$y intent is to use this in a turn-based strategy game, where the ints represent different individual phases of a turn. It's not even really about changing the values of the ints during the game, it's really just a matter of having them be fluid during the development and prototyping stage. When I'm scripting, I have different actions and functions restricted to their specific phase, and I have a central int keeping track of what phase it currently is.
So like, if I script the function for a move action, I only want that to trigger during the movement phase. That's easy enough, I just wrap it inside a "if (CurrentPhaseInt == $$anonymous$$ovementPhaseInt)" statement. The problem is, during the prototyping stage, I don't really know for sure how many phases there will be, or what order they'll be in. Let's just toss out a hypothetical turn sequence for illustrative purposes...
ActivationPhaseInt = 1;
$$anonymous$$ovementPhaseInt = 2;
SelectCombatPhaseInt = 3;
ResolveCombatPhaseInt = 4;
CombatAdvancePhaseInt = 5;
ReinforcementPhaseInt = 6;
ResupplyPhaseInt = 7;
RegroupPhaseInt = 8;
...Those aren't really the phases in my game, but you get the idea. Now, let's say that I decide to move the Reinforcement Phase to right after the Activation Phase. I have to renumber all the ints from 2 to 8. If I then take out the Activation Phase completely, I have to renumber all the values that are left. Not difficult, by any means; just tedious, and I'm hoping there's a better way. It might not seem like much, but the longer the list gets, the more annoying it is to ever move anything around, and that puts a damper on creativity.
I think part of my problem is that before I learned to code, I got accustomed to using Excel to do a lot of things that I might have done with program$$anonymous$$g if I knew how (like generating random numbers). But Excel is also just really good at storing data in an orderly fashion, so I think there are a lot of situations where I think, "this is really easy to do in Excel, so it should be easy to do in code too." In Excel, if you Cut a cell in Row 10, then Paste it between the cells in Row 5 and 6, then it just moves the other cells down and takes up a position in Row 6. That's essentially the idea of what I'm trying to do here, be able to rearrange elements and have them renumbered dynamically.
It sounds like you are describing an enum actually :p So i think that is what you are looking for... (This site explains them pretty well I think) So for you example, you'd create a new script, call it Phase, delete content and replace it with this:
public enum Phase
{
ActivationPhase,
$$anonymous$$ovementPhase,
SelectCombatPhase,
ResolveCombatPhase,
CombatAdvancePhase,
ReinforcementPhase,
ResupplyPhase,
RegroupPhase
}
Now in the rest of your code, you can use it like this:
Phase currentPhase = Phase.ActivationPhase;
if (currentPhase == Phase.RegroupPhase)
{
// Do something
}
By default, the first one gets index 0, second one gets index 1, etc. So reordering them automatically changes index, but you can number them yourself if you want (just set = 5 behind a name and before the comma for example). You can also cast them to int to use the greater/lower then operators etc.
That sounds like it might be exactly what I'm looking for. I'll definitely give that a try, though it will take some effort to reformat the existing script from the int stuff it already has.
Do the enum values work just like ints in every other respect? Like, if I did "Phase currentPhase = 0" then did "currentPhase++" would that turn currentPhase to 1 (without adjusting the value of any other items in the set)?
Your answer
Follow this Question
Related Questions
How can I store HUGE NUMBERS on unity? (256bit integer) 0 Answers
Big Numbers - How to convert (k, M, a ) 4 Answers
Sequential GameObject Activation 2 Answers
Periodic fraction gets converted to int 1 Answer
help in unity! please enter! 0 Answers