BenChung / KRPC.jl

A kRPC client for Julia
MIT License
3 stars 4 forks source link

UndefVarError: `this` not defined for many generated functions #12

Closed Rhahi closed 1 week ago

Rhahi commented 1 year ago

Hello again,

I discovered that some generated functions try to access,this, when this variable was not defined in the function itself.

LaunchVesselFromVAB(conn::KRPCConnection, name::String, recover::Bool = getJuliaValue(this.conn, UInt8[0x01], Bool)) = begin
        kerbal(conn, var"##609"(name, recover))
    end

So trying to launch vessel from VAB will fail.

SCH.LaunchVesselFromVAB(sc.conn, "Kerbal X")
ERROR: UndefVarError: `this` not defined
Stacktrace:
 [1] LaunchVesselFromVAB(conn::KRPC.KRPCConnection, name::String)
   @ KRPC.Interface.SpaceCenter.Helpers ~/.julia/dev/KRPC/src/generated.jl:10046
 [2] top-level scope
   @ ~/.julia/dev/SpaceLib/test/live/kerbalx.jl:19

Workaround (half fix)

First, the name needs to be changed. The change should be made in the code generation part, but I do not know how it works yet. Instead, I can point out how it should be changed to address this issue.

1. Rename some generated code.

Change this to conn, in code that referes to this and does not set it as function parameters. I did this using regex replacement in my setup, which is error-prone. (\(conn::KRPCConnection.*)this.conn -> $1conn.conn (VSCode regex replace flavor)

LaunchVesselFromVAB(conn::KRPCConnection, name::String, recover::Bool =
getJuliaValue(conn.conn, UInt8[0x01], Bool)) = begin
              ^^^^
        kerbal(conn, var"##609"(name, recover))
    end

2. Bring in refernce to getJuliaValue

these helper functions use getJuliaValue from types.jl, but do not have access to it. In Helpers module, I added import ....getJuliaValue so that the methods know where to find it. There might be other modules that have missing imports, but this is what I know for now.

After that, I could launch the rocket using KRPC.jl. Yes!