[SOLVED][C#]Destroy check on parent/child?

Started by Master Bucketsmith, August 31, 2016, 06:34:28 AM

Previous topic - Next topic

Master Bucketsmith

Hey,

I've been tinkering with some code in relation to a mod that creates a custom turret.
So far, for the functionality, all code is dependant on BuildingTurret_Gun, which is a child (I think) of BuildingTurret.
When the turret changes target or simply has no valid target, the altered code successfully changes with it or destroys the spawned glower buildings.
The problem arises when the turret is destroyed, by the various means of destruction the game has.

By moving my destruction checks around within the code of BuildingTurret_Gun, I get various results whether or not the spawned glowers also get destroyed.
As far as I'm able to deduce this is logically sound; the checks are within the BuildingTurret_Gun, so once that is destroyed, so is the code for those checks to remove the spawned glowers.
I'm in need of some sort of check that says if the turret/turretweapon is destroyed, it's glower buildings should also be destroyed.
If I write a second class that works with BuildingTurret, I can't call for condition checks for it's child BuildingTurret_Gun can I? Since they're different classes?

I've learned a little bit about C# by now, but not enough to figure out where I need to look. Any ideas?

skullywag

So you want to destroy glower buildings (im guessing made from projectile that the gun fired) when the turret is destroyed. Your best bet is to reverse your logic, have the projectile/building check back to see if its parent is not destroyed. Id need to know how youve achieved what you have already to be more specific.
Skullywag modded to death.
I'd never met an iterator I liked....until Zhentar saved me.
Why Unity5, WHY do you forsake me?

Master Bucketsmith

Quote from: skullywag on August 31, 2016, 07:03:22 AM
So you want to destroy glower buildings (im guessing made from projectile that the gun fired) when the turret is destroyed. Your best bet is to reverse your logic, have the projectile/building check back to see if its parent is not destroyed. Id need to know how youve achieved what you have already to be more specific.
No, I've 'branched off' of the targeting logic to create a much smoother experience of the glower building following its target.
It bypasses the projectile entirely, which is now simply an empty class. I haven't found a way to get rid of that portion of the firing mechanism, because it is embedded in private routines for Tick and one other I can't name out of the top of my head.
It simply checks if it has a new position once every X ticks (as long as I'm still building the mod, I have not implemented performance control as it is not heavy enough to cause problems during development) and if it does, it destroys the current one and spawns a new one.
A secondary glower is spawned on position of the turret.
All this happens within a check if the current target is not null and is valid, so they disappear/'go off' if that's no longer the case.
But those checks only happen if the BuildingTurret_Gun still exists. It never checks whether it should remove existing glowers once BuildingTurret_Gun is gone, because all of the code is within BuildingTurret_Gun.

I can go deeper in my explanation and show you the code I have so far, but not over the forum, as I want to be able to quickly ask simple questions to get some things out of the way.
After all, 'spoonfeeding' doesn't work, as some would say. ;)

The check for its parent, how is that done?

skullywag

there has to be some connection between the glower building and the turret, if you dont have this, then itll never work. You may have to add a custom field to the building (extend thingdef via custom <def Class="mythingdefextension">) then you can populate that new field with the parent thing on creation of the building, then you have your link and can remove all glowers with the thing set to the parent in the destroy method.
Skullywag modded to death.
I'd never met an iterator I liked....until Zhentar saved me.
Why Unity5, WHY do you forsake me?

Master Bucketsmith

Quote from: skullywag on August 31, 2016, 08:01:46 AM
there has to be some connection between the glower building and the turret, if you dont have this, then itll never work. You may have to add a custom field to the building (extend thingdef via custom <def Class="mythingdefextension">) then you can populate that new field with the parent thing on creation of the building, then you have your link and can remove all glowers with the thing set to the parent in the destroy method.
The connection is that it's a child of BuildingTurret_Gun and overrides Tick(). If I get that right.
All of the code is in that child, so as far as I can see, there's no check to perform once the parent is destroyed, as it also destroys the child.

I'm not sure I get the part about a custom thingdef. I don't think I've ever messed around with that and I haven't seen it in any mods I've glanced at the code for.
This differs from a custom thingClass?

skullywag

yes, quickest example i can find is:

https://github.com/Skullywag/LaserWeapons/blob/master/Defs/ThingDefs/Weapons_Laser.xml where i set the custom thingdef class and:

https://github.com/Skullywag/LaserWeapons/blob/master/Source/LaserWeapons/ThingDef_LaserProjectile.cs

where i set the new fields.

Im still confused it sounds like you simply to need to add a check in the destroy method of the building turret gun to destroy its child glowers, no?
Skullywag modded to death.
I'd never met an iterator I liked....until Zhentar saved me.
Why Unity5, WHY do you forsake me?

Master Bucketsmith

Quote from: skullywag on August 31, 2016, 08:14:36 AM
yes, quickest example i can find is:

https://github.com/Skullywag/LaserWeapons/blob/master/Defs/ThingDefs/Weapons_Laser.xml where i set the custom thingdef class and:

https://github.com/Skullywag/LaserWeapons/blob/master/Source/LaserWeapons/ThingDef_LaserProjectile.cs

where i set the new fields.

Im still confused it sounds like you simply to need to add a check in the destroy method of the building turret gun to destroy its child glowers, no?
I've already looked at the code of your laser weapons, but not in regards to this. :)
I'll have a glance at that.
Won't doing that mean I would have to copy over ALL of the vanilla turret code to make it work the same - minus the alterations I want?

This is the current check I have:
        public override void Destroy(DestroyMode mode = DestroyMode.Vanish)
        {
            base.Destroy(mode);
            InstallBlueprintUtility.CancelBlueprintsFor((Thing)this);

            if (!this.selfLight.DestroyedOrNull())
            {
                //this.selfLight.Destroy();
                this.selfLight = null;
            }
            if (!this.searchLight.DestroyedOrNull())
            {
                //this.searchLight.Destroy();
                this.searchLight = null;
            }

            if (mode != DestroyMode.Deconstruct)
                return;
            SoundStarter.PlayOneShot(SoundDef.Named("BuildingDeconstructed"), (SoundInfo)this.Position);
        }

Copied from Building, plus my alterations.
If I keep the commented lines in, it will upon using the devmode destroy tool that it wants to destroy an already destroyed thing - I think it is wanting to destroy it twice.
I've tried it without the if statements first. I've tried moving it around and using if statements in various ways but it always comes up with one of a number of problems and/or errors. I've lost track of what method gives what result. XD

selfLight = the glower placed on the turret with a fixed position
searchLight = the glower placed on the target, that continuously checks if the target has a new position and functions accordingly.

skullywag

Just out of interest have you tried using the damage tool to see how it reacts when destroyed through valid means. Ive found some interesting things go wrong when using certain tools in the dev options.
Skullywag modded to death.
I'd never met an iterator I liked....until Zhentar saved me.
Why Unity5, WHY do you forsake me?

Master Bucketsmith

#8
Yes. Depending on where I put those destroy lines, the damage tool works or it leaves glowers.
With my limited knowledge of the gamecode, I figured that there might be other instances in the game where it uses the same method as the devmode tool. Hence I wanted to make it errorproof, in case it could go wrong elsewhere if I McGuyvered it.

There's two routines that are called for the normal functionality of the 'turret', that also call for destruction;
        public void SwitchOffSelfLight()
        {
            if (!this.selfLight.DestroyedOrNull())
            {
                this.selfLight.Destroy();
                this.selfLight = null;
            }
        }

Substitute 'selfLight' for 'searchLight' for the other one, they're identical. They need to be separated, otherwise the selfLight glower will be destroyed and not remade if the target of the 'turret' changes position. (In other words, the selfLight glower relies on a static position and should not disappear on the same if statement the searchLight glower uses.)

For the curious mind, Rikiki's mining helmet code helped inspire my work. :)
I've written this piece of code with the help of a programmer, who took the time to tell me the structure and logic and answered questions that arose. :D
I've adapted and implemented some things on my own that seem to work, aside from the fact I can't get that destroy-if-parentbuilding-turret-is-destroyed thing to work right.

EDIT:
In regards to your LaserWeapons snippets, I don't get why you made a custom ThingDefClass. Why use that when you already have a custom ThingClass?

skullywag

for me calling the destroy method has always worked when calling it on an instance of a thing. Not sure whats wrong here.

In answer to your second question, yes it could be done differently it was the only example i could think of quickly, basically you do this if you are adding custom fields (xml) to thingdefs for use in the custom class (you want to make them configurable by the player for instance), which I did want to do.
Skullywag modded to death.
I'd never met an iterator I liked....until Zhentar saved me.
Why Unity5, WHY do you forsake me?

Master Bucketsmith

I see. The only thing I can think of is that my code resides within BuildingTurret_Gun, and not BuildingTurret. So once the turret is destroyed as a result of the base structure being destroyed, all of my code is already gone and there is nothing left to also remove the spawned glower buildings.
In effect, all my mod does is tell the game to spawn or destroy those glower buildings as long as there is a valid target for the turret active.
I'm scratching my head on how I could make sure any 'remnants' get destroyed along with the turret.

Master Bucketsmith

Someone took the time to glance over the code with me and we managed to figure out a way to implement this. :)
Solved.

skullywag

Glad you got it fixed, sorry i caught the lurg and kind of slumped into a small coma....ughh i feel rough
Skullywag modded to death.
I'd never met an iterator I liked....until Zhentar saved me.
Why Unity5, WHY do you forsake me?

Master Bucketsmith

Quote from: skullywag on September 02, 2016, 03:06:33 AM
Glad you got it fixed, sorry i caught the lurg and kind of slumped into a small coma....ughh i feel rough
No idea what a 'lurg' is, but that's all fine. :)
Right now I'm trying to figure out why the hell this mod degrades performance so much when on time compression. With just one instance of the turret spawned, it gets a lot worse with multiple spawned. :(

skullywag

Whats it doing on its tick method. Any heavy processing in there?

Also lurg = cold/sickness
Skullywag modded to death.
I'd never met an iterator I liked....until Zhentar saved me.
Why Unity5, WHY do you forsake me?