raymond
New Member
Posts: 47
Posts: 47
|
Post by raymond on Sept 4, 2017 16:00:28 GMT
Hello,
I want an option to change the game object layer when spawned. I am using PoolBoss but am using a different Spawner other than DarkTonics for this particular part of my game. I am referencing the Instantiate GameObject so I have assigned it to a variable. It spawns fine but on the default layer. My code is below:
public int spawnLayer; //gives me the ability to change layer in the inspector
GameObject newItem = PoolBoss.SpawnInPool(_gameObject.transform, spawnLocation, spawnRotation).gameObject as GameObject;
newItem.layer = spawnLayer;
Thanks for any help.
|
|
|
Post by DarkTonic Dev on Sept 4, 2017 17:31:08 GMT
That code should work fine, I do stuff like that all the time - what's the problem?
|
|
raymond
New Member
Posts: 47
Posts: 47
|
Post by raymond on Sept 5, 2017 0:27:04 GMT
Sorry my mistake,
I now realize I am using the Core Gamekit Spawner. I placed the above code in the wrong script and that is why it was not showing up.
How would I go about changing the spawned Game Object Layer using your DarkTonic Spawner? I am not sure where to place code similar to the above. I don't really want to change your code directly due to Core Gamekit updates in the future that would just overwrite my modifications.
Would be nice to reference the spawned object with a completly separate simple script just for this purpose.
Thanks,
|
|
|
Post by DarkTonic Dev on Sept 5, 2017 0:35:54 GMT
Oh. Well the spawners in CGK already have this ability. You don't need to code anything.
Syncro Spawners have the section near the top: Spawn Layer Mode. You can choose "Custom", then select the layer from a dropdown. Triggered Spawners have the same setting as well.
If you wanted to do any other modifications by script you can subclass the spawner class and use that instead of the normal spawner class, then override the appropriate method, call the base.method and add code for whatever. We do this in our games.
Or you can just add a Listener subclass and do the same for most things.
|
|
raymond
New Member
Posts: 47
Posts: 47
|
Post by raymond on Sept 5, 2017 4:24:11 GMT
Thanks,
Overlooked the Spawn Layer Mode at the top.
I used the custom setting to set the Custom Spawn Layer. It does work for the Game Object Parent but my Game Object has children that don't change over to the correct layer. Is there a way to do this using this Custom Spawn Layer?
In addition to the above I also tried making a Listener for it. To test it I wrote directly in your TriggerSpawnerListener script.
It changes the Parent Game Object Layer but I need to work on the appropriate code to change the children as well.
Should I copy the Lister Code over to a new script and use that to Subclass rather than modifying your original Listener script? (My instincts say Yes but I am much more of an artist than a programmer).
Thanks
|
|
raymond
New Member
Posts: 47
Posts: 47
|
Post by raymond on Sept 5, 2017 5:37:14 GMT
UPDATE ON MY ATTEMPT.
I have succesfully changed the childrens layer.
Modified the TriggeredSpawnerListener.cs with this:
public virtual void ItemSpawned(TriggeredSpawner.EventType eType, Transform spawnedTrans) { // do something to the Transform. // Destroy(spawnedTrans.gameObject); spawnedTrans.gameObject.layer = 11; //also can be written as (spawnedTrans.gameObject).layer = 11; foreach (Transform child in spawnedTrans) {
child.gameObject.layer = 11; } }
But I still don't know if I should copy the Lister Code over to a new script and use that to Subclass rather than modifying your original Listener script?
Honestly I still need to learn how to Sub Class. I have not used that technique yet.
|
|
|
Post by DarkTonic Dev on Sept 5, 2017 5:56:12 GMT
Yes - always subclass, that way you will not be limited to a single Triggered Spawner Listener. You can make as many different ones as you like, and not have to add weird logic into a single multi-purpose one if you went that way. That's a nightmare to maintain and debug sometimes.
Are you saying we should add a checkbox to the Spawn Layer section to "recursively apply to all children"? We can do that if so.
Making a subclass is easy. Just put ": baseClassName" on the class name line. The subclass will only have whichever methods you want to override, and the rest can be left out. And the ones you are overriding need to say "override", like:
public override void ItemSpawned() { base.ItemSpawned(); // always call base.sameMethod name so it will do both the base class implementation (if any) and the new one, unless you specifically don't want it. Even if it's blank right now in the base, you will forget later and it will cause major confusion.
// do other stuff specific to this subclass }
or whatever.
I believe there is some sort of subclass in the example Scenes. Look for a class having "subclass" in the name.
|
|
raymond
New Member
Posts: 47
Posts: 47
|
Post by raymond on Sept 5, 2017 7:05:13 GMT
I see the advantage to use Listeners in my case but YES it would be a nice option in the Inspector to change the layers of children too. I build many of my game objects in a parent game object. Most of my scripts will be on the Parent Object and the actual model with be a child.
I have watched a couple of videos on Inheritance and think I understand how to use subclass at a novice level. I now realize I will have a separate listener script that inherits from your listener script. Wish I learned this before.
Thanks for your help. I will see how far I get. I'm sure I will have more questions.
|
|
|
Post by DarkTonic Dev on Sept 5, 2017 15:53:12 GMT
Cool, I'll put it on the roadmap since it's very easy to add. Good that you even had the insight to ask the question "should I subclass". That says you're more of a programmer than you admit to 
|
|