A way to do Jump/Branch/Dispatch/Control tables in Lowres NX?

2

Meticulac 2024-11-22 06:07

One of the first things I thought to try was to make a multidimensional array for sprites. That way I could have a simple pool for allocating game objects, with a column for each loaded game object with a sprite, and rows for x position, y position, current sprite index, object type, and a few more for misc object data.

At first I thought I could use numbers for all of these uses, but for directing object behavior it would be nice to loop over the object and for each simply jump to a function of subroutine determined by the object type ID. This would be very simple with line numbers, but with labels, I'm not really sure how to do this, since I can't have an array of labels

Even if labels were a variable of a different type, that need their own array, I could just use corresponding indices to make that work anyway. But it seems that labels can't be put in an array at all? So I'm not sure if I can use such a table in Lowres NX at all. If not, then I guess I could use binary chop instead, though that would use a little more CPU time per object.

So, it's not something I absolutely can't do without, but this kind of control table would be convenient for such things as state machines, emulation of case statements, and management of dynamic sprite pools. I wasn't sure whether to put this under How To or discussion because I don't know if this is possible or not, but I strongly doubt there is so I'm classifying it as being an idea more than a question.


SP4CEBAR 2024-11-22 08:42 (Edited)

As far as I know, there is indeed no way to jump to a label loaded from a variable. It is likely that the only way to achieve this would be with if statements unfortunately.


SP4CEBAR 2024-11-22 08:49

nathanielbabiak's Raster Interrupt Speed Tricks reference post mentions a SWITCH-CASE-like structure for IF statements that could be used to improve performance at the cost of code length (takes more tokens)


Meticulac 2024-11-23 03:51

The CASE-like structure described as being faster at the cost of more tokens does sound like the binary chop method I had in mind. I'm just starting out with my game, so I guess I won't worry too much about optimization for now, since I don't even know if I'll be running into either performance or token limits at all, much less which one I'll need to be more careful of.

Incidentally, I guess another thing jumping to a label loaded from a variable would allow would be passing a label as a function argument for the purpose of making a wrapper function, though I don't know if that's something I'd be doing much in game development.


was8bit 2024-11-23 16:25 (Edited)

I use labels for:

1) goto or gosub
2) dynamic loading of DATA for READ
3) as a way to visually mark a location in long code

... one powerful thing to use is SUB, where variables inside the sub are seperate from the rest of the program, and if you are careful enough, can reduce your code by repurposing the code..

So

SUB MOVEIT(x,y,dx,dy)
Add x,dx
Add y,dy
.... if statements
Then X=....
Then Y=....
END SUB

This code can be used by ANY item that moves... your players, your enemies, etc...

CALL MOVEIT(myshipx,myshipy,2,3)

The SUB code changes your myshipx,myshipy variables in your main game code..

You can even pass ARRAYS into the SUB which can edit the array and send those changes back back to the sent array...

Carefull use of SUB for many different things can really reduce your code


was8bit 2024-11-23 16:37

If you have a long list of complicated IF statements, lets say you want to constantly scan every cell on then screen each VBL pass, Timo helped me increase code speed with this...

C=CELL.C(X,Y)
IF C>0 THEN
... long list of IF statements
END IF

This way the mostly empty spaces only hits one IF statement, greatly speeding up your code...

If you did
IF C=1 then
ELSE IF C=2 then
....etc...
ELSE IF C=30 then
END IF

lowres handles all the code for each space and slows things way down...


was8bit 2024-11-23 16:43 (Edited)

One odd thing about GOSUB and SUBS...

GOSUB in your normal code functions as you would expect, having access to all variables in your normal code...

However, a GOSUB called inside a SUB functions as an extention of your SUB variables only... one "could" carefully repurpose GOSUB code for normal code and for SUB code, but one might get themselves confused...


Log in to reply.