I have a piece of road that spawns an obstacle in one of three slots in its construction events. And by spawning I mean it adds a child actor component. I now want these child actor components (obstacles) to continue to exist even after the piece of road is destroyed.
I’ve googled around and experimented around for a long time now and can’t figure it out. How do I detach these obstacles from the road so they don’t get destroyed when the road destroys itself?
you would need to create a new actor and attach the component to the new actor. this however isnt ideal so what i would do is to spawn the obstacles as their own actor which would bypass the issue entirely. or at the very least you could spawn the obstacles then attach to road actor so that you could detach them if / when desired.
why not spawn the cars at runtime? i assume your creating the road as the game goes on right. spawning something at runtime vs in construction isnt much difference timewise.
Yes, that’s what I tried to do, however the road spawns the obstacle onto itself in the construction event, which doesn’t let me use “spawn actor from class”. Is there another way to create an obstacle when the road gets created?
The way this thing works (its from the endless runner tutorial):
- Piece of road gets spawned, it has 3 arrows attached, each representing a spawn position for the obstacle
- The locations of the 3 arrows get put into an array
- In the construction event the piece of road creates a child actor (because spawn actor from class doesnt work in the construction event) at the location of a random arrow out of the three arrows.
To learn more I’m trying to modify this so it spawns cars instead of obstacles. These cars drive forward on the road, which is why I don’t want to destroy them when the piece of road it was spawned on leaves the scenery. Since they both travel in opposite directions the piece of road gets destroyed behind the player when the cars are still visible, meaning they disappear in front of the players eyes.
When trying to spawn them at runtime (I use the beginPlay event) , it always spawns them at the location of the very first piece of road, it’s as if it doesn’t update the arrow locations after the first time when doing it this way. Even calling the “set spawnpoj ya” script that populated the array with the 3 arrow locations again before spawning results in them being spawned in in one of the first 3 locations. I’m sure I’m doing something wrong there but that’s the reason why I’ve not continued with that approach.
what does your script look like?
Right now this is what it looks like. (Screenshot attached). It’s in the event graph of the floor tile (piece of road). The cars get spawned and start driving from only the position of the very first piece of road. I attached a screenshot of that as well. In the distance you can see the player character driving, the road being deleted behind it, and the cars spawning from where the first piece of road spawned.
I’ve also tried calling the set spawn points function from the construction event and not with “beginplay” in the event graph but that doesn’t seem to change anything.
![alt text][3]
you left out the important bit here, the setting of the spawn points function. we can see from the picture that everything is spawning in correctly just not in the correct location so it has to be the way that the spawn location is set.
Oh yeah, right. As I said, the spawning works correctly when called from the construction script. (Just to reiterate). Thanks for your help!
Here’s how the set spawn points script looks:
i would say this is a relative to world space issue. i believe the spawn actor uses world space so i would try getting the component location in world space and setting the location that way. or in this case you are getting transform instead of just the location. aside from that issue everything else looks pretty good from what i can tell.