raylib nim i learn this language today i have to much problem

117 views Asked by At

code https://pastebin.pl/view/a021251b

question I need to explain this errors while trying to fix it but I can't and I'm so tired I've been trying to fix this for 7 hours. 1 - expression '' has no type (or is ambiguous) 2 - undeclared field: 'y' 3 - type mismatch

question 4 - Expression: += astro.speed [1] : [2] astro.speed: Expected one of (first mismatch at [position]): [1] proc +=[T: SomeInteger](x: var T; y: T) [1] proc +=[T: float | float32 | float64](x: var T; y: T) [1] template +=[T: Vector2 | Vector3 | Quaternion | Matrix](v1: var T; v2: T) [1] template +=[T: Vector2 | Vector3 | Quaternion](v1: var T; value: cfloat)

question 5 - expression 'astro' has no type (or is ambiguous) thamls good people i search in google what this erros mean but still i can't fix i change but i don't know i search my problems but still i can't fix and i comes here i start learn nim today

1

There are 1 answers

0
xbello On

I don't know how did you end up with this code, but it's plagued with errors.

  1. "Has no type...", then give it a type.

  2. You construct the Object without reading the manual. At L46 you have:

    var astroObject = AstroObject(astro_texture, astro_x, astro_y, astroSpeed)
    

    But the manual shows this:

    var a1 = Student(name: "Anton", age: 5)
    

    Change the construction at L46 to:

    var astroObject: AstroObject = AstroObject(
      texture: astro_texture,
      x: astro_x,
      y: astro_y,
      speed: astroSpeed)
    
  3. This leads us to "type mismatch:", as astro_texture is an Image, and not a Texture2D as expected by AstroObject:

    var astro_texture: Texture2D = loadTextureFromImage( 
      loadImage("img/astro.png"))
    
  4. Now we have another "type mismatch: got 'int' for 'astroSpeed' but expected 'float'". Just cast:

    var astroSpeed = float(random.rand(1..4))
    
  5. Now we got: "Error: undeclared identifier: 'astro'" at L82, turns out a if astro in astros really means for astro in astros.

  6. This hurts: "Error: undeclared identifier: 'astrp'" at L83.

  7. Now "Error: type mismatch" at L83. Why do you cast astro.speed to a float, when it's already a float and astro.y expects an int? Change the line to:

     astro.y += int(astro.speed)
    
  8. Finally, we arrive at a proper error that could puzzle anyone:

    Error: type mismatch
    Expression: astro.y += int(astro.speed)
    Expected one of (first mismatch at [position]):
    proc `+=`[T: SomeInteger](x: var T; y: T)
    

    Why the compiler claims that two ints are not ints? Turns out astro.y is inmutable, but you want to mutate it (i.e. the compiler wants astro.y to be a var int). Go change the Object to a Mutable or Ref Object:

    type AstroObject = ref object
    
  9. Back to incomprehensible errors: "Error: undeclared identifier: 'random'". You don't call random, you call rand:

    astro.y = -rand(1..1000)
    astro.x = rand(1..906) 
    astro.speed = float(rand(1..4))
    

    (Notice the casting to float).

There you go. Assuming you have all the needed libraries, it should work. Next time, you should try to introduce code step by step, and making each of them work properly, before adding lots of code and then having dozens of errors, typos and lack of types all around.