Hello my fellow nesdevers,
What is the best way to jump to an address held by a pointer?
The way I have it set up right now is that each of my gameobjects (16bytes each) that are held in a list in ram have their own update routine I call once during the NMI.
I could have just one ID byte for each gameobject and go through it like one big switch statement.
like this:
I want to avoid this cause if the Gameobject happens to be on the bottom of the list it would waste allot of time.
Instead I was thinking about this:
What is the best way to do this?
At the moment I'm doing it like this:
It works and I'm very happy it does but surely there must be a better way?
Thanks in advance!
What is the best way to jump to an address held by a pointer?
The way I have it set up right now is that each of my gameobjects (16bytes each) that are held in a list in ram have their own update routine I call once during the NMI.
I could have just one ID byte for each gameobject and go through it like one big switch statement.
like this:
Code:
;; GameObject in ram:
.db #ID, ,,,,other values
;;;; When i want to update a gameobject:
;X holds ID
CPX #PLAYER
BNE .NotPlayer
JSR PlayerUpdateRoutine
.NotPlayer
CPX #ENEMY
BNE .NotEnemy
JSR EnemyUpdateRoutine
.NotEnemy
;; And so on.
.db #ID, ,,,,other values
;;;; When i want to update a gameobject:
;X holds ID
CPX #PLAYER
BNE .NotPlayer
JSR PlayerUpdateRoutine
.NotPlayer
CPX #ENEMY
BNE .NotEnemy
JSR EnemyUpdateRoutine
.NotEnemy
;; And so on.
I want to avoid this cause if the Gameobject happens to be on the bottom of the list it would waste allot of time.
Instead I was thinking about this:
Code:
;; GameObject in ram:
.db #ID,BehaviourAdrresLow,BehaviourAdrresHigh, ,,,,other values
;; Then in the update code I want to jump to the address that is held in that piece of ram
;; This would also allow to easily swap behaviors if so desired.
.db #ID,BehaviourAdrresLow,BehaviourAdrresHigh, ,,,,other values
;; Then in the update code I want to jump to the address that is held in that piece of ram
;; This would also allow to easily swap behaviors if so desired.
What is the best way to do this?
At the moment I'm doing it like this:
Code:
;; in ram:
GeneralOpcode .rs 1
GeneralPtr .rs 2
GeneralOpcode2 .rs 1
;;;;;;
LDA #$20
STA GeneralOpcode ;; store the JSR opcode
LDA #$60
STA GeneralOpcode2 ;;Store the RTS opcode
LDA $0500,y ;;holds the BehaviourAdrresLow
STA GeneralPtr
INY
LDA $0500,y ;;BehaviourAdrresHigh
STA GeneralPtr + 1
;;Store the returnadress on the stack
LDA #High(ReturnLabel - 1)
PHA
LDA #low(ReturnLabel - 1)
PHA
JMP GeneralOpcode
ReturnLabel
;; rest of program
GeneralOpcode .rs 1
GeneralPtr .rs 2
GeneralOpcode2 .rs 1
;;;;;;
LDA #$20
STA GeneralOpcode ;; store the JSR opcode
LDA #$60
STA GeneralOpcode2 ;;Store the RTS opcode
LDA $0500,y ;;holds the BehaviourAdrresLow
STA GeneralPtr
INY
LDA $0500,y ;;BehaviourAdrresHigh
STA GeneralPtr + 1
;;Store the returnadress on the stack
LDA #High(ReturnLabel - 1)
PHA
LDA #low(ReturnLabel - 1)
PHA
JMP GeneralOpcode
ReturnLabel
;; rest of program
It works and I'm very happy it does but surely there must be a better way?
Thanks in advance!