Tonight was extremely frustrating.
I did two main things today. It wasn’t supposed to take as long as it did.
1. Move follower movement function into Controller class
Until now I was initiating follower movement in the actual follower entity. The code for each follower entity is being stored in its base file (so I have base1.js where both the base entity and its follower entity is defined). However, last night I noticed that I’m actually not hard coding any values into the movement code, but using attributes of the follower entity which I set earlier in that entity’s code. So really there’s no need to duplicate this movement code across every single base file/every single follower entity.
What I did instead was move this entire thing into a new function in the Controller class.
And that was fine - no problem. But the second thing…
2. Count how many total followers each level has
In an earlier post I talked about how I now assign
batch_size attribute to each base entity, which uses a minimum and maximum number to generate a random quantity of followers in each base as soon as the level loads.
Now I just needed to count them.
What I did was create a
total_people variable in the Controller and added the following code to the base init function, right after the batch size is calculated:
[crayon] ig.game.playerController.total_people = ig.game.playerController.total_people + this.batch_size; [/crayon]
I then printed both the
batch_size and the value of
total_people to the console.
That’s where I hit a problem (I mean, there were a couple of problems before, but they got resolved fairly quickly). The problem was that I was seeing the batch size calculation duplicated in the console. Somehow instead of initiating 2 bases in my sample level, 4 bases were being created.
After a bit of an investigation to make sure I wasn’t doing something weird in the base code I suspected what the problem might be - it wasn’t a matter of entities somehow spawning twice in the level. It was a matter of the level itself being loaded twice.
I went into my
main.js and thought I suspected the problem - I’m using a plugin called Director to easily switch between levels, but am still using the old level load at the same time. Hence the same level being created twice.
And then it hit me - crap. In my last/first project before Gwen I did the exact same thing. I just never had to count the number of spawned entities so never noticed. This means that in my last project I basically spawned everything twice. Do you know how long it took me to try to optimise that thing to run at a semi-decent frame rate? A long time. Do you know how many performance issues I ran into? Many. This was a major facepalm moment. At least now I know. I have half a mind to go back and test if performance will improve if I stop it from duplicating now - because it’s just a hunch, I really don’t know (as many of the images are actually being loaded outside the levels).
Anyway, I removed the old level setting function and just kept the one being called in Director. This then caused another problem - the IDs of all of my entities changed because now instead of having 20 entities spawn on level load I had 10. So I had to go back to my adjacency array in the Controller class to remap all of the adjacent nodes using the correct IDs before anything would run.
From there I ran into a whole bunch of other niggly problems that should not have taken as long as they did at all. In the end it took me hours to get these two things working, not helped by the fact that it’s 2:30-fricking-am, but at least now they’re working and at least now I’m not spawning two of everything. Gahh.
So now I have the total number of followers in each level being printed to the console when the level is loaded (before those followers are actually spawned). I made it so that this number decreases by
1 each time an entity reaches the destination, so I am now able to count how many followers are left in the level at any one time. This is all just being printed to the console for now, but soon I’ll start implementing a rudimentary HUD.