Jump to content

Recommended Posts

Hi I'm new to both Lua and Modding. I've done Lua 101, and have basic understanding on how metatable is used to support OOP. But the modding API is rather mysterious.

I've read some code, and I have some newbie questions how someone can clarify one of the following:

1. What's `Class()`? Does it return a table? Is it DST's way of constructing a class?

2. Why is GLOBAL.ThePlayer nil? Is ThePlayer a pointer to your character? Does it contain detail such as player's location, temperature, hunger hp sanity?

3. What's inst? I see it everywhere in code, but don't get the context. Is it a global variable? Or does it just mean "instance"?

4. How to use `DoPeriodicTask`? I tried the code below in `modmain.lua`, but the game refuses to load it

local myFunction = function(inst)
	-- Do something
end

inst:DoPeriodicTask(1.0, myFunction)

Any help is appreciated. Thanks for reading.

@goatt

"Class" is a wrapper function that creates tables with object-like properties of it such as inheritance.  Find its definition in: scripts/class.lua

GLOBAL.ThePlayer, when used in modmain.lua, points to the current client's player entity if it exists.  It only exists on a client (not a server) when the client is in-game and spawned in the world.  The data the player entity has on the server is absolute, and only the important networked bits the client knows about.  To access the information on the server is straight forward by poking the entity's components table.  `inst.components.health:GetPercent()` for example.

`inst` is used when referring to an instance of something.  It's an arbitrary name and could have been anything, but the game likes to use it for conformity.  It's very context sensitive to whatever is using it.  For example, a component's self.inst is the owner of the component.  Thus inst.components.somecomponent == somecomponent.inst.  Each inst gets its own somecomponent, so inst1.components.somecomponent ~= inst2.components.somecomponent.

The periodic task is ran on an entity.

  • Like 3

@CarlZalph Thank you for the explanation. A further question, where can I find scripts folder? It doesn't seem to locate anywhere in C:\Program Files (x86)\Steam\steamapps\common\Don't Starve Together.

8 hours ago, goatt said:

@CarlZalph If I want to get my coordinate from Client, the code should be something like this?


GLOBAL.ThePlayer.components.location:Coordinates()

 

First, check if GLOBAL.ThePlayer isn't nil, then:

local x, y, z = GLOBAL.ThePlayer.Transform:GetWorldPosition()

 

  • Like 3

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