Jump to content

Lightning and Gears Upgrade (Scripts)


Recommended Posts

I wanted to make a script for my character to be able to level up not only by gears but lightning strikes as well. Anyone got a idea of how to make that happen. I'm still kind of new to this scripting thing. I know that robot gain level though just eating gears.

Screenshot_7.png

Edited by ProfessorQ
Link to comment
Share on other sites

If you are copying the gear leveling thing from wx-78

you'll see that he also has a onlightningstrike function as well.

To enable them you have to use the components functions in your master_postinit

inst.components.eater:SetOnEatFn(oneat)

inst.components.playerlightningtarget:SetOnStrikeFn(onlightingstrike)

 

The level itself is just a variable, you can see it increases in the oneat function

local function oneat(inst, food)
    if food and food.components.edible and food.components.edible.foodtype == FOODTYPE.GEARS then
        --give an upgrade!
        inst.level = inst.level + 1
        applyupgrades(inst) 
        inst.SoundEmitter:PlaySound("dontstarve/characters/wx78/levelup")
    end
end

so to make the onlightningstrike function level up as well.

local function onlightingstrike(inst)
	inst.level = inst.level + 1
	applyupgrades(inst)
end

I am simplifying it by omitting other stuff wx-78's functions do

Wx-78 applyupgrades via a local function

local function applyupgrades(inst)
    local max_upgrades = 15
    inst.level = math.min(inst.level, max_upgrades)

	--Do your stuff that you want your levels to do. E.g. wx78's stuff below
    local hunger_percent = inst.components.hunger:GetPercent()
    local health_percent = inst.components.health:GetPercent()
    local sanity_percent = inst.components.sanity:GetPercent()

    inst.components.hunger.max = math.ceil(TUNING.WX78_MIN_HUNGER + inst.level * (TUNING.WX78_MAX_HUNGER - TUNING.WX78_MIN_HUNGER) / max_upgrades)
    inst.components.health.maxhealth = math.ceil(TUNING.WX78_MIN_HEALTH + inst.level * (TUNING.WX78_MAX_HEALTH - TUNING.WX78_MIN_HEALTH) / max_upgrades)
    inst.components.sanity.max = math.ceil(TUNING.WX78_MIN_SANITY + inst.level * (TUNING.WX78_MAX_SANITY - TUNING.WX78_MIN_SANITY) / max_upgrades)

    inst.components.hunger:SetPercent(hunger_percent)
    inst.components.health:SetPercent(health_percent)
    inst.components.sanity:SetPercent(sanity_percent)
end

Last but not least you want to make sure you save your levels when you disconnect and probably reset it when you die.

Check the wx78.lua file for more info. But this should get you started.

Link to comment
Share on other sites

9 hours ago, IronHunter said:

Assuming you want your character to have a range attack like this: 

Maybe check it out? They even use the lightning projectile.

Yeah but it comfuse me. 

 

local myaction = GLOBAL.Action(7, true, true, 7)myaction.str = "Cast Lightning Ball"myaction.id = "CASTLIGHTNING"myaction.fn = function(act)	act.doer.lightincooldown = true	act.doer:DoTaskInTime(10, function() act.doer.lightincooldown = nil end)    act.doer.AnimState:PlayAnimation("throw")    act.doer:DoTaskInTime(7*GLOBAL.FRAMES, function()        local projectile = GLOBAL.SpawnPrefab("bishop_charge")        projectile:AddComponent("weapon")        projectile.Transform:SetPosition(act.doer.Transform:GetWorldPosition())        projectile.components.projectile:Throw(act.doer, act.target)    end)    return trueendAddAction(myaction)local function editPAP(inst)	if not (inst.prefab == "wally") then		return	end	local self = inst.components.playeractionpicker	local _GRCA = self.GetRightClickActions	self.GetRightClickActions = function(self, target_ent, position)		local actions = _GRCA(self, target_ent, position)		if not self.inst.lightincooldown and target_ent and target_ent.components.health and not (self.inst == target_ent) then			actions = self:SortActionList({ GLOBAL.ACTIONS.CASTLIGHTNING }, target_ent)		end		return actions	endendAddSimPostInit(editPAP)

This I don't know where to put it.

Link to comment
Share on other sites

Here is the code for the projectile attack from that thread, you can paste it into your modmain.lua after everything else in the file.

Spoiler

local Action = GLOBAL.Action
local ActionHandler = GLOBAL.ActionHandler
local ACTIONS = GLOBAL.ACTIONS
local FRAMES = GLOBAL.FRAMES

local CASTBOLT = AddAction("CASTBOLT", "Cast Bolt", function(act)
	act.doer.lightincooldown = true
	act.doer:DoTaskInTime(10, function() 
		act.doer.lightincooldown = nil
		local sparks = GLOBAL.SpawnPrefab("sparks").Transform:SetPosition(act.doer.Transform:GetWorldPosition())
	end)
    act.doer.AnimState:PlayAnimation("throw")
    act.doer:DoTaskInTime(7*GLOBAL.FRAMES, function()
		local projectile = GLOBAL.SpawnPrefab("bishop_charge")
        projectile:AddComponent("weapon")
        projectile.Transform:SetPosition(act.doer.Transform:GetWorldPosition())
        projectile.components.projectile:Throw(act.doer, act.target)
	end)
	return true
end)
CASTBOLT.rmb = true
CASTBOLT.distance = 7
AddStategraphActionHandler("wilson", GLOBAL.ActionHandler(GLOBAL.ACTIONS.CASTBOLT, "throw"))
AddPrefabPostInit("yourprefabnamehere", function(inst)
	inst:DoTaskInTime(0, function()
		local self = inst.components.playeractionpicker
		self.rightclickoverride = function(inst, target, position)
		local actions = {}
		if not self.inst.lightincooldown and target and target.replica.health and not (inst == target) then
			table.insert(actions, GLOBAL.ACTIONS.CASTBOLT)
		end
		return self:SortActionList(actions, target)
		end
	end)
end)

 

As you get more comfortable with modding you can look into ways of making the modmain.lua more organized, such as having a seperate .lua file just for stuff like this that is imported into the modmain.lua

e.g. keeping a lot of variables separate for organization

modimport("tuning.lua")

Cheers, hopefully this should help get your mod going

Link to comment
Share on other sites

Honestly after some testing, it seems like the code from that thread is broken with the current version of DST.

Unfortunately this is currently beyond my current knowledge of DST engine, unless somebody else can pitch in and help figure out the problem. I advise to start with just making a custom weapon/item that will accomplish the result you are trying to do for now.

Link to comment
Share on other sites

On 4/19/2017 at 8:38 PM, IronHunter said:

Honestly after some testing, it seems like the code from that thread is broken with the current version of DST.

Unfortunately this is currently beyond my current knowledge of DST engine, unless somebody else can pitch in and help figure out the problem. I advise to start with just making a custom weapon/item that will accomplish the result you are trying to do for now.

How is that done?

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