KSP-KOS / KOS

Fully programmable autopilot mod for KSP. Originally By Nivekk
Other
691 stars 229 forks source link

Local Variables not scoped correctly #2691

Closed MathewUlanowski closed 4 years ago

MathewUlanowski commented 4 years ago

When putting local variables inside of a conditional they are not scoped to the function it acts as if they are scoped to the conditional The specifics of how I found this are that I have a library program that is ran at start and a controller that passes values into those functions however i dont think this is the issue but the code goes kinda like this

FUNCTION Velocity_TWR_Offset { // WARNING this function has a input limit of 100m/s // required parameter this is the desired vertical velocity velocity PARAMETER Vertical_Velocity. print(Vertical_Velocity). if(Ship:verticalspeed < -15){ local TWR to ship:MAXTHRUST/ship:MASS. } else if(ship:verticalspeed > 15){ local TWR to 0. } else if(Vertical_Velocity = 0){ local TWR to 1+(100/ship:verticalspeed). } else{ local TWR to (Vertical_Velocity/ship:VERTICALSPEED). } RETURN TWR. }

the specific error says Undefined Variable Name 'twr'.

Dunbaratu commented 4 years ago

You have correctly described how it behaves. You have not described why it is wrong.

Some languages scope locals to the braces they are in, regardless of whether that brace is a function, and if clause, a loop, or whatever. C++, Java and C# are like that. Others "pop" the scope upward to the function or method they are in, not the innermost braces. Python is one of those.

This is one of those places where different languages do it differently. The Kerboscript docs do state that it works this way (to the brace, not to the function).

Dunbaratu commented 4 years ago

On an unrelated note. If you want to post a code snippet and have it format the spacing properly so we can see the indenting, like so:

this
    is
        indented

you do that by starting the block with a line with three backticks ``` and ending the block with another line of three backticks ```