Load savegame: finding specific GameObjects

Discuss modding questions and implementation details.
Post Reply
User avatar
Ralzar
Posts: 2211
Joined: Mon Oct 07, 2019 4:11 pm
Location: Norway

Load savegame: finding specific GameObjects

Post by Ralzar »

So I recently threw together a fun little mod: Torch Taker.
It lets you click torches in the dungeons to pick them off the wall and add them to your inventory. Making this took about 10 minutes and was easy-peasy.

In practice it deactivates the torch GameObject you clicked and inserts a torch item into your inventory.


Then came the problem: I can't figure out any way to save any identifying data about the torch gameObjects that can also be used to deactivate the dungeon torches upon loading a savegame.
At the moment, the only data I could find that at least fits, is the Vector3 position data for the torches. However, I can't for the life of me figure out a way to use the position data to find the GameObject in that position and manipulate it.

The closest I've gotten so far is this:

Code: Select all

        private static void DouseTorches()
        {
            if (dousedTorches != null && dousedTorches.Count > 0)
            {
                List<GameObject> allUntaggedObjects = new List<GameObject>(GameObject.FindGameObjectsWithTag("Untagged"));
                foreach (GameObject obj in allUntaggedObjects)
                {
                    foreach (Vector3 torch in dousedTorches)
                    {
                        if (obj.transform.position == torch)
                        {
                            Torch = obj;
                            DouseTorch(Torch);
                        }
                    }
                }
            }
            else
                Debug.Log("Not running DouseTorches()");
        }
Which still does not work because the torches appear to not acutally be tagged with "Untagged" but with null.


So, am I way off in how I'm even trying to solve this? Is there another way to go about this?


GitHub link

User avatar
Hazelnut
Posts: 3016
Joined: Sat Aug 26, 2017 2:46 pm
Contact:

Re: Load savegame: finding specific GameObjects

Post by Hazelnut »

Not sure how to do this, but I was trying to understand the code and I cant work out why you destroy all lights on entering a dungeon and then recreate them for archive 210 only. Seems like this would remove some lights that have nothing to do with torches, but as I said I can't follow it.
See my mod code for examples of how to change various aspects of DFU: https://github.com/ajrb/dfunity-mods

User avatar
Ralzar
Posts: 2211
Joined: Mon Oct 07, 2019 4:11 pm
Location: Norway

Re: Load savegame: finding specific GameObjects

Post by Ralzar »

(Edit: updated the github)

Haha, I solved this but in an "Oh no..." kinda way.

There are two problems:

1: I can't identify the individual torches except by their Vector3 position.

2: The vanilla lights attached to the torches are not attached to the torches. Instead they are placed slightly off, making them a separate object.


Solution to 1: As you take torches, the vector3 gets saved. When you load, the mod takes each saved vector3 and casts a ray from below and up. If it hits a torch sprite, it removes/deactivates that game object.

Solution to 2: To make the vanilla lights disappear when you remove the torch, I needed the light to be attached to the torch. So upon entering a dungeon, the mod simply strips out all torch light and makes new identical light sources that are attached to the torches.

Post Reply