greenfork / nimraylib_now

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

if object has over 4 variables, one will always be 0 #79

Closed PyryTheBurger closed 2 years ago

PyryTheBurger commented 2 years ago

so I have a button type with 4 variables, no problem:

type
    Button* = object
        texture*: Texture2D
        rect*: Rectangle
        text*: string
        pressed*: proc(): void

and just a norrmal constructor like button1: Button = Button(rect: (100.0, 0.0, 24.0, 24.0), text: "1", pressed: echohelloworld) and everything works.

now if i have over 4 variables like this:

type
    Button* = object
        texture*: Texture2D
        rect*: Rectangle
        text*: string
        pressed*: proc(): void
        annoying*: string

1 of the variables (rect in this case idk if its random or something = (0.0, 0.0, 0.0, 0.0)) is 0 even though i construct it the same way as above

PyryTheBurger commented 2 years ago

i also have these methods that use it could they be the problem idk

proc updateHButtons*(hButtons: seq[Button]) =
    if selectedButton == 0 and isKeyPressed(leftKey):
        selectedButton = len(hButtons) - 1
    elif selectedButton == len(hButtons) - 1 and isKeyPressed(rightKey):
        selectedButton = 0
    else:
        if isKeyPressed(leftKey):
            dec(selectedButton)
        if isKeyPressed(rightKey):
            inc(selectedButton)

    if isKeyPressed(interactKey):
        hButtons[selectedButton].pressed()

    buttonSelected = hButtons[selectedButton]

proc drawButtons*(buttons: seq[Button]) =
    for i, v in pairs(buttons):
        if v == buttonSelected:
            drawRectangleLinesEx(v.rect, 1, Black)
            drawText(cstring(v.text), int(v.rect.x), int(v.rect.y), 1, Black)
        else:
            drawRectangleRec(v.rect, Black)
            drawText(cstring(v.text), int(v.rect.x), int(v.rect.y), 1, White)
enthus1ast commented 2 years ago

@PyryTheBurger could you create a minimal, self contained example code that we can run to reproduce the issue?

greenfork commented 2 years ago

@PyryTheBurger I saw your answer on discord that you solved it by using ref object instead of plain object. This changes the semantics of how things work so there are cases where you would want to have a plain object still. If you could provide us a snippet of code which clearly demonstrates the issue, we could help you with the problem you are facing.