PDA

View Full Version : [AS2] Memory for Random Level Generation



AlphavilleNY
July 25th, 2010, 03:56 PM
Note-- I originally had this in the AS2 forum, but hadn't noticed this category until now. Sorry for the confusion, I'm new here...

I've been banging my head against the wall for about a month now trying to figure out how to work out what I thought would be a very simple idea. I'm making a game (in Flash 8, AS2) where I want to procedurally generate the world with a mix of memory and randomly generated levels. I can get the random-generation part fine, but the memory part continues to elude me. I've used arrays and shared objects to come create a memory system for it, and I'm sure the answer is something rather simple in one of those but... well, enough rambling.

My random level generation is pretty simple. I have a set of maps I've designed and coded to be randomly selected based on the player's position as they exit/enter the frame (don't want the player to call up a map that puts them inside a wall). The map is selected based on a number variable I call "gameLevel". At the same time, I'm able to get Flash to count which room the player is in with a variable called "roomNumber"-- it starts at Zero, and then is added to or subtracted from depending upon the direction the player goes in (I'm only dealing with the horizontal right now).

Basically, what I want to do is this-- whenever a new "roomNumber" is generated, I want Flash to remember what "gameLevel" was randomly generated, and save it. The next time that the player enters that "roomNumber", the game should load its stored "gameLevel" instead of creating a new one. Random "gameLevel" generation should only happen when a new stage is needed (ie when the current "roomNumber" is either smaller or larger than any of the previous ones).

Like I said, this is probably exceedingly simple, but if the answer's staring me in the face, then I just can't recognize it.

therobot
July 26th, 2010, 09:20 AM
Right, it sounds like you just want to store the room numbers in an array.



// create an array to store the players path from random room to random room
var roomSequence:Array = new Array();


// .. then, later:
// player just got to a new room
roomNumber = whatever;

// tack the new room number onto the end of our array.
roomSequence.push(roomNumber);


Is that what you're looking for?

AlphavilleNY
July 26th, 2010, 12:19 PM
Right, it sounds like you just want to store the room numbers in an array.



// create an array to store the players path from random room to random room
var roomSequence:Array = new Array();


// .. then, later:
// player just got to a new room
roomNumber = whatever;

// tack the new room number onto the end of our array.
roomSequence.push(roomNumber);


Is that what you're looking for?

This looks a lot like what I was trying before, but I'll check this out. Like I said, I tried both Arrays and Shared Objects before, but I'm almost certain I was getting them wrong. Hopefully it's a simple fix, like this. Even if it isn't the case, much thanks.

Gathan
July 26th, 2010, 05:01 PM
This looks a lot like what I was trying before, but I'll check this out. Like I said, I tried both Arrays and Shared Objects before, but I'm almost certain I was getting them wrong. Hopefully it's a simple fix, like this. Even if it isn't the case, much thanks.
So you want something like


class gamelevel {
var roomNumber:Number;
// contains the level that was randomly generated for this one
var data:Array;
}
var levels = shared.getlocal('gamelevels');
for (var i in levels.data) {
rooms.push(levels.data[i]);
}

then at first run it would go through the shared object and take all of the existing levels and load them into an array


var levels = shared.getlocal('gamelevels');
for (var i in levels.data) {
rooms.push(levels.data[i]);
}

you would also want a method to save new levels that were made to the local harddrive


var levels = shared.getlocal('gamelevels');
levels.data[level.roomNumber] = level.data;

a method to see if the game has already made a level would also be helpful, this could be avoided depending on existing design though


function containsExistingRoom(roomNumber) {
for (var i = 0; i<rooms.length; i++) {
if (roomNumber == rooms[i].roomNumber) return true;
}
return false;
}

Thats it for the pseudo code, maybe it can give you some ideas on how to fix your problems and I wrote it in like thirty seconds so please don't yell at me haha.

AlphavilleNY
July 27th, 2010, 01:43 AM
Hmm... So far neither of these are working... But I've only just started to work with them. I'll keep tinkering for a while and see if I can get one of them square. I will say that the shared objects thing starting out as an external class is a little beyond me, but I'm assuming it can be rigged to work within the code itself.

AlphavilleNY
July 27th, 2010, 04:21 PM
So far the Array approach has helped me a little bit, at least as far as accumulating a list of the various rooms the player enters. What I'm trying to do now is just match up the room's "gameLevel" with its respective "roomNumber". I was thinking at first that the code should be as simple as this...




roomSequence.push(gameLevel[roomNumber]);

...Is this possible, to simply get the randomly generated "gameLevel" to be counted along the confines of the "roomNumber" var? It looks like the only way to load the information into the game after would have to be a shared object, anyway, but the array looks so simple, in theory anyway, that I'm tempted to believe that's all you really need.

AlphavilleNY
September 6th, 2010, 03:39 PM
Okay, I posted this bit in another thread, but no bites yet. I'm reposting here in my original thread just in case there's some recognition, as I have an idea that I think might work, if anyone knows how.


What I've been doing all this time is destroying an old level map whenever I create a new one. What I'd like to do instead is merely move the old map into the opposite direction the player just went out through. I've gotten part of it working so far with this line of code, for example, when the player exits via the left side of the screen:

this._x += Stage.width;

Simple enough. Problem is, when I do this it obviously moves everything away, including the player and the new level that's just been generated. I'd like to simply move the old room, but I've been unable to make it work by simply substituting the name for my tile-map array in for "this". Somehow I need to give each map a declarable identity to move as a whole in the code, or possibly make each new map an addition to the level which is moved around the player.

I suppose what I'm trying to come up with is cellular room scrolling, or something to that effect.