Saturday, December 20, 2014

Final Post Fall 2014

Ugh... Okay, sorry for not posting for most of the semester.  I just didn't feel like I was making any real progress with this once I ran into the problem of getting the mirrors to stop reflecting when the "light" was no longer shining on them.  It's still a problem I'm nowhere close to solving, but I've got to report something if I want a good grade.  Well, let's see what we have here...

Huh.  Looking back at my blog, I could've sworn I was further along before I stopped blogging.  Well, I suppose that's what I get for not keeping up.  I already took screenshots, and I'm not going back to take more.  I just hope these will suffice.

The push-block Blueprints haven't really changed, so I'm not showing them off.  Here's the start of the reflection code on the pushable mirrors.

This was originally designed to accept light from any one of four directions and reflect it in a ninety-degree angle counter-clockwise.  I figured this could be done with one Blueprint with the right model, or two copies of the same one with the models pointed in opposite directions, but I was pretty sure just rotating the blueprint was out of the question.  I still had issues with that approach, but I figured all I needed was one Blueprint that could be duplicated four times with only minor changes to the Nodes.  That way, I only had to send one with instructions on how to rig it up properly.  No need to mess with the settings, just connect and disconnect those four event nodes as necessary.  You only needed one.  Actually, having even two connected at once caused issues that I didn't have time to resolve any other way.
A closer view of the Blueprint.  Each mirror has four box components just for sensing the "light."  As you can see, one of the first things it does is throw a boolean switch so it knows which way to reflect.
Then it checks to make sure the sensor-boxes are being overlapped by a source of light, like the original shaft or another mirror.  When that happens, the boolean "isReflecting" is set to True.  I've had issues with commands being executed too many times, and this prevented that from happening.  It also serves other uses, too, but I still need it to resolve that issue.
This is where the main body of the reflection Blueprint starts.  I've tried using loops to get the Blueprint to constantly update itself, but Unreal would either freeze or return an "Infinite Loop" error and quit the game.  Event Tick occurs every time the game state updates, and here I use a Branch node and isReflecting to make sure it only does stuff when I want it to.  The first thing it does is destroy the reflection it hasn't made yet, because if it's at the end of the line you won't get to see it.  Then it determines its current location so it knows where reflection starts.
Then it remembers which way the light is supposed to go, and sets the coordinates for the end of the Line Trace I'm about to call.  Those integers are supposed to help the reflection point the right way, also, but it looks like I didn't need those nodes after all.

Here's the Line Trace.  It's purpose is to detect the first object the reflection hits so it doesn't go through it.

Then it calculates how far away that is, and converts said distance into a transform representing how long the reflection is going to be.  The calculations are still the same regardless of direction, but it needs to know whether the calculations are for the X axis or the Y axis.  It's twice the number of nodes you'd need otherwise, but I don't know an easier way to change axes.
Then, finally, it uses the transform data to create (or recreate) the two components of the reflection.  One is the Godray static mesh I snaked from the Blueprint Office demo in the Marketplace, the other is a box component.  Part of the complexity of the calculations involves these components being different sizes.  Since the transforms are relative, I have to divide the distance by the length of the mesh or else the length of the mesh will be multiplied by the distance rather than set to it.  Another consideration is that their origin points are in different locations.  Godray's is on its side, while the box's is at its center.  Thus, the box's origin point has to be placed at the midpoint of the distance to the next object.  Once these components are (re)created, they are set into respective variables to be destroyed when Event Tick occurs again.
But we're not done yet!  This is supposed to be how the Blueprint knows its not being reflected on anymore.  It doesn't work, which may or may not be due to ComponentEndOverlap not being able to detect when something that overlaps with it has been destroyed.  I eventually tried this with a transform component node instead of destroy and add nodes, but still destroyed the reflection when End Overlap occurred.  It destroyed the wrong reflection, and I didn't have time to figure out how to fix it.
Close up of the End Blueprint.  As you can see, it destroys the reflection one last time and sets isReflecting to False.  The issue isReflecting solved was that the reflection would initialize more than once, creating more reflections than destroy component could remove.  Now the reflection only initializes once and continues updating after that.  Even with the updating part, isReflecting is necessary to prevent extra reflections from occurring.
You see that white Material Sphere?  That's the rotating mirror.  In case I didn't say this before, an object that is both pushable and rotatable will change direction if it rotates while moving, so it was necessary to make two different mirrors.  The white sphere is actually invisible in-game, but I need it so I can tell where it is while I'm working on it.  You'll see a different Material Sphere in-game, which is chrome like the movable ones.  You'll be able to tell the difference by the vertical shaft of light being cast on it.
Since the rotating mirror starts off already reflecting, it doesn't need to be told when to reflect after play begins.  It also doesn't move, so it only needs to check where it is once.  Thus, Begin Play is used to initialize the reflection.  Also note it creates a chrome Material Sphere.
When you hit it with a projectile (or, in this case, something not identified as a light source or a character), it uses a Switch on Int node to change direction.  As you can see, the first thing it does is update the integer that tells it where to face next.
You may recognize this as the reflection initialization Blueprint from the moveable mirror.  As you can see, I determined I really only needed one boolean for this, but I still hadn't gotten past the integer thing.  I originally used a Set Rotation node here, but I thought it was choosing directions at random so instead I'm destroying and recreating the mirror instead.  It has been suggested to me that the Rotation may have been relative rather than absolute, but I haven't had a chance to test that theory yet.
Pretty much the same reflection updating code as the other mirror.
This is pretty much the only difference.  It doesn't stop reflecting, so there's no end Blueprint.

So let's see this stuff in action.  It's a little hard to tell, but the player character is using the My Projectile from the FPS demo, which has been altered to destroy itself on impact instead of bouncing around. It could still trigger Event Hit while bouncing around, so I put a stop to it.
Am I done now?  If you'll excuse me, I'm going to go lay down until Christmas.