Object-Parameter passing costs
Hello everyone, first post on these forums. I've been using Java on and off for the past few years but still have much to learn.
Right now I'm trying to write a simple 3d tile-based game in LWJGL, sort of like Minecraft in a few aspects, but not that many. Really, it's more like the old Tomb Raider 1-5 games where all the levels are built off of regular-sized grid platforms.
Anyway, I'm trying to figure out some of the game logic in order to improve performance, and I need to know a little something about passing by parameter/referance or whatnot.
Currently, I have a "World" object that is in turn made out of a grid of grids. It is created once upon entering the program and drawn every frame using openGL calls. I also have "Monster" objects that move around on in the World. Each frame refresh, I have the game check what tile the monster is above (in order to see if it should fall or not, or whether the next block is too high up for it to climb.) Currently, this is done in my main loop, ie:
1) The main loop looks at the World object and gives the Monster object the tile it is on with its relevant height data.
2) The Monster object determines the next location the monster will be if it can make a successful move, giving that new data to the main loop.
3) The main loop looks at the World object again and gives the Monster the information on the next tile.
4) The Monster checks if the next position is valid based on the new tile data. If it is valid, the monster moves. Otherwise, it stops and does nothing for that refresh.
This is a bit messy, though. My monsters are stored in an ArrayList, so this means a lot of passing tiles back and forth between the main loop and each Monster object. What I'd prefer to do is just give each Monster a pointer to the world so it can do its current/next tile checks without needing to jump between classes.
So, if I initialize each Monster like this:
Monster monster = new Monster(World world)
and I have, say, 175 monsters running around at a single time, how much of a performance hit is this? Does Java need to allocate room in memory for 175 new World objects in RAM (which would be far too costly)? Or would Java simply stick pointers to my World object and incur little to no performance penalty?
Thanks for your help! I know this goes back to parameter passing, which is something I've never fully understood in Java.
Re: Object-Parameter passing costs
A reference is a sort of pointer to the real thing. No matter the size of the real thing, references all have the same size (typically 32 or 64 bits). All you do is pass references around; only the 'new' operator creates a new object (or 'new' in disguise, such as the clone() method). Don't worry too much about passing parameters; it is done quite efficient in Java.