RuneScape 2 Development > Server Help

[Runesource] For loop in a GameObject array.

Pages: (1/1)

sorecheeny:

Hello, I might be really stupid or something, and it might be a super easy fix, but I'm really not sure how to fix this.

So I'm trying to make the Party Room for my 2006 remake, and I've got it all done, the chest and everything, but now I'm trying to spawn in the party balloons properly, and pop them properly as well.

I'm currently dropping the balloons in this method:


--- Code: Java ---    private static void dropBalloons() {        dropItems = false;        for (int x = 2730; x < 2745; x++) {            for (int y = 3462; y < 3468; y++) {                for (int i = 0; i < balloons.length; i++) {                    if (balloons[i] == null) {                        int num = generateRandomNum(7);                        balloons[i] = new GameObject(balloonForInt(num), x, y, 0, generateRandomNum(3) + 1, 10, -1, 999999, false);                        ObjectHandler.getInstance().addObject(balloons[i], false);                    }                }            }        }    }
I do think that it looks really bad, but let me explain the code:

I'm spawning the GameObject using this constructor in its class:


--- Code: Java ---    public GameObject(int ID, int X, int Y, int Height, int Face, int Type, int NewId, int Tick, boolean c) {        GameObject p = ObjectHandler.getInstance().getObject(X, Y, Height);        if (p != null && ID == p.def.getId()) {            return;        }        def = new GameObjectDef(ID, Type, Face, new Position(X, Y, Height));        faceOriginal = def.getFace();        newId = NewId;        tick = Tick;        clip = c;        ObjectHandler.getInstance().addObject(this, clip);    }
And as I'm looping through the GameObject array that I declared as:


--- Code: Java ---    private static GameObject[] balloons = new GameObject[90];
I'm checking to see if the GameObject item in the array is null, and if it is, I'll make the one selected as i from the for loop a new GameObject, with the x and y as the position, a random face from 1-4, type 10, and the newId as -1, since I'll be handling popping the balloon in a different way than just updating it and transforming it into it's newId.

But, the problem I get when I try to run it is this:


--- Code: Java ---java.lang.NullPointerException        at com.sc2006.model.players.ObjectHandler.addObject(ObjectHandler.java:487)        at com.sc2006.model.content.activities.partyroom.PartyRoom.dropBalloons(PartyRoom.java:179)        at com.sc2006.model.content.activities.partyroom.PartyRoom.process(PartyRoom.java:76)        at com.sc2006.model.World.process(World.java:66)        at com.sc2006.Server.cycle(Server.java:359)        at com.sc2006.Server.run(Server.java:262)        at java.lang.Thread.run(Thread.java:745)
It's calling a NullPointerException, saying that the balloons that I already set to be a new GameObject, is null, and it can't add it because it gets an error at the if statement in the method:


--- Code: Java ---    public void addObject(GameObject o, boolean clip) {        if (getObject(o.getDef().getPosition().getX(), o.getDef().getPosition().getY(), o.getDef().getPosition().getZ()) == null) {            objects.add(o);            placeObject(o, clip);        }    }And with this, I only get one balloon to spawn, which is at the first x and y coordinate that is being called in the for loops.

What I think is that it's not going up because it's getting some sort of error while creating the GameObject, and then it just stops it? I'm not too sure.

If anyone could help me with this, it'd be greatly appreciated, thanks.

sk8rdude461:


--- Code: Java ---if(balloons[i] != null)     ObjectHandler.getInstance().addObject(balloons[i], false);
Well that's the easiest fix I can think of.

But I wouldn't handle a party room like this.

What I would do is create an array for the balloons and then randomly fill the slots in the array (Because sometimes balloons wont fall on a tile), then drop the balloons. After the last balloon drops (I'd assume you're using some form of an event container to control the balloons falling) I would repopulate the array using the same method. Until all X balloons (I'm not exactly sure how many would need to be dropped in total...) are dropped.

sorecheeny:

Oo, that's actually a great idea. Thanks a lot.

Pages: (1/1)

Go to full version