Class selection?

how could i go about having an option for a player class selection before loading into the first level i was thinking showing each class and then selecting and loading whatever character you pick and loading up by using button events just wondering if anyone had ideas on how i could go about that i get key not found errors whenevr my different player classes are shown in a scene

I’m sorry, MrCream, I read this question at work last week and forgot to tag it so that I could get back to it when I got home.

First, I need to know a little bit about your setup.

Are your classes each represented by different Player prefabs with different models for each of them?
How are you saving/loading which class is the current class at runtime?

I may need to see the script you’re using to instantiate the classes.

i dropped the script i had and decided to get back to it later but i have the pregerssion setup and prefabs for each class i want an option for but no logic anymore related to saving/ loading any of that

these are the class prefabs i have

Here’s how I would go about this.

I would start with a PlayerSpawner class. The PlayerSpawner would have the SaveableEntity, and be responsible for spawning the correct prefab for the chosen class. It would also be responsible for gathering the components in Capture/RestoreState from the spawned character.

Something like this:

PlayerSpawner
public class PlayerSpawner : MonoBehaviour
{

    [SerializeField] private PlayerController[] playerClasses;
    private PlayerController currentCharacter;
    private int index = -1;

    void SpawnCharacter(int _index)
    {
        if (currentCharacter)
        {
            Destroy(currentCharacter.gameObject);
        }
        currentCharacter = Instantiate(playerClasses[_index], transform);
        index = _index;
    }

    [System.Serializable]
    struct PlayerSave
    {
        public int index;
        public Dictionary<string, object>  state;
    }
    
    public object CaptureState()
    {
        PlayerSave result = new PlayerSave();
        if (index < 0) result.index = -1;
        else
        {
            result.index = index;
            result.state = new Dictionary<string, object>();
            foreach (ISaveable component in currentCharacter.GetComponents<ISaveable>())
            {
                result.state[component.GetType().ToString()] = component.CaptureState();
            }
        }

        return result;
    }

    public void RestoreState(object state)
    {
        PlayerSave save = (PlayerSave)state;
        if (save.index < 0) index = -1;
        SpawnCharacter(save.index);
        foreach (ISaveable component in currentCharacter.GetComponents<ISaveable>())
        {
            if (save.state.ContainsKey(component.GetType().ToString()))
            {
                component.RestoreState(state[component.GetType().ToString()]);
            }
        }
    }
}

This script will go on a GameObject under Core, along with the SaveableEntity script. NONE of the Player Prefabs will have the SaveableEntity script, as this new Spawner will be responsible for saving the component data from the spawned prefabs.

In your main menu’s character selection, the buttons should select the class (and Spawn the new character for visual feedback). When the new game button is clicked, you’d then save the scene and then load the starting scene, and within the starting scene RestoreState() to load the character.

i think i undertsand that ill give it a shot later today thank you :smiley:

is there anything I should be adding the to the saving/persistent object prefab for when hitting play it loads the last save I’m getting an error currently after saving and then loading from the main menu

That looks like your trying to restore an inventoryItem that is not in a folder named Resources

it only happens when i click the play button in my main menu scene then load the scene i saved in, if i build then click play in game then save then quit then open the game again and click play to load the save it gives me that but if i start in the first level then play and save then re open from the first level it works fine could it be how im loading the first level after clicking play in game im just calling SceneManager.LoadScene(1);

Yes, because the Inventory is trying to restore an item that does not exist in a Resources folder.

In Inventory.CaptureState() (as well as ActionBar and Equipment), the UUID of the inventory item is captured and stored.
In RestoreState, that UUID is used to retrieve the item from a Dictionary of all items found in Resources folders (Resources.LoadAll<InventoryItem>()). If the Capture system saves an inventory item that is not in a Resources folder, then when you go to restore, Inventory.GetFromID() will return a null, and the error message is indicating (by reading the stack trace) that this is what is happening.

Make sure all InventoryItems (or their descendants, such as EquipableItems, etc) are in a folder named Resources. For example:

  • Game/Resources //Guaranteed to work, it’s in a Resources folder
  • Game/Resources/EquipableItems //This is legal because it is UNDER a Resources folder
  • Game/EquipableItems //This will not work.
  • Game/resources/ This will not work because the magic folder Resources is case sentitive.

its weird because everything related to inventories is under a Resources folder and it all works fine except when I load in from the main scene if I load in from the first or second level there’s no issues is there something I could be missing in the main menu?

i have all the scriptable object inventory stuff iunder that folder i thought maybe i need the prefabs as well but that didn’t do anything either

No, prefabs shouldn’t be in a Resources folder.

I think I’m going to need a closer look at your project.
Zip up your project and upload it to https://gdev.tv/projectupload. Be sure to remove the Library folder to conserve space.

its been giving me issues with file size being too large can i upload a .rar?

its still too large after compressing to zip and or compressing to .rar i could try to find some assets not being used and removing them to bring the file size down

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms