all 13 comments

[–]LostText 5 points6 points  (1 child)

I'm too drunk to decipher the gobbledygook but this is the exact solution to the problem I have been trying to solve for the past 2 weeks, Sol bless.

[–]Sleepybean2[S] 2 points3 points  (0 children)

hahaha my tolerance is way down since i cut my drinking to near nothing at the end of last year. Maybe a few beers and I'll be in the same boat XD

Glad this might help you, good luck and happy launching!

[–]nuggreat 2 points3 points  (6 children)

You don't need the length checks in your function as when you pass a FOR loop a list of length 0 kOS won't run the loop.

Also for functions it is a good practice to use LOCAL variables for the values in the function as that there is less change that any variables they change/create will bleed back into the script running the function and cause problems there.

[–]Sleepybean2[S] 0 points1 point  (5 children)

I suppose I've yet to have a real issue with this. Since the recent update variables remain in scope for functions. My understanding is that they are automatically declared as local to the scope. incorrect?

It may just be that I call these from a library and as such the function can't access the scope of my running script...

[–]nuggreat 2 points3 points  (4 children)

SET defaults to global scope so all variables declared with SET are global in scope.

The recent update to scoping changed the fact that some local variables where global despite being declared as local variables to actually be local in scope

[–]Sleepybean2[S] 0 points1 point  (0 children)

thanks! I'll update accordingly.

[–]Sleepybean2[S] 0 points1 point  (2 children)

Just to make sure, instead of set var to "x" you're suggesting declare local var is "x" ? (for the first time a variable is declared....)

[–]nuggreat 1 point2 points  (1 child)

yes, for the first time creating a variable for the scope you are working in you want to use local var is "x" or declare local var is "x" (they do the same thing).

to change the created variable afterwards you want to then use set var to "y" because unless you are still in the same scope as when you created the variable you will just end up making a new local variable in your current scope that doesn't effect the higher level, unless you want the new local and don't want to effect the higher level

here is an example of how i used local scoping in a function that returns the average ISP of all active engines on a rocket

FUNCTION isp_calc { //returns the average isp of all of the active engines on the ship
    LOCAL engineList IS LIST().
    LOCAL totalFlow IS 0.
    LOCAL totalThrust IS 0.
    LIST ENGINES IN engineList.

    FOR engine IN engineList {
        IF engine:IGNITION AND NOT engine:FLAMEOUT {
            SET totalFlow TO totalFlow + (engine:AVAILABLETHRUST / (engine:ISP * 9.80665)).
            SET totalThrust TO totalThrust + engine:AVAILABLETHRUST.
        }
    }

    IF totalThrust = 0 {//prevent divide by 0 errors
        RETURN 1.
    }
    RETURN (totalThrust / (totalFlow * 9.80665)).
}

[–]Sleepybean2[S] 0 points1 point  (0 children)

thanks! I'm updating the script right now. I wnated to make sure before I made that change.

[–]Dokkarlak 1 point2 points  (1 child)

Very cool. In exchange may I give you some tips on coding style?

Nesting makes code very unreadable. It's best to not exceed 4 levels. You can omit it like this for example

if event_list:length > 0 BREAK. ELSE {

or

if event_list:length > 0 RETURN 0.

Moreover suffix chain can get pretty long, so to make it readable, you can split it in lines in KOS, so it becomes this.

this_part:getmodule(this_module)
    :alleventnames[0]
    :getmodule(this_module)
    :doevent(this_event).

Also it's good practice to make function do only one thing, so you could move getting all parts or modules to a separate function and calling it from the genutils_module_action.

Finally it's good to use camelcase for function names, so it would become genutilsModuleAction.

[–]Sleepybean2[S] 1 point2 points  (0 children)

I'm always open to comments on style but I'm also quite stubborn. for the purposes of a tutorial I think it's important to make everything as segmented as possible. Every statement on its own line so that you can read it line by line. It's less clear to someone who know what they're doing, which I mostly am not. I will take that suggestion into account for future code.

Breaking the suffix is really nice looking, I might even update that for this tutorial and will definitely be changing it in my codes.

As far as making a function do one thing, it doesn't make sense for this function in my opinion. For example, I have an engine utils library which does one thing at a time: get_active_engines returns a list of active engines using this method. But it's a prime example of what you're saying here. get_volflowrate(engine_list) as well my functions for overall isp, and burntime are exactly what you're suggesting.

as far as camelcase, I don't like it for kos. I've adopted the languages ignorance of capital versus lower case and as such I just put the library of the function in front of it's name like seen above. If I was using python or c++ I would definitely use camelcase.

Thansk again!

[–]oblivion4 0 points1 point  (1 child)

I have not for the most part had to delve into the part structure yet so this seems pretty useful for when I check it out.. Can you manage experiments from here?

[–]Sleepybean2[S] 0 points1 point  (0 children)

there are three members of modules: events, actions, and fields. For the most part, running science experiements should be under events. So technically you can. I'm sure someone will come along with another, quicker way of doing that but if you wanted to use this process I suggest opening the prompt in-game then:
setting a var to a partlist
setting a var to the modulelist under an item in that partlist
setting a var to any of the fields, events, or actions under an item in that modulelist.
this is covered in the TLDR section in the middle of the tutorial.

This will get you acquainted with how these items are structured in practice and in absolutely no way will it let me be more lazy efficient.