digitom
New Member
Posts: 2
Posts: 2
|
Post by digitom on Oct 15, 2015 17:59:08 GMT
Hello. was wondering how to store a spawned game object as a variable. basically so I can manipulate the spawned object without having to attach a script to the spawned object itself. if (fire) { PoolBoss.SpawnInPool(prefabToSpawn,transform.position,transform.rotation); spawned =    ?? rb = spawned.GetComponent<Rigidbody>(); rb.AddForce(0,0,10,ForceMode.Impulse); } Any help is appreciated! Thanks!
|
|
|
Post by DarkTonic Dev on Oct 15, 2015 19:39:26 GMT
Hello. was wondering how to store a spawned game object as a variable. basically so I can manipulate the spawned object without having to attach a script to the spawned object itself. if (fire) { PoolBoss.SpawnInPool(prefabToSpawn,transform.position,transform.rotation); spawned =    ?? rb = spawned.GetComponent<Rigidbody>(); rb.AddForce(0,0,10,ForceMode.Impulse); } Any help is appreciated! Thanks! I would do this: var spawned = PoolBoss.Spawninpool(your params); spawned.Getcomponent, etc
However usually this isn't what you want to do. 1) if you need code to always fire when that prefab is spawned, add code to an OnSpawned method in a script on the prefab. That will get called automatically. 2) if you are using Killables or our spawners, you can use the Listener classes or a subclass and override what happens during a spawn.
|
|
digitom
New Member
Posts: 2
Posts: 2
|
Post by digitom on Oct 16, 2015 15:11:23 GMT
Awesome thanks!
|
|
|
Post by DarkTonic Dev on Oct 16, 2015 15:24:57 GMT
No problem.
|
|
Basbo
New Member
Posts: 5
Posts: 5
|
Post by Basbo on Feb 24, 2016 10:42:22 GMT
Hei I have nearly the same problem addressing the spawned prefabs.
my old line: GameObject proj = Instantiate(projectile, projectileSpawns[i].transform.position, Quaternion.Euler(projectileSpawns[i].transform.forward)) as GameObject;
with Poolboss spawn: GameObject proj = PoolBoss.SpawnOutsidePool(projectile, projectileSpawns[i].transform.position, Quaternion.Euler(projectileSpawns[i].transform.forward));
further code: proj.GetComponent<BaseProjectile>().fireProjectile(projectileSpawns[i], target, damage); m_lastProjectiles.Add(proj);
Ok, if I replace instantiate with Poolboss like shown in your example, I get 2 errors:
Assets/2-Scripts/ShootingSystem.cs(79,60): error CS1502: The best overloaded method match for `DarkTonic.CoreGameKit.PoolBoss.SpawnOutsidePool(string, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments Assets/2-Scripts/ShootingSystem.cs(79,60): error CS1503: Argument `#1' cannot convert `UnityEngine.GameObject' expression to type `string'
So i would love to use instead the event solution proposed, but my problem there: the called <BaseProjectile>().fireProjectile is an abstract class from a tutorial and I don't know how to integrate in this case my event 'OnSpawned'. At the moment instantiated projectiles are saved in the array m_lastProjectiles - is there a way with Poolboss api to collect the spawned projectiles (shot from this turret eg.) to despawn them if the turret gets inactive?
Any help is appreciated! Thx
|
|
Basbo
New Member
Posts: 5
Posts: 5
|
Post by Basbo on Feb 24, 2016 10:43:57 GMT
...By the way I saw this is my first post here  So I have to say how awesome to use is Coregamekit and Masteraudio, love them, great work!
|
|
|
Post by DarkTonic Dev on Feb 24, 2016 17:01:05 GMT
Hei I have nearly the same problem addressing the spawned prefabs. my old line: GameObject proj = Instantiate(projectile, projectileSpawns[i].transform.position, Quaternion.Euler(projectileSpawns[i].transform.forward)) as GameObject;
with Poolboss spawn: GameObject proj = PoolBoss.SpawnOutsidePool(projectile, projectileSpawns[i].transform.position, Quaternion.Euler(projectileSpawns[i].transform.forward));
further code: proj.GetComponent<BaseProjectile>().fireProjectile(projectileSpawns[i], target, damage); m_lastProjectiles.Add(proj);
Ok, if I replace instantiate with Poolboss like shown in your example, I get 2 errors: Assets/2-Scripts/ShootingSystem.cs(79,60): error CS1502: The best overloaded method match for `DarkTonic.CoreGameKit.PoolBoss.SpawnOutsidePool(string, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments Assets/2-Scripts/ShootingSystem.cs(79,60): error CS1503: Argument `#1' cannot convert `UnityEngine.GameObject' expression to type `string'
So i would love to use instead the event solution proposed, but my problem there: the called <BaseProjectile>().fireProjectile is an abstract class from a tutorial and I don't know how to integrate in this case my event 'OnSpawned'. At the moment instantiated projectiles are saved in the array m_lastProjectiles - is there a way with Poolboss api to collect the spawned projectiles (shot from this turret eg.) to despawn them if the turret gets inactive? Any help is appreciated! Thx I have no idea what the script you are in is doing, but a few things: 1) The first parameter you should be passing "projectile.transform", not "projectile". It takes transforms not GameObjects, and returns Transform as well (may be the 2nd error but I can't see your code). 2) You can edit the abstract class and add an OnSpawned method if you need one. It should automatically be included in any subclasses. 3) Pool Boss has a way to get "all of spawned pool item X", but has no idea what spawned them. There is also an option on Killable to "die when spawner destroyed" to automatically kill the spawns. Probably that's what you want. Doesn't need any code written.
|
|
|
Post by DarkTonic Dev on Feb 24, 2016 17:01:34 GMT
...By the way I saw this is my first post here  So I have to say how awesome to use is Coregamekit and Masteraudio, love them, great work! Glad you like them! We use them in every project.
|
|
Basbo
New Member
Posts: 5
Posts: 5
|
Post by Basbo on Feb 26, 2016 11:06:19 GMT
Ok thanks! Problem was actually the transform instead of GameObject, this part works great. :-)
Now I'm on another aspect and wondering if/how to register several listeners for one killable. My setup in short: Several turrets are detecting and shooting lots of wandering targets, some kind of Towerdefense... The turrets/towers create and update a list of targets who are in range of the respective tower and choose out of this list the nearest/furthest etc. target to destroy. All targets are of course killables, and I use a KillableListenerSubscript on the tower to detect which targets are killed and have to be deleted from the actual target list. That works awesome so far, but: There are different towers and I can only register one listener on the killable - so how can I achieve the functionality needed and what would be a performance friendly solution for this setting? Some kind of custom event 'killable despawned' received by all towers in the level? I hope you have some hints or some killable options I haven't realized yet, cause this is my first time to use CoreGamekit in this way.. ;-)
|
|
Basbo
New Member
Posts: 5
Posts: 5
|
Post by Basbo on Feb 26, 2016 12:02:50 GMT
Just now I've tried to use this.sourceKillableName in the KillableListenerSubclass but the variable isn't referenced anymore when the Despawning Event is triggered. How can I see what context objects/information is passed by? Sorry when I'm a bit confused, events in C# are still some kind of mystery for me
using System.Collections.Generic; using DarkTonic.CoreGameKit; using UnityEngine;
public class KillableListenerCreep: KillableListener { // if you need more than one Listener for of each type (KillableListener etc), create subclasses like this, inheriting from KillableListener
RangeChecker m_rangeChecker;
void Start () { m_rangeChecker = GetComponent<RangeChecker>(); }
public override void Despawning(TriggeredSpawner.EventType eType) { base.Despawning(eType); // your code here. m_rangeChecker.RemoveDespawnedTargets(this.sourceKillableName); Debug.Log("KillableListenerSubclass - target will despawn"); }
/// <summary> /// This method gets called when the Killable's spawner is despawning (after Despawning). /// </summary> public override void Despawned() { // your code here Debug.Log("KillableListenerSubclass - target died");
}
public override void TakingDamage(int pointsDamage, Killable enemyHitBy) { base.TakingDamage(pointsDamage, enemyHitBy); // your code here. }
...
|
|
|
Post by DarkTonic Dev on Feb 26, 2016 19:52:55 GMT
Are your Killables spawned by Trigggered Spawners or Syncro Spawners? Different answer depending on which you are using.
Anyway, you one Killable Listener can do whatever it likes, I.e. Inform multiple other objects if needed. You will need to code that yourself though.
|
|
|
Post by DarkTonic Dev on Feb 27, 2016 0:27:57 GMT
Just now I've tried to use this.sourceKillableName in the KillableListenerSubclass but the variable isn't referenced anymore when the Despawning Event is triggered. How can I see what context objects/information is passed by? Sorry when I'm a bit confused, events in C# are still some kind of mystery for me using System.Collections.Generic; using DarkTonic.CoreGameKit; using UnityEngine;
public class KillableListenerCreep: KillableListener { // if you need more than one Listener for of each type (KillableListener etc), create subclasses like this, inheriting from KillableListener
RangeChecker m_rangeChecker;
void Start () { m_rangeChecker = GetComponent<RangeChecker>(); }
public override void Despawning(TriggeredSpawner.EventType eType) { base.Despawning(eType); // your code here. m_rangeChecker.RemoveDespawnedTargets(this.sourceKillableName); Debug.Log("KillableListenerSubclass - target will despawn"); }
/// <summary> /// This method gets called when the Killable's spawner is despawning (after Despawning). /// </summary> public override void Despawned() { // your code here Debug.Log("KillableListenerSubclass - target died");
}
public override void TakingDamage(int pointsDamage, Killable enemyHitBy) { base.TakingDamage(pointsDamage, enemyHitBy); // your code here. }
... SourceKillableName isn't anything you'd be wanting to use for this anyway. Maybe you want to subclass Killable so you can use the Killable itself as a key in your list? You can override the DestroyKillable method and add code there.
|
|
Basbo
New Member
Posts: 5
Posts: 5
|
Post by Basbo on Feb 27, 2016 8:12:14 GMT
Are your Killables spawned by Trigggered Spawners or Syncro Spawners? Different answer depending on which you are using. Anyway, you one Killable Listener can do whatever it likes, I.e. Inform multiple other objects if needed. You will need to code that yourself though. I'm using Synchro Spawners / Global Waves so far - ok then I'll give some kind of MaserTargetListener a try.
|
|
|
Post by DarkTonic Dev on Feb 27, 2016 18:01:58 GMT
Cool.
|
|