Saving multiple enemy positions

Discussion and help for Easy Save 3
Post Reply
Fezza
Posts: 4
Joined: Mon Mar 21, 2022 12:21 pm

Saving multiple enemy positions

Post by Fezza »

Hi There!

Im testing out saving the position of multiple enemies. As of now i have the amount of enemies in a scene, but when i save it and load they all spawn on the same point. This is because the save method uses a tag, so i am unsure how i can increment that tag or if there is another way maybe using lists? i tried using lists but that would still result in me assigning a tag for each.

Attached is the code for the GameData, the saving & Loading. Thanks!

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameData : MonoBehaviour
{
    [Header("Enemies")]
    public List<GameObject> enemies;
    public GameObject[] amountOfEnemies;



    public void Awake()
    {
        amountOfEnemies = GameObject.FindGameObjectsWithTag("Enemy");
    }
}

Code: Select all

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SaveSystem : MonoBehaviour
{
    public ManagerScript ms;

    public GameData gd;

    public Transform player;


    public void Save()
    {
        ManagerScript script = ms.GetComponent<ManagerScript>();
        ES3.Save("Current Ammo In MP5", script.mp5._currentAmmo);
        ES3.Save("Player Position" , player.transform.position);
        ES3.Save("Current Weapon", script.awR.playersActiveWeapon);
        ES3.Save("9MM Reserve", script.playerAmmoBag.nineMM);

        GameData gdContainer = gd.GetComponent<GameData>();
        foreach(GameObject enemies in gdContainer.amountOfEnemies)
        {
            ES3.Save("EnemeyPostion", enemies.transform.position);
            Debug.Log("Saving enemy");
        }
    }
}

Code: Select all


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadSystem : MonoBehaviour
{
    public ManagerScript ms;

    public Transform player;

    public GameData gd;

    public void Load()
    {
        player.transform.position = ES3.Load<Vector3>("Player Position");

        ManagerScript script = ms.GetComponent<ManagerScript>();
        script.awR.playersActiveWeapon = ES3.Load<activeWeapon>("Current Weapon");
        script.mp5._currentAmmo = ES3.Load<int>("Current Ammo In MP5");
        Debug.Log("Loading " + script.awR.playersActiveWeapon.ToString());
        script.playerAmmoBag.nineMM = ES3.Load<int>("9MM Reserve");

        GameData gdContainer = gd.GetComponent<GameData>();
        foreach(GameObject enemies in gdContainer.amountOfEnemies)
        {
            enemies.transform.position = ES3.Load<Vector3>("EnemeyPostion");
            Debug.Log("Loading enemy");
        }
    }
}
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving multiple enemy positions

Post by Joel »

Hi there,

As you mention, you're overwriting the same tag. In this circumstance you would use a list instead. I.e. something along the lines of:

Code: Select all

var positions = new List<Vector3>();

foreach(GameObject enemies in gdContainer.amountOfEnemies)
    positions.Add(enemies.transform.localPosition);

ES3.Save("positions", positions);
And then to load:

Code: Select all

var positions = ES3.Load<List<Vector3>>("positions");
for(int i=0; i<positions.Count; i++)
    gdContainer.amountOfEnemies[i].transform.localPosition = positions[i];
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Fezza
Posts: 4
Joined: Mon Mar 21, 2022 12:21 pm

Re: Saving multiple enemy positions

Post by Fezza »

Works!

Thank you so much!
Post Reply