Following Traces
As we are building real applications here we have issues and errors. no cheat sheat awaits me to make it look like building games is magic work. Time to take advantage of our favorate friend the trace using it we will explore our issues in our game development and sort them out before they come back to bite... By the end of this video we will know how to find out when our application changes from save to unsafe in the exact moment it changes, we will optimize some of our code and follow our traces to validate that we aren't calling functions more times then we need them.
Video Transcript
00:00The next thing that we have to do is we need to know each time when we step out of a safe zone
00:04that we stepped out of that safe zone to be able to to then determine where is our beginning point
00:09and basically our plan right now is going to be as soon as we step out of the safe zone we want
00:14to store that dot between what two dots are we add between between what two dots and when we
00:20land we want to make sure that we know between what two dots so we could then create that path
00:23of creating the the path of least resistance. All right so the next step for us to actually
00:29do is actually figure out where are those points where do we actually know that we just stepped
00:34out into a safe zone so if we just roam around in our code so we know that changing directions
00:40we have an on enter and bam we have here is safe well is safe as already we're in that enter so
00:47if we take a step back we see that we have our when we change a direction which is basically
00:54anytime we click on our mouse when we change a direction a new point is being added in a trail
00:59is being moved now this change direction thing it happens when our keyboard gets pressed
01:05but what the only point that we're missing right now is we're missing the understanding of our
01:11where are when are we in a position where we just added we just stepped into an unsafe area
01:17now let's try to find it i can't really find it well is safe is probably a good place to
01:24ask well let's search for that is if function and is is safe basically tells us that we are safe
01:32and it removes it but we don't really have something that tells us we're in jeopardy
01:37do we i'm trying to find something here but i can't see one so we created the dot right
01:44we're listening to the keyboard that's fine as soon as we move a keyboard we're start
01:47starting to change a direction we change a direction then we start an enter frame
01:53when we start an enter frame then we're checking if we're safe then we remove that enter frame
01:58and we add the safe to bg now we already could see that we're calling this probably more than
02:02what we should right because we're calling out to save even when it is safe already
02:07and the second step is only if we're already not safe then we'll connect we're starting our trail
02:13so basically here we're really starting our trail if we're not safe but we have here nothing that
02:17defines the first first time when the user became unsafe to do that we're gonna have to add a little
02:23bit more logic which we don't really have here yet so to do that we're gonna have to in our
02:28enter probably this is a good place to start we want to check to see if we first of all we
02:33want to check to see if we are safe but we are heading into unsafe water that's a good spot to
02:41trigger we're in the unsafe area so to do that i'm gonna have to create another variable i'm
02:46gonna go up here just create a variable that will help us determine if we are in unsafe waters
02:53private var is isn't safe ah isn't safe sorry about that isn't safe it's gonna be a bullion
03:04usually i prefer not giving names of variables that are negative but in this case it seems like the
03:09most appropriate name so isn't safe now isn't safe we want to make sure that we only activate
03:16it when it's time to activate it so if we go back into our on enter the best thing that we
03:20have to ask is we have to find here a way for us to know that we've just flipped we just changed
03:25from being safe to being unsafe now to do that the best way for us to do that is first store
03:31our current safe mode before we actually change anything so to do that i could just create here
03:36a new variable um call it was safe bullion and we could just dump into it that is safe information
03:46so this is this happened before we actually changed our x and y positions as soon as we
03:50change our x and y positions then this safe value is going to bring back a new value which will be
03:55different now so we know right here that it could be or safe or not safe but if the change
04:01if the status of was safe is not the same as is safe what does that mean if if was safe
04:10is not equal is safe that means that something changed could be for good it could be for bad it
04:17could be that now we just flip from a safe mode to an unsafe mode or could be we just flip from
04:22a unsafe to a safe if we just flipped we could ask okay so if we just flipped and it's safe what
04:27do we want to do if we just flipped and it's safe we could probably take this if statement
04:32as it is intact what this if statement just does is it stops our enter frame it stops our
04:38element from moving and it adds the safe information to its place now if we're not safe that means we
04:44just flipped and if we just flipped we could say that um we could use that new variable that we
04:49created that isn't safe if i go back right down here and we could set our isn't safe to be true
04:56it's not safe anymore now whenever we're in is safe we could say that isn't safe is false all
05:05right so this way we we kind of know that exactly in this point our application flipped it flipped
05:10from being safe to being unsafe now how about the trail when do we want the trail to happen we
05:15don't care if it just flipped or if it's a flip that's going on already right so we probably want
05:22to keep it going on outside of our if statement so if change or not change doesn't really matter
05:27to us we probably just want to take here an if is not safe and then just if not safe let it
05:33roll let it create that line so no matter what if it's not safe it's going to create that line
05:39now what else is this enables us what else this enables us sorry this enables us to give a little
05:46bit more flexibility onto when things are when extra things that we need to do when things are
05:51not safe so we're going to see in a second where this is actually going to become useful
05:55but we just want to test this out for a second we're just going to put here a little trace here
05:59and this trace should really only happen once as soon as we step out out of the safe waters
06:09all right so if we run our application and that's not our application if we run our application
06:14and we just let it roll and probably should fix this not wasn't isn't safe and as soon as we
06:23step out it should say that we change and that's it it's not going to do it anymore and that's
06:29great already we know that we're in a good place because at this point right now we actually
06:34wanted to start collecting points we want to start collecting the new dots that are being created
06:40well things that are happening here we don't really care about them so now we know when it's
06:45when it isn't safe and when it is safe so we could probably delete all this goodness we don't
06:50need that trace anymore now now we could use this variable isn't safe for our advantage so
06:56for example in our on change key change directory our new point we have our creating a new enter frame
07:05when it changed directory now each time we change a direction sorry not directory we change a
07:12direction do we really need to start a new enter frame not necessarily right because we might be
07:18in the middle of a flow but it didn't really matter to us too much because adding another
07:23enter frame for an enter frame that existed just creates another it keeps it in that same event is
07:29still running so we didn't really care too much about it but our next step here because we want
07:34to get more efficient to be able to also control our basically we want to control our flow a little
07:41bit better all right so so far so good now the next step for me is actually what i need to do
07:51is i need to start saving those points and we're going to see we're going to go back to this
07:54isn't safe when we figure out that we actually need it and right now where we really need to
07:59is actually when we start pushing these path points because right now we've been pushing
08:05path points no matter what whenever we change a direction we've been pushing when it whenever
08:10it was safe whenever it wasn't safe it didn't really matter now what we really need to do is
08:14we want to make sure that we're only adding corners we're only adding to the path whenever
08:19we're in a corner and let me show you why that this is important if i put here a little trace here
08:25added corner and i run my application and i let i let it run i see see already at two were added
08:35and every time i press on my arrow it'll add another ready even though we didn't change the
08:39direction so we need to be a little bit smarter here we first of all this will be easier to
08:44to solve because because we could add here a little bit of extra logic here we could say
08:49if our direction didn't change then just don't do anything so we want to do is basically push back
08:56so if our directory x already equals the new direction and our old directory y are
09:09is exactly the same thing as our new direction that we want to go into if that's the case and
09:14we don't really want to do anything so a good way to do that we could just put here our return
09:20and just push out but a lot of times what developers like doing is you could grab all
09:24this if statement because we want exactly the opposite saying this as the opposite is probably
09:29more complicated it's easier to just say take all that and turn it into a negative what that means
09:34is if it's not that if it's not both of them the same then we could open a curly bracket and
09:41just make sure that we do everything that's inside here if not just ignore the change
09:44because nothing really changed and if we try this one more time we're going to see that
09:50when we go out we got the two anyways but now if i press them and how many times i press
09:55it doesn't add it another time now we still need to figure out why is it adding the corner more
09:59than once now first of all it's adding it first with the zero zero the second time is when we
10:06actually change a direction so so far we now we're a little bit cleaner and it's really
10:11important for us to clean our corners to make sure that we have the right amount of corner so
10:15it doesn't get messy later on but we still need to figure out when are we actually changing a
10:21direction which is really happening in here and we just want to make sure that this is only called
10:26when it's really needed so let's just run through this change directory and try to find where it's
10:32being called so it's being called an on key change and it's only calling and it's only called
10:37an on key change so what i want to do is i want to have your little trace here to be able to see what
10:43values are being sent inside to be able to see why is it being called more than once okay so what
10:50i'm going to do is actually added corner and we could just put here the new we could just put
10:54here the new value so we could see to what did it change and if it came in twice with the same
10:59value all right so we're ready we're going to run it one more time and we can see that in
11:04the beginning it was zero minus one and the second time it was zero minus one and somehow
11:09it's still passing our our radar now the reason is probably because somewhere else we're changing
11:17our directory and if we probably should look for that out because it's only only viable explanation
11:23is somewhere else we're putting here a value that's conflicting with the values that we're
11:26setting here and here we go so when we add something to the safe to bg then we're clearing
11:31it so it seems like that we're calling add to save to bg more than we should and if we put
11:37here a trace we'll see if that's true so i'll put here a little trace and really this should
11:44only be called whenever we complete around not when we're starting around and if i press this
11:49we'll see that added corner called and we see that called happens even though we're still in
11:53the safe zone so we definitely want to fix that before we even start building the more complicated
11:58stuff so we're back into the on our on entry frame because this is the only place where add to be
12:05add safe to bg happens now add safe to bg happens now the the big question here is when do we want
12:14to add safe to bg when do we want to remove that the enter frame when safe happens but it was
12:23it isn't safe just a second ago because if it was always safe then there's no reason to change
12:32anything right well we could we could always remove the enter frame but do we really want to
12:39maybe not so but i think we do but what we want to do is probably we could add here another
12:44extra if here asking if it isn't safe which is that means it was now we're talking double negatives
12:53it wasn't safe a second ago just before it flipped if it wasn't safe a second ago only then
13:01add that bg all right so let's try this one more time we'll see are we still getting that extra
13:06call so first of all we're adding a corner that's fine let me and one one point is that we're
13:13we're having here is that oh here we go so first of all if i take one step forward let me clear that
13:23run it one more time so when i press up here once i moved one point i press up again it seems
13:30that we're stuck i press up again it seems that we're stuck we're not really moving only if i
13:35somehow if i press really hard then we definitely didn't solve it so we still have to funnel
13:41through this and see why isn't this working and i'm assuming that this isn't working because
13:46we're not resetting our values somehow somewhere in a in a way that we need to because we're not
13:50calling this ad safe to bg if i as soon as i remove it then everything should be working again
13:55so if i that that indicates to me that whenever i'm not calling this i'm i'm messing something up
14:02now if we're looking at the change directory the change directory is supposed to we just follow
14:07it just once so let's say we're we're this is basically the direction we're going in the
14:11beginning because we're going up so in the beginning we're sending zero and minus one
14:16and zero zero and minus one is not what's inside because right now there was zero and zero
14:20then it sets that new value then it adds that point then it moves to that point and then it
14:31starts an enter frame so far so good then it moves right it does that one point it's still safe so it
14:39removes the enter frame it doesn't add a safe bg and then we're back up here again to go for one
14:44more round now this value is not true anymore that means that this new line that we added that
14:51we're protecting our change here is actually preventing us from actually following through
14:58so you see that was our issue now we still need to figure out a way because we still need this code
15:02but we probably need this code to add into another another something so first of all if our
15:07line didn't change and what else and we aren't safe so now we could use that as in safe so
15:15basically we're saying if nothing changed and it's not safe then don't do anything
15:23all the other times then do something and if i just put here another end here and it isn't safe
15:30basically what i'm saying is this is a little bit hard to comprehend so first of all our dot didn't
15:35change if our dot didn't change and it isn't safe then well because we have here or not
15:45that's where it gets confusing first of all imagine that it's all positive so if our dot
15:49changed or more actually our dot didn't change we're in an unsafe scenario if our dot didn't
15:55change we're in unsafe so now we really don't want to do this so we're not going to do it
15:59okay it's even though it's easier to understand it if we would put here different and different
16:03and different it makes it easier to write it down this way because we're excluding every
16:08we're basically writing exactly what we don't want we because we know we knew that we didn't
16:13want this to happen when the dot didn't change unfortunately we didn't notice that
16:17we actually wanted to happen if our dot didn't change but we're not in the unsafe and the
16:22reason was is because only in the unsafe the enter frame continues and doesn't stop
16:27so if we test this one more time we should be getting that our dot will continue working
16:31and we'll be able to complete a full cycle so we're already uh much more ready now for our next
16:39phase where our next phase is actually going to be starting to add those corners
