Godot Version
4.3 Stable
Question
I’m making an emulator/interpreter for an old console called the chip-8 In Godot 4.3 stable, It’s going really good so far, I basically just load an external ROM into a virtual RAM (8BitPackedArray) and execute it’s each operations code like this for example
0x3000: # 1st nibble is '3':
# 3XNN: Skips the next instruction if VX equals NN.
if registers[opcode >> 8 & 0x0F] == opcode & 0x00FF:
program_counter += 2
My current issue is that I wanna run the emulator at the same speed as the OG console, I first tried having a for loop in the _ready() function that executes all the opcodes, But it ended up running everything before the game even starts.
So next I tried executing the opcodes in the _process() function, And it worked but it was very slow compared to how the fast the console is meant to run, So I wanted to increase how many times _process() would run every second but I couldn’t figure out how, So I came up with a funny solution; have a for loop run 10 times in _process(), This should make _process() run 10 times faster, And yeah it worked but this solution seems very dangerous ? I mean How is this gonna run consistently ? Is it gonna run at the same rate on my PC and also stronger/weaker PCs ? And also when there’s movement happening the frame rate dropped to 45 ?
func _process(delta: float) -> void:
for i: int in range(10):
execute_opcode()
program_counter += 2
queue_redraw()
So next I tried using _physics_process(), It’s meant to run more consistently and I managed to change how often it’s run by increasing the “physics tics per second” option, I tried increasing from 60 to 1000 but my game is still not running fast enough.
# Even if 'physics tics per second' is 1000: It's still too slow.
func _physics_process(delta: float) -> void:
execute_opcode()
program_counter += 2
queue_redraw()
Next I tried returning the “physics tics per second” back to 60, But I added a for loop that would run 5 times, But it resulted in the game being very choppy and slow:
# Very choppy and slow
func _physics_process(delta: float) -> void:
for i: int in range(5):
execute_opcode()
program_counter += 2
queue_redraw()
So yeah I guess I’m confused and don’t know how to solve this issue.