Jump to content

kOS Script not Working


Recommended Posts

Hi there, I'm playing around with kOS in KSP and I keep getting an error whenever I try to run it. I've made sure there is periods when needed (at least I hope there's periods where needed). I get this error: Syntax: Unexpected token '{' found. Expected EOI at line 11.

Here is my script:

Spoiler

//engine and clamps on same stage.

//launch.
clearscreen.
lock throttle to 1.
lock steering to up + R(0,0,180).
wait 1.
stage.

//gravity turn.
wait until altitude > 2000 {
    lock steering to up + R(0,-60,180).
}

//fairing jettison.
wait until altitude > 50000 {
    stage.
}

//second stage.
wait until apoapsis = 120000 {
    lock throttle to 0.
    wait 1.
    stage.
    wait 1.
    stage.
    RCS on.
    lock steering to prograde.
}

//orbit burn.
wait until eta:apoapsis < 25 {
    lock throttle to 1.
}
wait until periapsis >= 120000 {
    lock throttle to 0.
}

//check orbit.
if apoapsis > 150000 {
    wait until eta:periapsis < 35 {
        lock steering to retrograde.
        wait 5.
        lock throttle to 0.5.
        wait until apoapsis = 120000 {
            lock throttle to 0.
        }
    }
    else {
        break.
    }
}

//payload deploy and finish.
wait 5.
stage.
AG1 on.
set ship:control:pilotmainthrottle to 0.

It found the "unexpected token" here: wait until altitude > 2000 {

Thanks :)

Link to comment
Share on other sites

Delete the word "wait" from the loops like this:

wait until altitude > 2000 {
    lock steering to up + R(0,-60,180).
} 

so they become like this:

until altitude > 2000 {
    lock steering to up + R(0,-60,180).
} 

The command "wait until" isn't a loop header.  It's just a single line statement that says "the program will do nothing and be stuck on this line until the condition is true."

The command "until", on the other hand, is a loop header.

 

That being said, there's some other problems in the code, but you haven't encountered them yet because of that syntax error.  When you get rid of those syntax errors, you'll run into the following issues next:

 

(1) constantly re-running lock steering:   There's no point in (and it's actually detrimental to be) re-running the lock statement again and again inside a loop if you're not actually changing the formula you're using.  The whole point of "lock" is that you give it a formula and it can re-run it when it needs to.  When using lock steering, re-running the lock statement again and again essentially turns kOS's PID controller into just a dumb "P" controller, since you keep telling it to make a fresh start with a "new" steering formula, so it clears its memory of how it's been behaving under the "old" one.  (It doesn't realize the "new" and the "old" one are identical and it didn't need to do that.)

(2) busy looping fast without an explicit wait: The loop you are trying to run, not only re-locks the steering again and again unnecessarily, but also pointlessly re-executes it *multiple times per game-engine update*, even though it can't have any effect until the next 'physics tick' of the game engine anyway.  Usually when you do have a loop like this, you want to explicitly allow a 'tick' to pass in between iterations by inserting a 'wait 0.' (wait the minimum possible wait time, which is just until the next 'tick'.) into the loop body.

But that being said, I only mention (1) and (2) for your future information in situations where you do use loops, because what you *probably* really want to do in this specific instance is just get rid of this loop entirely and turn this:

wait until altitude > 2000 {
    lock steering to up + R(0,-60,180).
} 

into this:

lock steering to up + R(0,-60,180). // no need to loop this - it will keep being re-run in the background by the kOS steering manager.
wait until altitude > 2000. // just do nothing and sit here until the condition is true.

 

Link to comment
Share on other sites

Rather use structure like this:

lock steering to up + R(0,-60,180). // you don't need it inside loop if you don't want to change direction

until altitude > 2000 {
// execute some other commands if needed
wait 0.1.
}. // don't foreget "." after closed bracket - main reason for your previous error

EDIT:

Steven have probably provided better answer, forgot to read whole thread before posting.

Edited by kcs123
Link to comment
Share on other sites

11 hours ago, Steven Mading said:

Not true.  Period at the end of a bracketed section is optional. 

Don't know why I thought that is needed to be. Probably I got confused with different programing languages and my old habit to put "." or ";" after each command or block. Perhaps jumping from one language to other on daily basis have left some marks on brain :)

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...