Game.Economy.ResourceIterator
Assembly:
Assembly-CSharp
Namespace: Game.Economy
Type: struct
Base: System.ValueType
Summary:
A small iterator helper for stepping through Resource enum values that are represented as bit flags. It maintains the current resource value in the public field resource
, provides a static factory to obtain a fresh iterator starting before the first resource, and advances by left-shifting the underlying value. The iterator expects a sentinel enum value Resource.Last
to mark the end of iteration and Resource.NoResource
to represent the initial (zero) state. It uses ulong arithmetic to perform the shift and ensures the first yielded value is the lowest (1) bit.
Fields
public Resource resource
Holds the current resource enum value for the iterator. Starts asResource.NoResource
when obtained via GetIterator(), and is updated by calling Next(). The field is public and mutable, so callers can read the current resource after each Next() call.
Properties
- This type has no properties.
Constructors
public ResourceIterator()
Structs have a default parameterless constructor generated by the runtime. Typical usage is to obtain a properly initialized iterator via the static GetIterator() method rather than relying on the default constructor.
Methods
-
public static ResourceIterator GetIterator() : ResourceIterator
Returns a new ResourceIterator withresource
initialized toResource.NoResource
. Use this to start a fresh iteration. -
public bool Next() : System.Boolean
Advances the iterator to the next resource by left-shifting the current resource value. Implementation details: - If the current
resource
isResource.NoResource
(usually 0), the method will setresource
to the lowest resource bit (1). - Otherwise, it shifts the current value left by one:
(ulong)resource << 1
. Math.Max(1uL, ...)
is used to guarantee the first yielded value is 1.- The method returns true while the newly assigned
resource
is not equal toResource.Last
. Whenresource
becomesResource.Last
, Next() returns false (and leavesresource
set toResource.Last
), indicating the end of iteration. - Note: This iterator assumes the Resource enum uses bit-flag values and that
Resource.Last
is a sentinel value indicating termination. If Resource enum layout differs, iteration behavior may be incorrect.
Usage Example
// Obtain a fresh iterator
var it = ResourceIterator.GetIterator();
// Iterate through resources (stops before Resource.Last)
while (it.Next())
{
Resource current = it.resource;
// Handle current resource value...
// e.g. Debug.Log($"Handling resource: {current}");
}
Remarks:
- The iterator yields resource values corresponding to single-bit flags (1, 2, 4, ...) up to but not including Resource.Last
. If you need to include Resource.Last
in processing, check for it explicitly after the loop.
- Because resource
is a public field, callers can also set it directly, but doing so may break the intended iteration sequence. Use GetIterator() and Next() for standard usage.