Results 1 to 2 of 2
- 05-16-2012, 05:10 PM #1Member
- Join Date
- May 2012
- Rep Power
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.
- 05-16-2012, 05:23 PM #2
- Join Date
- Sep 2008
- Voorschoten, the Netherlands
- Blog Entries
- Rep Power
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.
JosThe only person who got everything done by Friday was Robinson Crusoe.
- By tike in forum New To JavaReplies: 5Last Post: 12-08-2011, 06:30 PM
- By firstname.lastname@example.org in forum NetBeansReplies: 1Last Post: 01-06-2010, 02:50 PM
- By adammyth in forum Threads and SynchronizationReplies: 1Last Post: 01-02-2010, 08:58 PM
- By aarthi2learn in forum AWT / SwingReplies: 4Last Post: 12-22-2008, 06:46 AM
- By javanewbie83 in forum New To JavaReplies: 16Last Post: 07-16-2008, 05:27 AM