Forum begins after the advertisement:


[Part 7] Map UI problems

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #14253
    Anuk Thotawatta
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    My maps are not named in number order. Instead they are named like this:

    View post on imgur.com

    And my mapManager.cs :

    <code>using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class MapManager : MonoBehaviour
    {
        // Start is called before the first frame update
    
        [SerializeField] GameObject[] maps;
        Coffin coffin;
    
        private void OnEnable() {
            coffin = FindObjectOfType<Coffin>();
            if(coffin != null){
                if(coffin.interacted){
                    UpdateMap();
                }
            }
        }
    
        void UpdateMap(){
            var savedScenes = SaveData.Instance.sceneNames;
            for(int i=0;i<maps.Length;i++){
                if(savedScenes.Contains("Cave_"+i+1)){
                    maps[i].SetActive(true);
                }
                else{
                    maps[i].SetActive(false);
                }
            }
        }
    }
    </code>

    how do i modify this code to work with my naming system? i Also have alot of interconnected scenes. will the above code work if i named them in order?

    #14254
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    Right now, the code should only accept naming systems of “Cave_number. It should not be able to take your style of naming the caves.

    First off, I believe this is incorrect.

        void UpdateMap(){
            var savedScenes = SaveData.Instance.sceneNames;
            for(int i=0;i < maps.Length; i++){
                if(savedScenes.Contains("Cave_"+  i+1  (i + 1))){
                    maps[i].SetActive(true);
                }
                else{
                    maps[i].SetActive(false);
                }
            }
        }

    If you want to modify the code, one simple solution is to; First, rename your subcaves from “Cave_1.1” > “Cave_1.2”, to something like “FirstCave_1” > “FirstCave2″. Simply try avoid adding decimals to the name. You could stick with the intended naming convention of “Cave1-any number“, but if not, try changing the “Cave” part to another similarly identifiable word.

    Then you can add on an extra line of else if statement in the UpdateMap()

        void UpdateMap(){
            var savedScenes = SaveData.Instance.sceneNames;
            for(int i=0; i < maps.Length; i++){
                if(savedScenes.Contains("Cave_"+ (i + 1))){
                    maps[i].SetActive(true);
                }
                else if(savedScenes.Contains("FirstCave_"+ (i + 1))){
                    maps[i].SetActive(true);
                }
                else{
                    maps[i].SetActive(false);
                }
            }
        }

    I haven’t tested these, but with this knowledge, you should be able to create your own version of the map without much change.

    #14255
    Anuk Thotawatta
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    thanks alot. I just changed the names to cave_1,cave_2,cave_3… and now almost everything works fine.

    but now for some reason the 14th room always appears, even if i hadn’t gone there

    View post on imgur.com

    also everything resets when i restart the game

    #14256
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    Likely what has happened is either, “Cave_14” is set active in your scene instead of inactive, or there is already a save file stating that you’ve been to “Cave_14”. So just delete the saved maps’ sceneNames file.

    As for the maps not reloading on start, you just need an Awake() or Start() that activates the updatemap method.

    #14257
    Anuk Thotawatta
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    Cave_14 is set to inactive. What did you mean by “delete the saved maps’ sceneNames file” ?

    #14258
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    In MapManager.cs UpdateMap(), the savedScenes are taken from the SaveData.cs HashSet called sceneNames. Try clearing the saved HashSet by following this: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.clear?view=net-8.0

    #14259
    Anuk Thotawatta
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    I did this to the MapManager.cs and the map doesnt appear at all

    <code>void UpdateMap(){
            var savedScenes = SaveData.Instance.sceneNames;
            SaveData.Instance.sceneNames.Clear();
            for(int i=0;i<maps.Length;i++){
                if(savedScenes.Contains("Cave"+(i+1))){
                    maps[i].SetActive(true);
                }
                else{
                    maps[i].SetActive(false);
                }
            }
        }</code>

    And then i removed the SaveData.Instance.sceneNames.Clear(); line and still all of the map segments remain invisible

    #14260
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    Try saving scenes and opening maps again, now that you removed the sceneNames.Clear() function.

    Again, if you know how the code works, what we are doing is using the Clear() function once to reset the state of your map, you can remove it for future attempts, but to reset your map progress, use Clear(). With the code now removed, your Map should work normally through it’s intended use of saving scenes to open map.

    #14261
    Anuk Thotawatta
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    ok i figured it out. cave 14 was visible because it was the scene open in the editor. when i switch to the scene Cave_1, the cave_1 map segment is visible. if i start from the main menu everything works fine.

    The only problem now is resting at a checkpoint. i have to mash F multiple times get the message “Rested in checkpoint”. I sometimes have to jump around while pressing F to get the message.

    View post on imgur.com
    View post on imgur.com

    I also have to rest twice to update the map. how do i fix that?

    View post on imgur.com
    #14262
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    Try this out:

    [Part 7] Save Function
    #14266
    Anuk Thotawatta
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    Everything works thanks alot

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: