Jump to content

Recommended Posts

So I was going through Creature Tutorial 6 (Brains) and saw these lines of code in the tut06_brain.lua.

RunAway(self.inst, "scarytoprey", AVOID_PLAYER_DIST, AVOID_PLAYER_STOP),StandStill(self.inst, function() return true end),

My curiosity took me to the respective files - runaway.lua and standstill.lua. The very first lines of both files were

RunAway = Class(BehaviourNode, function(self, inst, hunterparams, see_dist, safe_dist, fn, runhome)StandStill = Class(BehaviourNode, function(self, inst, startfn, keepfn)

Coming from a Java background, I feel that the arguments of the function RunAway in the first code section don't match the arguments of the function RunAway in the second code section. 

 

The first argument should be self, but instead is self.inst (because the function in the second code section asks for a 'self')

The second argument should be self.inst, but is "scarytoprey".

The third argument should be "scarytoprey", but is AVOID_PLAYER_DIST.

The fourth argument should be AVOID_PLAYER_DIST but is AVOID_PLAYER_STOP.

There should also be two more arguments which I didn't understand - fn and runhome.

 

The same goes for StandStill - there are four args in the second code section, but only two are present in the tut06_brain.lua file.

 

Is this something Don't Starve/Lua specific? Are the tutorials outdated? Can someone please explain?

 

If you didn't understand, don't hesitate and ask me for a more clear explanation.

I am only a hobbyist modder, but I think the first variable "self" is being auto-filled somehow (words of an expert :p).

 

Regarding the second point: The additional variables are optional. The function takes as many as it gets and runs. Tests such as

if fn then    fn(inst, stuff)end

make sure you don't have to declare that variable, but can do.

Indeed the self is a hidden parameter. More here: http://www.lua.org/pil/16.html

 

What it comes down to is that when coding a function these two are interchangeable

 

function one:two(test) 

 

one.two = function(self, test)

Edited by Heavenfall

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...