I’m trying to create some blueprints that can construct solar systems (for a 4X prototype). I’m a complete noob with blueprints and UE4, so I hope my approach is not completely wrong. Currently I have two blueprints that I’m working with:
The System blueprint contains the sun static mesh and the construction script that creates the planets. Based on some randomness and an average distance a fixed amount of planets is placed around the center of the System, i.e. the sun.
Now, I want the planets to orbit around this sun. And I’d like them to do this without the need of any additional input. They should know their own sun somehow and be able to calculate the axis of their rotation on their own. For that I need to somehow make the System blueprint available to all child planets (see picture).
How can I do that? Or is my approach wrong and there is a much easier way to achieve random numbers of orbiting, procedurally generated, randomly sized planets at average + a little random distances?
If you need to reference the System from within a Planet, you should create a variable of type Object ‘System’. Then you can either:
1.Set this variable as public. Then when a planet is created in construction script, set this variable to ‘self’(self at this point is the system itself)
Make it private. Then create a public function that accepts a parameter of type Object’System’. Within this function set the System variable to this param. After you create the planet from constructtion script, call this function with ‘self’ as input.
This is needed only if you want the planets to know their system for some future logic. But if all you want is to make them rotate around it, you can instead create two variables called ‘PivotActor’ and ‘radius’ inside the planet BP. Set the Pivot actor to the Actor around whcih it should rotate (in your case Sun) and radius as the orbit radius. Within the Event tick function, you can update the planets location using Vector Rotator operation. Find a unit vector from Sun to Your planet, rotate this vector with a rotator (whcih is interpolated). Then multiply the resultant vector with radius and set this as the new location for the planet.
Alright, that variable thing is something I’ve already tried and failed. The object that comes out of my AddChildActorComponent Planet_C node is of type ActorComponent and I don’t have the slightest clou on how to get a Planet object from that which I can tell the System.
I’d absolutely appreciate it if you could lead me this way based on the Blueprint you see in the picture. Thank you very much in advance!
Great. However, I think that I’ve made some progress. I’m not yet sure that it’s going to work but it looks promising. So, no need to put this on your top priority. I’ll appreciate further help and advice nevertheless. It’s always great to learn some cheats and undocumented stuff from the experience of others…
I’ve been trying this for a few days now and I’ve made it working. With one major problem: UE4 is very unstable with my solution. I’m frequently getting editor crashes when launching the simulation and after a restart of the editor everything works fine until I change something in the blueprint and have to recompile. Then again: save → launch simulation → crash → restart → works. This is really annoying and slowing me down.
I did some research on what might be causing the crashes and it seems that they are somehow related to accessing my Planet blueprint from my System blueprint after casting it accordingly. But I need this access to tell the planet which system it belongs to (I use a public variable in Planet that I set in System). It seems it’s got something to do with the order of compilation. But I couldn’t figure out yet what I can do to prevent this from happening.
All I can do up to now is avoid accessing Planet directly from System. But I don’t know how to tell Planet its System without casting the corresponding Actor and using the Planet object directly.
To me it looks like it’s a bug which is still unsolved in 4.4 preview (encountered it in the current 4.3 release as well as the latest 4.4 preview built from GitHub source on my own system).
Glad to hear that you’ve were able to figure it out yourself. About the ‘crash’, I have no idea. I am also trying to implement this in my own way. I might be able to complete it tomorrow. If I get it working properly, I will post it here.