This site requires JavaScript, please enable it in your browser!
Greenfoot back
RUMMAKER
RUMMAKER wrote ...

2013/12/21

World not loading due to memory issues??

RUMMAKER RUMMAKER

2013/12/21

#
I'm making my world randomly generate stuff for a game I'm making but the problem is the world will not load after pressing reset or compiling it again. I know it is capable of loading the world because i have usually successfully loaded it on the first try but I cant load It again after that? I don't know if its because I'm using too much memory or some other reason so I'm asking for help on spotting the problem because I'm not even sure what the problem is D: problem is most likely in the constructor because loading the world is my problem and when the world loads successfully i have no problems with it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
public class Field extends World
{
    public Point[][] grid;
    public int[][] gridState;
    private Mover m;
    private final int WATER = 2;
    private final int OCCUPIED = 1;
    private final int EMPTY = 0;
    public Field()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 800, 1);
        m = new Mover(this);
        grid = new Point[50][40];//used for pathing, objects, and etc(maybe fog of war as well)
        gridState = new int[50][40];//is the square ocupied? what occupies it
        for(int x = 0; x < 50; x++)
        {
            for(int y = 0; y < 40; y ++)
            {
                grid[x][y] = new Point(x*20 + 10, y*20 + 10);
            }
        }
        spawnRiver();
        spawnTrees(16);
    }
    public void act()
    {
        m.move();
    }
    public void spawnRiver()
    {
        int x = Greenfoot.getRandomNumber(10)+20;
        int y = Greenfoot.getRandomNumber(10)+15;
        int x2 = 0;
        int y2 = 0;
        while(gridState[x][y] != EMPTY)
        {
            x = Greenfoot.getRandomNumber(10)+20;
            y = Greenfoot.getRandomNumber(10)+15;
        }
        Point newP = new Point(x, y);
        if(Greenfoot.getRandomNumber(10) < 5){x2 = x - 1;}
        else{x2 = x + 1;}
        if(Greenfoot.getRandomNumber(10) < 5){y2 = y - 1;}
        else{y2 = y + 1;}
        Point newP2 = new Point(x2, y2);
        spawnRiverHelper(x2, y2, newP, newP2);
        spawnRiverHelper(x, y, newP2, newP);
    }
    public void spawnRiverHelper(int x, int y, Point origin, Point secondary)
    {  
        if(x == 0 || x == 49 || y == 0 || y == 39)//at one of the edge piece
        {
            spawnWater(x, y);
        }
        else
        {
            spawnWater(x, y);
            int newX = 0;
            int newY = 0;
            if(Greenfoot.getRandomNumber(10) < 5){newX = x - 1;}
            else{newX = x + 1;}
            if(Greenfoot.getRandomNumber(10) < 5){newY = y - 1;}
            else{newY = y + 1;}
             
            Point newP = new Point(newX, newY);
            while(newP.nextTo(origin))
            {
                if(Greenfoot.getRandomNumber(10) < 5){newX = x - 1;}
                else{newX = x + 1;}
                if(Greenfoot.getRandomNumber(10) < 5){newY = y - 1;}
                else{newY = y + 1;}
                newP = new Point(newX, newY);
            }
            spawnRiverHelper(newX, newY, secondary, newP);
        }
    }
    public void spawnWater(int x, int y)//grid's x and y!! not world's x and y
    {
        Point p = grid[x][y];
        Water w = new Water();
        addObject(w, p.x, p.y);
        gridState[x][y] = WATER;
    }
    public void spawnTrees(int amount)
    {
        for(int n = 0; n < amount; n ++)//planting trees
        {
            Tree t = new Tree();
            int x = Greenfoot.getRandomNumber(50);
            int y = Greenfoot.getRandomNumber(40);
            while(gridState[x][y] != EMPTY)
            {
                x = Greenfoot.getRandomNumber(50);
                y = Greenfoot.getRandomNumber(40);
            }
            Point p = grid[x][y];
            addObject(t, p.x, p.y);
            gridState[x][y] = OCCUPIED;
        }
    }
}
oh yeah heres the source in case u guys need it http://www.greenfoot.org/scenarios/10478
Super_Hippo Super_Hippo

2013/12/21

#
Line 36. This while loop repeats forever I think and does nothing else than creating random numbers that are not used.
RUMMAKER RUMMAKER

2013/12/21

#
that loop's purpose is to find a square on the world that is not occupied by anything. So its needed and it does not go on forever because i only called the spawn river method when the world is empty.
Super_Hippo Super_Hippo

2013/12/21

#
Maybe you should not set 2000 references in one time (line 20). (By the way, some points are outside the world, one right und down, because of the +10). Line 67, another while loop. I am not sure how you defined this "nextTo()", but it seems like it is always next to the origin because you place the next part of the river next to the last. Actually always diagonally. If you look at this scenario, where I created a pattern (Link), it does not create the world at once. This probably would not work either. You could try to split up the preparing of the world to see if this is the problem.
Super_Hippo Super_Hippo

2013/12/21

#
When I open your scenario in Greenfoot, then it sometimes work and sometimes it does not. I changed line 03 and 04 to "private ...", but not sure, if this is the solution.
You need to login to post a reply.