Hello dear Nim community,
I'm getting started with Nim after realizing that gdscript is way too slow and C++ way too complicated in many situations.
I'm having the following issue:
In gdscript I write:
var mat = get_surface_material(0)
var tex = ImageTexture.new()
tex.create_from_image(image)
mat.set_shader_param("texture_map", tex)
where the material has a custom shader with a uniform sampler2D texture_map;. Doing the same in nim:
var mat: ShaderMaterial = getSurfaceMaterial(0) as ShaderMaterial
var texture = gdnew[ImageTexture]()
texture.createFromImage(image)
mat.setShaderParam("texture_map", texture)
Gives me an error about texture not being Variant. It doesn't work with texture as Variant, but maybe I missed something? Also, I'm not sure if the as ShaderMaterial works as hoped. Unfortunately a clear documentation on how to use materials is ... not existent?I'm glad you're interested in Nim!
When is this error occurring? I suspect it's a compile time error and not an exception?
I'm looking at the godot docs and I'm only seeing that proc setShaderParam in an older version. I did a search on GitHub but I didn't find anything. I could be missing it, though?
What version of godot are you using, and what version of the godot binding are you using?
Hey twetzel59,
thank you so much for taking a look!
It's not that the compiler doesn't find setShaderParam, it's that it expects a Variant!
This is what I get:
Error: type mismatch: got <ShaderMaterial, string, ImageTexture>
but expected one of:
proc setShaderParam(self: ShaderMaterial; param: string; value: Variant)
first type mismatch at position: 3
required type: Variant
but expression 'texture' is of type: ImageTexture
proc setShaderParam(self: ShaderMaterial; param: string; value: Variant)
first type mismatch at position: 1
required type: ShaderMaterial
but expression 'mat' is of type: ShaderMaterial
Never tried it but perhaps like this?
mat.setShaderParam("texture_map", texture.toVariant)
Oh, I missed that function toVariant()! It actually works! It's a bit confusing to have the as command not working here... But I'm new to Nim, so maybe there is a reason for it?
Anyways, thank you so much! :D
toVariant calls newVariant, whereas the as proc doesn't perform any calls, just safe type conversions. You can allow as to call toVariant by overloading the as call like this:
proc `as`(obj: NimGodotObject, t: typedesc[Variant]): Variant =
obj.toVariant()
# ...
mat.setShaderParam("texture_map", texture as Variant)