Jump to content

Recommended Posts

I've just started diving into the code of Don't Stave and I'm trying to familiarize myself before I begin doing anything. I've never done any lua coding before so I'm learning as I go.

 

I've tried googling what this global("foo") is to no avail. I've also tried looking through the don't starve code base and can't find where this is declared.

 

Any sort of help would be welcome. Thanks!

This question is concerning Don't Starve Together, not Don't Starve. The correct forum can be found here.

 

In regards to your question:

I don't know specifically what 'foo' is used for, but the 'global' function declares global variables. Normally in Lua global variables can be declared by assignment, but in Don't Starve the global table has been modified to require this function to be called first. In DST, this likely declares it only for the local instance, not for other networked instances.

This question is concerning Don't Starve Together, not Don't Starve. The correct forum can be found here.

 

In regards to your question:

I don't know specifically what 'foo' is used for, but the 'global' function declares global variables. Normally in Lua global variables can be declared by assignment, but in Don't Starve the global table has been modified to require this function to be called first. In DST, this likely declares it only for the local instance, not for other networked instances.

 

foo is actually just a placeholder name. Do you know where specifically in the code this global("") function is defined? I couldn't find it anywhere.

Could you be more precise on your question because I have no idea what you are actually asking :grin:

 

There are calls to global("foo") throughout the code and I want to know what it does. :D Or actually better I want to know where it is defined if you have any idea.

Why is it so important to know where the global() function is defined? What do you want to do with it? (otherwise it's in strict.lua but it won't help you at all).

 

About what it does, basically what Arkathorn said.

 

This is actually my first time working with lua so I want to learn as much as I can. I especially want to learn the framework because that seems particularly interesting. Thanks!

This is actually my first time working with lua so I want to learn as much as I can. I especially want to learn the framework because that seems particularly interesting. Thanks!

The way the system with the 'global' function works is by overriding the '__index' function of the metatable of the global table.

 

The global table is the generic overarching table, where non-local fields are declared. In 'modmain.lua' and 'modworldgenmain.lua' it is referenced with 'GLOBAL' (due to those files being run in a special environment, achieved with 'setfenv').

 

Metatables are special tables that can be used to add special functionality to other tables. 'getmetatable(t)' returns the metatable of 't' (if any), and 'setmetatable(t,mt)' sets 'mt' as the metatable of 't'.

 

The most common (as far as I can tell) use of metatables is the '__index' function. When a nil field is referenced in a table, it will call it's metatable's '__index' function, if there is one (The first argument is the table, the second the key value). The return value of the function will be returned to the accessor.

 

The '__index' function can be bypassed by using 'rawget(t,k)'.

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...