Problems with includes

Discussion in 'CommandHelper' started by Oboist, Dec 27, 2017.

  1. Oboist

    Oboist Member

    Hello!
    I was writing some scripts for holidays (presents for players, little event and such) and, of course i was writing them in a separate .ms file to easily remove (and maybe to use again year later).

    But encountered a problem.
    if i write in my "xmas.ms":
    PHP:
    set_interval(100, 500, closure(_dothing()))

    proc _dothing(){...}
    , i'll get exception "Unknown procedure "_dothing"", unless procedure is defined BEFORE code that uses it. Weird, in auto_include.ms one proc can use another, therever they are relatively to each other
    Another problem - if i try to use procs from auto_include.ms, i also will not be able to use them (same exception).
    "include('xmas.ms')" is in beginning of auto_include.ms
    That i'm doing wrong?
  2. Oboist

    Oboist Member

    ...and it all works perfectly when i put all include()'s in the end of auto_include.ms!
  3. PseudoKnight

    PseudoKnight Well-Known Member

    Procs are stored in the "environment". Closures clone the current environment and store it for later use when the closure is executed, so it doesn't know about the proc that's added later in the first environment.

    Procedures don't store an environment at all, so it uses the proc list from the environment it was called from.

    Try not to put executable code in auto_include.ms, only procs. This means including code that has executables, which is what you're doing. Instead, you can just leave xmas.ms by itself in a non-library folder and it'll execute normally after all the auto_include.ms files are compiled and added.
  4. Oboist

    Oboist Member

    Thanks, i think i understand logic now.
    I'm not putting any code in auto_include.ms, aside procs and include()'s
    Can you explain that again, please? So there's a way to add code without writing "include()"?
  5. PseudoKnight

    PseudoKnight Well-Known Member

    As long as those includes don't have code besides procs too.

    You don't need to include a ms file to have it run on startup. It just needs to be in LocalPackages.
  6. Oboist

    Oboist Member

    That's awesome =)
    But as i understand it't huge difference for procedures like read(). So, scripts in LocalPackages will not be able to use any data that the rest scripts stored with CHDangerous... unless through procedure located in base dir
  7. PseudoKnight

    PseudoKnight Well-Known Member

    That's not true. Any file under the base-dir is accessible by any script.
  8. Oboist

    Oboist Member

    ...but then i use same "chd_write('presents/'[email protected]'.txt', @value)"...
    in main.ms - it actually writes to "CommandHelper/presents/"
    in LocalPackages/xmas.ms - to "CommandHelper/LocalPackages/presents/"

    How can i access "CommandHelper/presents/" by script that is located in LocalPackages?

    Oh, Happy New Year! =)
  9. PseudoKnight

    PseudoKnight Well-Known Member

    Without a starting forward slash, it gets the file location relative to the script running it. To get a file location in the next directory up with a relative path, you can do "../presents".
  10. Oboist

    Oboist Member

    omg, how i lived without that? Huge Thanks!