Level stuff (just something I was wondering)

So, for collision I’m passing an arraylist for each thing (creatures, blocks, items), and every time I add a new type of “thing” in the level, I have to add another thing in the constructor for each creature/item/block or whatever it may be.

Since all of these things would be within a given “level”, would it make sense to just pass the instance of the level in the constructor of each object created? That way I can always just get the given arraylists in each level? I know I “could” do this, but I didn’t know if this is really bad regarding memory usage, lag, or something like that. I also didn’t know if this would cause huge delays, and make my game act weird.

Everything works fine the way I’m doing it, but I just noticed that when I want a creature to drop an item on death, I need to add that item to the item arraylist in the level. So I would need to pass that arraylist though the creature… which would create this whole situation. In the end, is it bad to give every object a shortcut (or whatever you wanna call it) of the level?

I’ve seen you post a lot of these questions lately. Honestly, the answer is up to you. Have you tested out your theory? If so, what were the results? A lot of programming is trial and error, and then figuring out what to do next. You easily figure this one out yourself just by implementing your solution and seeing what is working and what isn’t for you and your game.

Let me give you a hint, though. No, it won’t make your game “lag”. Just try it out and see what happens, all of us aren’t going to be here every time you run into an issue so it’s best to learn how to do it all on your own, or at least most of it :slight_smile:

When passing the Level object to each entity, it’s not like you’re copying the entire object and placing it in the entity. It merely makes a pointer to that Level object.Everything in Java is pass-by-value. Nothing will lag or cause memory leaks.

The code sample below actually copies the passed-in object in for the argument, hence why you can’t modify the original by overwriting it. However you can change fields in objects or call methods and still have an effect. This site has an excellent clear example of this.


public void reDog(Dog d){
   d = new Dog("fido");
}

Dog dog = new Dog("poofy");
reDog(dog);

// the dog object will still be poofy, and not fido
// if reDog actually SWAPPED the dogs, then that would be pass-by-reference (changing the reference)

I’m sure someone else can give you a more detailed answer, but you should be fine just passing a reference to the level upon creation of an object that would need access to these lists. You can also do it the other way. Depends on your programming style. After all, they’re only references to the existing object.

EDIT: See, two posts before I could even finish my own. xD

I understand passing by reference, I just didn’t know if having a lot of pointers or references caused lag. I noticed delay in some things in my game, but didn’t know exactly what caused it. So I was avoiding just passing a level, but if you all say that’s not the issue, then I’m fine. I know there’s different ways, and I just didn’t want to redo everything in my game… just to find out it doesn’t work well.

I’ll do it though, sorry for asking again xD.

Yeah don’t worry look at this example i just made up:

These are memory “slots” (A, B, C, and D are objects in memory):
AAAABBBBCCCCCCCCDDDD
1234 56789

9 being the start of C

Let’s say that C represents the Level object in your memory.

When you do:
public void doMethod(Level lvl) {
lvl.doSomething();
}

You’re passing in the address “9” to the computer and the computer is using what is already in memory slot C.
So therefore you don’t use things that you don’t need and you don’t create things that you don’t need.