Sign in to follow this  
muddykat

Problems with periodic task's

Recommended Posts

muddykat    14

i'm a somewhat experienced Dont starve mod creator unfortunately the same cannot be said about don't starve together, i'm experiencing a problem with converting my mod Bellatrix the realistic vampire into don't starve together. I have managed to get it work but without any of the well "vampiry" stuff, so no burning in sunlight.

 

i got the items to work same with the characters ghost and other bits and bobs to work except the sunlight thing, and that's were the periodic task comes into play.

 

So my sunburn works really simply i have a periodic function that checks the clock and find if its day or not check if its raining and if i have any items equipped with the tag "nosundam" and if the requirements are checked i just do a health delta, but in don't starve together it seems that how i did the event it only seems to occur on the client side of things. Knowing this i'm afraid i'm going to have to make a component or something along those lines, but i'm not sure how i would implement something like that though.

 

so that's what the problem i'm facing is there anyway to get around this or will i have to make a component or something else like that. i'm 95% positive that what i want to do is possible in some way shape or form because the character wx87 takes damage in the rain

 

(i have gone through wx87's code and still to no avail)

Share this post


Link to post
Share on other sites
Maris    233

What is "i have any items equipped" in DST? There are many players on the DST server.

 

in modmain.lua:

if TheNet:GetIsServer() then    AddPrefabPostInit("youcharacter",function(inst)        inst:DoPeriodicTask( .......... ) --your stuff with health etc    end)end

Edited by Maris

Share this post


Link to post
Share on other sites
muddykat    14

i get an error stating it doesn't know what TheNet is

 

 

sorry its not checking what the player is wearing the item just adds a tag to the player who has it on called "nosundam"

Edited by muddykat

Share this post


Link to post
Share on other sites
ZupaleX    270

Could you post your code so we can see a bit better how it's done? It would be easier to have a base for discussion than talking about generalities on the DoPeriodicTask, which can be used in a loooot of ways.

 

And I don't get why the hell you would put a AddPrefabPostInit to your character in the modmain, while it is, well, YOUR character so just do this kind of stuff in your character.lua...

Edited by ZupaleX

Share this post


Link to post
Share on other sites
muddykat    14

in both cases in which i put the code in the mod main or the characters.lua but i get the same error that i have never had before 

 

291: attempt to preform arithmetic on local 'dt' (a nil value)

 

from this i already know its trying to access a variable that doesn't exist or is a well nil value

 

i have no clue how i would fix this


o here is the code for what im trying to do now

local function dosundamage(inst, isday)	if not TheWorld.state.israining then		if not inst:HasTag("nosundam") then			if isday then		inst.components.health:DoFireDamage(.05, false, true)			end        end	endendlocal function onbecomevampire(inst)if TheNet:GetIsServer() then    inst:DoPeriodicTask(dosundamage(inst,TheWorld.state.isday),1) --your stuff with health etcend	inst.components.locomotor.walkspeed = 4	inst.components.locomotor.runspeed = 6end

Share this post


Link to post
Share on other sites
ZupaleX    270
 

 

in both cases in which i put the code in the mod main or the characters.lua but i get the same error that i have never had before 

 

291: attempt to preform arithmetic on local 'dt' (a nil value)

 

from this i already know its trying to access a variable that doesn't exist or is a well nil value

 

i have no clue how i would fix this


o here is the code for what im trying to do now

 

local function dosundamage(inst, isday)
    if not TheWorld.state.israining then
        if not inst:HasTag("nosundam") then
            if isday then
        inst.components.health:DoFireDamage(.05, false, true)
            end
end
    end
end

local function onbecomevampire(inst)

if TheNet:GetIsServer() then
inst:DoPeriodicTask(dosundamage(inst,TheWorld.state.isday),1) --your stuff with health etc
end

    inst.components.locomotor.walkspeed = 4
    inst.components.locomotor.runspeed = 6
end

 

 

DoPeriodicTask takes as a first argument the period, and as a second the function. Secondly you cannot call your function like that. You have to do

if TheNet:GetIsServer() then    inst:DoPeriodicTask(1, dosundamage, 0, TheWorld.state.isday)end

or 

if TheNet:GetIsServer() then     inst:DoPeriodicTask(1, function() dosundamage(inst,TheWorld.state.isday) end)end

It's equivalent

 

PS : third optional argument for the DoPeriodiTask is a "delta t" to execute the function. Like if you want to execute the function periodically, but delay it additionnally. This is useful when you want to have a bit of randomness. You then do

inst:DoPeriodicTask(period, function, math.random())

Finally, from 4th to nth arguments, are the potential additional arguments that are past to your function executed periodically (in your case TheWorld.state.blah)

Edited by ZupaleX

Share this post


Link to post
Share on other sites
Maris    233

 

 
 
if TheNet:GetIsServer() then    inst:DoPeriodicTask(1, dosundamage, 0, TheWorld.state.isday)end

or 

if TheNet:GetIsServer() then     inst:DoPeriodicTask(1, function() dosundamage(inst,TheWorld.state.isday) end)end

It's equivalent

 

Actually it's not equivavlent. Second example is better.

Share this post


Link to post
Share on other sites
Maris    233

@muddykat, if you write in youcharacter.lua, use

TheWorld.ismastersim

​instead of 

TheNet:GetIsServer()

​It's better for perfomance.

TheNet:GetIsServer() works fine even if world is not created, but it takes more cpu load.

​All prefabs are created after creation of the world, so it's better to use TheWorld.ismastersim in AddPrefabPostInit function and in prefab's lua file.

Share this post


Link to post
Share on other sites
Maris    233
Would you care to develop?

Well, it is obvious.

Let's look at this example:

if TheNet:GetIsServer() then    inst:DoPeriodicTask(1, dosundamage, 0, TheWorld.state.isday)end

​This code will take VALUE of TheWorld.state.isday variable and will never check it again.

 

And in second example the code will check this variable every time. So second code is much better than first. Right?

  • Potato Cup 1

Share this post


Link to post
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
Sign in to follow this