1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Understanding Precaching

Discussion in 'Scripts' started by greatguys1, Nov 1, 2017.

  1. greatguys1

    greatguys1 Active Adventurer Developer Warriors of the North

    Apr 20, 2013
    I think I have a bit of a skewed idea of precaching. In ms.stx, I find:
    precache    //<file> - precache media, beware, this happens before script loads, and happens even if commented
    precachefile    //<scriptname> - precache all media in a script - usually only works inside game_precache - it does not work via cascade (ie. a precachefile command in a top script will not catch any precachefile command in the script it precaches, so any such will need to be duplicated in the top script.)
    game_precache            //precachefile events will work here!
    I was under the impression that precache only worked under the game_precache event, though in effects/sfx_poison_burst I find that there are precaches in the initial open brackets: ( I dunno what they're called )
        const SOUND_BURST ambience/steamburst1.wav
        precache SOUND_BURST
    I assume that precache still works. Is it bad practice to have those precaches in those brackets instead of game_precache? What is the scope of precachefile? Does it get all "precache" commands, or only those in game_precache? What would be the point of an effect like that using the precache, unless it's supposed to be captured by the precachefile?

    Also, in the scripts_read_me.txt there are these entries:
    • Media (sounds/models/sprites) in #include'ed scripts must precached by the top script.
    • The 'precache' command works on a different parser. It therefore will be activated inside comments, and does not parse quotes proper.
    I don't understand how #included scripts aren't precached. Since it isn't, do I have use "precachefile" in game_precache? By "...works on a different parser... and does not parse quotes proper," does that mean I'm not allowed to put quotes around what I'm precaching? Like:
    { game_precache
         precache "monsters/chumtoad.mdl"
         //This is bad
    { game_precache
         precache monsters/chumtoad.mdl
         //This is good
  2. Thothie

    Thothie Administrator Staff Member Administrator Moderator Developer

    Apr 8, 2005
    When the map loads, the system looks through all the scripts used, and initiates space for all variables, sets all constants and setvar's (but not setvard) and runs all "precache" commands. It also automatically precaches some media from various commands, provided they are not in a variable form (constants are okay). During this pass it ignores all conditionals, just searching for the commands, regardless of where in the script they are. So yes, precache can be used anywhere. (Also yes, it sometimes fubars on quotes, and it used to even get pulled inside comments - think we fixed that, not sure.)

    This process does not always continue down the #include chain, so the top script must precache any and all media to be used.

    The commands that automatically invoke precache are as follows:
    setworldmodel //defunct
    setpmodel //defunct
    setshield  //defunct
    (Please note that "effect" is not among these, you must manually precache any model/sprite used with an "effect" command.)

    You should only precache .wav's played by svplaysound, svplayrandomsound, or svsound.play3d. All client side media (except for .wav's) must also be precached by the server side script that generates the clientevent.

    "precachefile", on the other hand, has to be in the game_precache, and just tells the game to precache media in a script that isn't on the map (usually summons). Keeping the regular old "precache" commands in there is just being neat, so you have a nice centralized place where you can find all the media not automatically precached.

    In case that wasn't clear, this means if you are using a piece of media that is stored in a setvard (such as a randomized sprite or model), you have to use the precache command on each possible result.

    If you want to be safe, precache each model and sprite used by the script, its #includes, and any clientevent or createnpc it may spawn, as well as their #includes. Redundant precaches don't hurt, as they are ignored. You, however, only want to precache sounds that are played serverside (via svplay commands mentioned earlier), otherwise you will needlessly inflate the precache count, and that's a key limiting factor to how much stuff you can squeeze into a map (keeping in mind that all the items and player summons have already stuffed that pretty full).
    greatguys1 likes this.
  3. greatguys1

    greatguys1 Active Adventurer Developer Warriors of the North

    Apr 20, 2013
    Clears up some stuff, and learned a few new things. Thanks :)

Share This Page