Jump to content

What is the point of requiring behaviours?


Recommended Posts

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

@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

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 mod

When 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 = 2

Notice 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

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 requires

require "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 requires

require "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

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.

×
  • Create New...