Jump to content

[SOLVED] Need Help With Unusual Behavior in Console Command


Recommended Posts

Hey guys not sure if anyone is around to be able to help out with these types of things or not but I'm going to give it a shot anyhow.. So I am bran new to messing around with Don't Starve Together (pc version).  I do have some programming experience in .NET (c#) but somehow that just doesn't seem to be helping me lol.  Anyway enough of my rant and I will show you what the problem is.  

I am trying to create a console command that loops through all the players and checks if they have :HasTag("playerghost").  If they do it revives them and sets their health, hunger and sanity all to full.  The problem is that not all of them are working.  The hunger and health both work but the sanity isn't changing.  If I run the command in the console on its own it works find or if I put it on a separate function and call it via the console that also works.  Not sure what I am doing wrong but if someone could please help I would greatly appreciate it.  I also have a question about some syntax that I see used quite heavily in the consolecommands.lua file that reads SuUsed(c_setsanity(var)) etc... obviously with different use cases.. Can someone explain to me what this is doing as I feel this may have something to do with the reason my function is working properly.  I have pasted my code below:

function revive()
    for k,v in pairs(AllPlayers) do
		if v:HasTag("playerghost") then
			v:PushEvent("respawnfromghost")
			v.components.health:SetPercent(1)
			v.components.hunger:SetPercent(1)
			v.components.sanity:SetPercent(1)
		else
			print(v.name .. " Isn't Dead")
		end
    end
end

function sane()
    for k,v in pairs(AllPlayers) do
		v.components.sanity:SetPercent(1)
    end
end

 

Edited by Silisiban
Link to comment
Share on other sites

Player death and revive events are pretty complicated things in the game code. Maybe devs will do somethnig with it, but now it is a hardcore part of modding. Anyway:
1) Objects collect events at the next frame the event was pushed. So, when you do: PushEvent("respawnfromghost"), the player is still dead and don't know about the resurrection event. But the player has health, hunger and sanity components (it's wierd, but "after-resurrect" values are setted when the player dies). For some reason the sanity component for the ghost form has the "ignore" flag, so it ignores any attempts to change the sanity value. Thats why "v.components.sanity:SetPercent(1)" doesn't work. So, I think there are two ways:

  1. Do it with a small delay with DoTaskInTime, and it should be longer then one frame (because "respawnfromghost" pushes another events)
  2. Do something like this:
    local prev = v.components.sanity.ignore
    v.components.sanity.ignore = false
    v.components.sanity:SetPercent(1)
    v.components.sanity.ignore = prev

     

2) SuUsed is a some kind of a log(?), I don't think it can be useful.

Link to comment
Share on other sites

Can you please elaborate a little more.. I am pretty new to LUA and maybe I'm missing something here.  I'm assuming "DoTaskInTime" is a LUA function or specific to DST API.  Also, in your code I'm seeing that your setting the prev var to ignoring the players sanity then setting the value but then also setting it back to ignore.  Wouldn't this then not allow the player to have sanity drain?  

Link to comment
Share on other sites

1 hour ago, Silisiban said:

 I'm assuming "DoTaskInTime" is a LUA function or specific to DST API.  

DoTaskInTime is the entity function:

object:DoTaskInTime(delay, fn(object, somedata), somedata) --delay in seconds; somedata isn't necessary

So you need something like this:

v:PushEvent("respawnfromghost")
v:DoTaskInTime(1, function(player)
	player.components.health:SetPercent(1)
	player.components.hunger:SetPercent(1)
	player.components.sanity:SetPercent(1)
end)

 

55 minutes ago, Silisiban said:

Also, in your code I'm seeing that your setting the prev var to ignoring the players sanity then setting the value but then also setting it back to ignore.  Wouldn't this then not allow the player to have sanity drain?  

Just in case (devs had a reason to do it), the flag will be setted to the correct value by the game later.

Link to comment
Share on other sites

First off I would like to thank you for the time you have taken to help me understand all of this.  I have one more question for you so that I don't need to keep bothering you.  Do you know where I can get any documentation on this API.  Like DoTaskInTime etc... or a list of tags and all that good stuff?

Link to comment
Share on other sites

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
 Share

×
  • Create New...