Posts Tagged ‘global variables’

ARRAYCEPTION

ARRAYCEPTIONUpdate – Feb 23, 2012, 12:50pm

In my previous post I talked about how I know that I should be sticking away from global arrays…in this post I’m going to talk about how I now have a global array of global arrays. Yes, I am facepalming right now.

I mean, it’s working. It’s doing what I need it to do (as far as what I’ve built so far – it actually needs to do much, much more), but it feels extremely messy. I have no doubt that I’m probably making this more convoluted than it has to be.

So here’s what I have:

  • - Each ‘base’ entity in the game creates its own path array when clicked.
  • - There is a controller class that holds an (empty by default) base array when the game is launched. This is a global array.

 

 

The rules

  • The player selects a base by clicking on it. There will be multiple base entities with different attributes (so base1.js, base2.js, except with more descriptive names), and sometimes multiple instances of the same bases in each level
  • They then draw a path by clicking on a series of nodes from the base to a set destination
  • When the path is complete, they can click “Go” to spawn entities from the base and have them move along the path to the destination

If the player wants to delete part of a path
They have to click on the nodes in backwards order (so first in last out) to deselect the parts they want to change.

If the player wants to delete the entire path
They can click on their selected base, which both deselects the base, removes it from the base array, and clears its entire path array.

How I’m currently doing it

Clicking on the base

When the player clicks on a base entity, the empty path array gets created in that base entity. At the same time, that base is pushed to the base array, which is a global array in the controller class. This is done like this:

Selecting the path

When a base is selected, the player can draw a path from that base along existing nodes in node.js:

The player can then select the destination in the same way as above, except the destination node is its own entity (in destination.js) and there is only one instance of it on the screen.

Moving along the path

When the player has a complete path that starts from a base entity and finishes at the destination entity in the path array, they can click Go in Gobutton.js:

The first item (the base element) is then shifted off of the path array, followed by the entities being spawned moving through the remaining nodes.

Array naming and stuff…

The problem here is that I’m doing this dodgy roundabout way of defining the path array. In the base.js init function:

So I’m kind of naming it like I would define a global array, and then using eval to actually let myself refer to it from other entities…

So I’m calling this thing ‘arrayname’ in base.js, but in other entities I then have to use arrayname along with the global base array to set a new name for it:

I can then push(), pop(), etc as required by referring to the array as this.path_array. However, this means I now have two different names for the same array in different places – in base.js where the array is set it’s “arrayname”. Everywhere else it turns into “path_array”. This also means I have to define path_array from scratch each time I want to use it in a new entity. It works in the sense that it’s functional, but it seems all kinds of screwed up.

In addition

In addition, James and Simon suggested some different ways to move the entities from node to node when Go is clicked, which I’m also itching to try out. I’ve been holding off for now as I want to get the rest of this path follow stuff done and the range checking I’m doing now is working for the time being, but it’s something I have to remember to go back to and try.


Update:

Feb 23, 2012 12:50pm

I modified this somewhat at lunch. I am now doing this in the Controller class:

And then simply referring to ig.game.path_array whenever I need to use the path array. I think originally I had this screwed up attempt to not use a global array for this, but it ended up being a roundabout way to refer to a global array anyway…this is just easier. I’m hoping I’ll still find a way around having to do this in the future, but now I can just refer to ig.game.base_array and ig.game.path_array when needed.

I’ve also created a new function in node.js to clear all nodes when the base is deselected. I’m having some weird problems with the animation not changing over for this, but otherwise it’s working. Will have a proper look at the animation problem tonight.

Git branching, naming conventions, global variables, and Mr. Carboon…

First, I’m very aware that the topic of this blog has kind of shifted a bit over the last few posts. It went from posts about games, the messages within games, and game-related issues to posts about JavaScript and the more technical aspects of learning how to make games. I’m not sure if this is going to alienate some of you guys, but as this is a personal blog…well…I’ll write what I want to write. However, I have now separated game related posts into different categories (as I’ve noticed from the traffic stats that quite a few people filter by the ‘gaming’ category specifically when reading this blog):

  1. games – All game related posts of all kinds will sit in this category
  2. gaming – Posts about existing games/game reviews/critiques of games/etc. Also more general game design-y musings and theories
  3. game dev – Posts like this one, that go more into the technical aspect of learning how to make games and specific projects

On to what I actually wanted to talk about:

Git branching

I’ve been thinking about Git branching strategies. As Gwen is a small one-person project I probably don’t really need to worry about things like this too much, but I’m treating it as a small-scale test that I can learn from for future scenarios as well, so in that sense considering things like this can be important. I found this article very interesting, but so far I just have two branches – master and development.

Naming conventions

I’ve also been looking into JavaScript naming conventions and decided to use Douglas Crockford’s guidelines. Tonight I’ll be going through what I have so far and renaming where required.

Avoiding global variables

One thing that I’ve been hearing and reading a lot is how bad global variables are due to potential clashes in the code, since they can be accessed by anything at any time. This is a problem for me because so far I don’t yet know of another way (much less a way that’s as simple as just declaring a global variable) to refer to my arrays.

Data flow diagrams

I remember learning about entity relationship diagrams and data flow diagrams in my high school Information Systems class (thanks, Mr. Carboon!) in years 11 an 12. At the time I didn’t really consider that I may actually have to ever use this knowledge, but the more I try to get the structure of this thing straight in my head the more I realize that I need to find a clear way of documenting what various entities actually do and how they respond when the player interacts with them.

This made me really appreciate my Info Sys teacher for forcing us to learn this stuff and drilling it into our heads. It was a long time ago and I have to do some major brushing up, but at least I still vaguely remember some of this stuff. I wonder if Mr. Carboon will ever get to see something I made and think “Yup, I contributed to that.”

Scope

Even though the complexity of building this thing has been ramping up as I get a handle on what I have to do, the idea for this game has stayed the same in terms of gameplay. I’m very wary of not letting this turn into some big complicated overambitious “thing” and don’t think that this will be a problem. I have an idea, I’m in the process of documenting it, and I know where it has to end up. As a player, the gameplay will be fairly simple – really centering around just one kind of interaction with different objects. As the person making this, the actual development process is what’s getting tough. I do feel like I’m heading in the right direction, though, and progress is being made on a daily basis (and most importantly I’m learning new things on a daily basis).