greenfork / nimraylib_now

The Ultimate Raylib gaming library wrapper for Nim
MIT License
151 stars 17 forks source link

Hot code reload #89

Open joseasv opened 1 year ago

joseasv commented 1 year ago

Hello. I was trying the hot code reload feature of Nim following the official docs (https://nim-lang.org/docs/hcr.html). Sincec I got that SDL2 example running I wanted to try that feature with NimraylibNow. I slightly modified the crown.nim example adding a main.nim file that calls some procs on the crown.nim file.

crown.nim

import math
import nimraylib_now
import std/hotcodereloading

var runGame*: bool = true

const
  nimFg: Color = (0xff, 0xc2, 0x00)          # Use this shortcut with alpha = 255!
  nimBg: Color = (0x17, 0x18, 0x1f)
...

proc init* () =
  for i in 0..<crownSides:
    let multiplier = i.float
    # Formulas are for 2D space, good enough for 3D since height is always same
    lowerPoints[i] = (
      x: lowerRadius * cos(centerAngle * multiplier),
      y: lowerRadius * sin(centerAngle * multiplier),
    )
    ...

proc update*() =
  while not windowShouldClose():
    if isKeyPressed(KeyboardKey.F9):      # Trigger the reload
      performCodeReload()

    if not pause:
      camera.addr.updateCamera   # Rotate camera

 ...

proc destroy*() =
  closeWindow()

main.nim

import crown

proc main() =
  init()
  while runGame:
    update()
  destroy()

main()

The code compiles just fine but when I run it I have this error:

/home/______/.choosenim/toolchains/nim-1.6.10/lib/nimhcr.nim(531) hcrInit
/home/______/.choosenim/toolchains/nim-1.6.10/lib/nimhcr.nim(490) recursiveDiscovery
/home/______/.choosenim/toolchains/nim-1.6.10/lib/nimhcr.nim(432) loadDll
/home/______/.choosenim/toolchains/nim-1.6.10/lib/system/assertions.nim(38) failedAssertImpl
/home/______/.choosenim/toolchains/nim-1.6.10/lib/system/assertions.nim(28) raiseAssert
/home/______/.choosenim/toolchains/nim-1.6.10/lib/system/fatal.nim(54) sysFatal
Error: unhandled exception: /home/______/.choosenim/toolchains/nim-1.6.10/lib/nimhcr.nim(432, 12) `lib != nil`  [AssertionDefect]

I don't know how to debug the nimhcr library file to check which library returns nil in loadLib.

greenfork commented 1 year ago

Hi, unfortunately I don't know how HCR currently works. Previously it was an implementation people didn't like with little documentation, now at least the documentation page is a lot more clear. So if we assume that this is going to be possible with Raylib at all, which may not be true, then I would just put a couple of debug statements in the file .choosenim/toolchains/nim-1.6.10/lib/nimhcr.nim to get to the bottom of the problem.

So in short this could be impossible with the current state of HCR but it is still worth trying. The Raylib library itself is pretty complicated because it tries to be compatible across many platforms, which could also contribute to the complexity of the problem. I will leave this issue open for tracking it. If you would like to take a stab at it, feel free to do so. This could be easily resolvable too.