I'm making a simple game with Nim and the kirpi game framework (https://github.com/erayzesen/kirpi) as a learning project. I want to draw different images to the screen depending on the state of the program. I was hoping I could do something like
import kirpi
var tex1, tex2: Texture # Texture belongs to kirpi
var currTex: Texture
currTex= tex1
# ...and then render or re-assign currTex at some point later
But it gives me the following error:
Error: '=copy' is not available for type <Texture>; requires a copy because it's not the last read of 'tex1'; routine: game
How can I accomplish this? I think references might be the answer, but I've never used a programming language with explicit pointers and I couldn't really parse the examples online (sorry).
I think You should ask this in kirpi forums or discussions to get fast response,
Although I never used kirpi but I think there is problem in how you are using it not Nim I think Texture type in the Kirpi framework is likely a resource managed
I think you should add ref like this this var tex1, tex2: ref Texture and var currTex: ref Texture and use new for loading that tex1 = new(Texture)
Try this and let us know, I am not a game dev but I use to handle this kind of error in rust(Move errors I use to get) and zig(there I need to use Use * and &) and I think texture are often non copyable / non cloneable.
I would try something like this:
var tex1, tex2: Texture
var currTex: ref Texture
new(currTex) # only call this once
currTex[] = tex1 In Nim, most types follow copy semantics (they are copied on assignment). If you need a reference, use ref or ptr - here's a good primer.
ref is a safer interface, but you can't create a reference to an existing object. Instead, create a ref version of your object and dereference it to get plain Texture:
var tex1, tex2: ref Texture = new Texture
loadTexture(tex1[], "someFile,png")
loadTexture(tex2[], "otherFile.png")
var currTex: ref Texture = tex1
drawTexture(currTex[])
ptr is an unsafe reference. You can use it to create a reference to an existing object, but it's your responsibility to ensure it's not stale or dangling.
var tex1, tex2: Texture
var currTex: ptr Texture = addr tex1
drawTexture(currTex[])
Another option is to not use references at all and just keep Textures in an array-like structure and store index IDs. This is very common in game development:
var textures: seq[Texture]
proc addTexture(tex: Texture): int =
textures.add tex
textures.high # Returns the index of the new texture
proc drawTexture(idx: int) =
drawTexture(textures[idx])
var tex1, tex2: Texture
let id1 = addTexture(tex1)
let id2 = addTexture(tex2)
var currTex = id1
drawTexture(currTex)
Note: I've never used kirpi myself. These examples are just to show the idea.