Sheepolution Posted July 7, 2015 Share Posted July 7, 2015 This has more to do with me wanting to learn how to program (properly), and less with modding Don't Starve, I hope that's okay, I'm sorry if it's not. I noticed that brains require behaviours. And my question is, why not require all the behaviours at the start? Isn't it better to require all files only once, from the start, rather than multiple times all over the place? Link to comment Share on other sites More sharing options...
Blueberrys Posted July 7, 2015 Share Posted July 7, 2015 @Sheepolution Erm, didn't quite understand your question. Which brain file(s) require them all over the place? I checked a couple of them and it seems that they do require all the behaviours they need at the top of the file. Link to comment Share on other sites More sharing options...
Sheepolution Posted July 7, 2015 Author Share Posted July 7, 2015 @Sheepolution Erm, didn't quite understand your question. Which brain file(s) require them all over the place? I checked a couple of them and it seems that they do require all the behaviours they need at the top of the file. With "All over the place" I mean in various scripts. Pigbrain and Rabbitbrain both have the Runaway behaviour for example. Link to comment Share on other sites More sharing options...
Blueberrys Posted July 7, 2015 Share Posted July 7, 2015 @Sheepolution That's just the way lua (and most languages) work(s). When you require something in a script, it becomes available for use in that script, not all scripts. More info on require:Explained in context to DS codeModules tutorial Link to comment Share on other sites More sharing options...
Sheepolution Posted July 7, 2015 Author Share Posted July 7, 2015 Except they're globals, so it does become available in all scripts. Right? Link to comment Share on other sites More sharing options...
Blueberrys Posted July 7, 2015 Share Posted July 7, 2015 No, not to all scripts, only the scripts that require it. Being global doesn't affect where they are available, just their accessibility when used in other scripts. This module uses and returns a local variable.local mod = {}mod.var = 1return modWhen you require it, you can access the internal variables like so.mod = require("mod")print(mod.var)-This module uses global variables.mod = {}mod.var = 2Notice that nothing needs to be returned here because mod is global.require("mod") -- Does not receive the mod table, so we don't need to store the return valueprint(mod.var) -- Using the mod variable from the module directly- That's the default behaviour, but I think Klei code gives you access to some commonly used global variables in your mod environment automatically.See more accurate post below instead. Link to comment Share on other sites More sharing options...
Sheepolution Posted July 9, 2015 Author Share Posted July 9, 2015 No I am highly sure it is all scripts, but I'm afraid my question can only be answered by the original devs. My only guess is that it's a way to show what behaviours a brain has. Thanks though. Link to comment Share on other sites More sharing options...
ColorfulStan Posted July 10, 2015 Share Posted July 10, 2015 In general its a desired convention to make all dependancies of a module visible where the module is defined. So as you already figured out this is a good way to tell a developer "look, these behaviours are used within this brain". Another reason to require files (multiple times) where they are needed is to have a looser coupling between files, so that one can exist without the need for the other one to exist. In general require only loads a file if it wasn't already loaded.Additionally the use of require in every file that depents on the behaviour instead of loading all behaviours in a single file before everything else makes sure the dependancy will (only) be loaded if needed. For example pigbrain requiresrequire "behaviours/wander"require "behaviours/follow"require "behaviours/faceentity"require "behaviours/chaseandattack"require "behaviours/runaway"require "behaviours/doaction"--require "behaviours/choptree"require "behaviours/findlight"require "behaviours/panic"require "behaviours/chattynode"require "behaviours/leash"and rabbitbrain requiresrequire "behaviours/wander"require "behaviours/runaway"require "behaviours/doaction"require "behaviours/panic"if we wouldn't have the pigbrain implemented yet we would need to load a lot less behaviours but still have to be certain that all neccessary behaviour-files are available. And then when we would add the pigbrain to our game-files we just require all the behaviours we need, regardless of which behaviours already are used by other brains. That way every bit of the game can be developed / added / removed independantly. Hope that makes some of the advantages of this approach more clear. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.
Please be aware that the content of this thread may be outdated and no longer applicable.