r/Basic • u/7ootles • Sep 11 '18
GW-BASIC, garbage collection?
Hi all. I've been playing with some game programming, getting back into BASIC after a long time away from it. Got a bit of a problem though - in GW-BASIC/BASICA, I've been doing a bit of an RPG (inspired by the "big" RPG I've been working on in QuickBASIC; wanted to see how small I could do it), and in the last couple of days, between doing other things, I've got something working that allows me to load and walk around a map, and load another if I walk off the edge. However, currently I'm coming up against "out of memory" errors after swapping maps ~10 times.
The maps are loaded from disk into a 2d array, this works fine, and I'd assumed that when I load another in it would just overwrite what's there... but it seems that it's just putting the new map in next to it or something. How can I revirginize just that variable? I've looked at FRE() and CLEAR, but all they've done is completely wipe the memory.
Or is it possible that I'm doing something else wrong? I'll post the code, in case there's a grown-up here who might have more of a clue.
Thanks :-)
Code below (as written in BASICA and tested on later GW-BASIC and the recent PC-BASIC):
100 DIM MAP%(24, 79)
120 PX = 15: PY = 9
200 MN$ = RIGHT$(STR$(MIDX), 1) + RIGHT$(STR$(MIDY), 1)
210 PXIN = PX: PYIN = PY
235 CLS
240 OPEN MN$ FOR INPUT AS #1
250 FOR MX = 1 TO 24
260 FOR MY = 1 TO 79
270 INPUT #1, MAP%(MX, MY)
275 GOSUB 10000
280 NEXT
290 NEXT
300 CLOSE #1
400 FOR MX = 1 TO 24
410 FOR MY = 1 TO 79
420 GOSUB 10000
430 NEXT
440 NEXT
500 WHILE 1
560 GOSUB 1000
570 GOSUB 2000
580 WEND
600 END
1000 LOCATE PX, PY 'SHOW PLAYER
1010 PRINT CHR$(2);
1999 RETURN
2000 A$ = INKEY$
2060 IF A$ = (CHR$(0) + CHR$(72)) THEN PXIN = PX - 1: GOSUB 3000 'UP
2090 IF A$ = (CHR$(0) + CHR$(80)) THEN PXIN = PX + 1: GOSUB 3000 'DOWN
2120 IF A$ = (CHR$(0) + CHR$(75)) THEN PYIN = PY - 1: GOSUB 3000 'LEFT
2150 IF A$ = (CHR$(0) + CHR$(77)) THEN PYIN = PY + 1: GOSUB 3000 'RIGHT
2900 IF A$ = CHR$(27) THEN CLS: END 'ESCAPE
2999 RETURN
3000 MX = PX: MY = PY
3010 IF MX > 24 THEN MX = 24: IF MY > 79 THEN MY = 79
3020 GOSUB 10000
3030 IF PXIN = 25 THEN MIDX = MIDX + 1: PX = 1: GOTO 200
3040 IF PYIN = 80 THEN MIDY = MIDY + 1: PY = 1: GOTO 200
3050 IF PXIN = 0 THEN MIDX = MIDX - 1: PX = 24: GOTO 200
3060 IF PYIN = 0 THEN MIDY = MIDY - 1: PY = 79: GOTO 200
3070 PXOLD = PX: PYOLD = PY: PX = PXIN: PY = PYIN
3110 IF PXIN < 1 OR PXIN > 25 THEN PX = PXOLD
3120 IF PYIN < 1 OR PYIN > 80 THEN PY = PYOLD
3200 GOSUB 20000
3400 GOSUB 1000
3999 RETURN
10000 LOCATE MX, MY
10010 IF MAP%(MX, MY) = 0 THEN PRINT CHR$(32); 'DISPLAY GROUND
10030 IF MAP%(MX, MY) = 9 THEN PRINT CHR$(176); 'DISPLAY WATER
10050 IF MAP%(MX, MY) = 2 THEN PRINT CHR$(61); 'DISPLAY BRIDGE
19999 RETURN
20000 IF MAP%(PX, PY) = 9 THEN GOSUB 30000 'WATER
29999 RETURN
30000 PXIN = PXOLD: PYIN = PYOLD: PX = PXIN: PY = PYIN 'PLAYER CAN'T MOVE
39999 RETURN
Click here for the maps. They're too big to just copy/paste in here.
View all comments
Show parent comment
1
u/7ootles Sep 16 '18
I did wonder about that, actually - especially after putting in a PRINT FRE(0) every time a new map loaded and having the memory usage flit between 2K and 20-ish. I even ERASED the MAP(x,y) variable and DIMed it again immediately before loading in a new map. So what you said makes even more sense. So I'll definitley give that a shot at some point soon. This'll also account for why the "big" game that I'm doing in QiickBASIC doens't have this problem: that's all properly declared SUBs and FUNCTIONS.
Since posting this, though, I've taken the map swapping code out - because this was doing my head in. But I might give it another shot, bearing in mind what you've said. I've got a few other things working and so maps are out of my head, which is maybe a good thing. I don't want to fill it with features though. To make maps simpler, I might have the "story" of the game set in a castle or something, where there's stairs going up and down. That way the code to load in a new map can be simpler too - it'll be a straight up and down, rather than moving in a meta-grid.
X and Y are... I don't know. I should swap them round, I know, but the reason why they're that way round is because that's how the LOCATE statement works. It does the Y axis first (line Y, column X). I don't like it at all, but it seems to work. I should do a backup and see about swapping it though.
I corrected the IF statements being on the same line, I'd had an "engage brain" moment and forgotten that the colon meant "carry on doing this after this IF statement and don't treat it separately".
Which dialect of BASIC did you translate it to, then? PICaxe, or was that reference just a comment about the GOSUB stack? Just curious. Thanks though, this is great :-D