slay-the-spire-map-in-unity: Shuffling Extension (I know, not your code) returning Index out of range.

In MapPlayerTracker I have Set up my nodes like this:

        {
            // we have access to blueprint name here as well
            Debug.Log("Entering node: " + mapNode.Node.blueprintName + " of type: " + mapNode.Node.nodeType);
            // load appropriate scene with context based on nodeType:
            // or show appropriate GUI over the map: 
            // if you choose to show GUI in some of these cases, do not forget to set "Locked" in MapPlayerTracker back to false
            switch (mapNode.Node.nodeType)
            {
            case NodeType.CommonEnemy:
	            // Load the scene or enable the UI game object for CommonEnemy
	            if (!string.IsNullOrEmpty(mapNode.Blueprint.sceneName))
		            SceneManager.LoadScene(mapNode.Blueprint.sceneName);
	            else if (mapNode.Blueprint.uiObject != null)
		            mapNode.Blueprint.uiObject.SetActive(true);
	            break;
            case NodeType.MinorEnemy:
	            // Load the scene or enable the UI game object for MinorEnemy
	            if (!string.IsNullOrEmpty(mapNode.Blueprint.sceneName))
		            SceneManager.LoadScene(mapNode.Blueprint.sceneName);
	            else if (mapNode.Blueprint.uiObject != null)
		            mapNode.Blueprint.uiObject.SetActive(true);
                    break;

MapGenerator:

public static class MapGenerator
    {
        private static MapConfig config;

        private static readonly List<NodeType> RandomNodes = new List<NodeType>
        {	
	        NodeType.CommonEnemy,
	        NodeType.MinorEnemy,
	        NodeType.EliteEnemy,
	        NodeType.RestSite,
	        NodeType.Treasure,
	        NodeType.Store,
	        NodeType.Mystery,
        };

NodeBlueprint:

namespace Map
{
    public enum NodeType
    {
	    CommonEnemy,
	    MinorEnemy,
        EliteEnemy,
        RestSite,
        Treasure,
        Store,
        Boss,
        Mystery
    }
}

For some reason, I continue to receive the error. Am I missing something?

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <dc753a1061284f8e971ee88ee4826eee>:0)
Map.ShufflingExtension.Random[T] (System.Collections.Generic.IList`1[T] list) (at Assets/STSMap/Scripts/ShufflingExtension.cs:29)
Map.MapGenerator.PlaceLayer (System.Int32 layerIndex) (at Assets/STSMap/Scripts/MapGenerator.cs:84)
Map.MapGenerator.GetMap (Map.MapConfig conf) (at Assets/STSMap/Scripts/MapGenerator.cs:41)
Map.MapManager.GenerateNewMap () (at Assets/STSMap/Scripts/MapManager.cs:41)
UnityEngine.Events.InvokableCall.Invoke () (at <6f7018b8b8c149e68c4a65a05ac289be>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <6f7018b8b8c149e68c4a65a05ac289be>:0)
UnityEngine.UI.Button.Press () (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:530)

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 20 (9 by maintainers)

Most upvoted comments

By all means! Whatever works for you!

So, when you win, while you are in a different scene, you can do something like:

PlayerPrefs.DeleteKey("Map");
PlayerPrefs.Save();

This ensures that:

  • the map is deleted, so it will try to generate a new one next time
  • the player position is also deleted, so it will prompt the player to select 1 of the first nodes next time

Another thing that you can do before going into the map scene is set the act number, so you could do something like:

PlayerPrefs.SetInt("ActIndex", 1);

Then you load the map scene.

And in the map scene you can have a new script that would set the MapConfig based on the value in PlayerPrefs. For example:

public class ActSetter : MonoBehaviour
{
    public MapManager mapManager;
    public MapConfig[] acts;

    private void Awake()
    {
        var index = PlayerPrefs.HasKey("ActIndex") ? PlayerPrefs.GetInt("ActIndex") : 0;
        if (index < 0 || index >= acts.Length) index = 0;
        mapManager.config = acts[index];
    }
}

Add it to an object in the Map scene, link the MapManager and populate the acts array in the inspector.

And when you lose the game, you can also delete the map, but set the act to 0:

PlayerPrefs.DeleteKey("Map");
PlayerPrefs.Save();
PlayerPrefs.SetInt("ActIndex", 0);

Hope it helps.