Jump to content

HCaracter with lvl system


Recommended Posts

It's more complicated than just copying some of WX-78's code. Electrical Doodads will also need to be made edible by your character. For testing purposes, start off with levelling via berries or something, then just make your character start with a pile of them.

 

so basically in character.lua:
 

local start_inv = {"berries","berries","berries","berries","berries",}local function upgrade(inst, food)    local health_percent = inst.components.health:GetPercent()    local hp = inst.components.health.maxhealth    if inst.components.eater and food.prefab == "berries" then        if inst.components.health.maxhealth <= 200 then        inst.components.health.maxhealth = math.ceil(hp + 10)        inst.components.health:SetPercent(health_percent)    endend
 
Then in master_postinit add:
inst.components.eater:SetOnEatFn(upgrade)

That should get you started!

 

EDIT: turns out adding doodads as edible isn't terribly hard. In modmain.lua:
 

function SetEat(prefab,health,hunger,sanity,tip)AddPrefabPostInit(prefab,function(inst)if not inst.components.edible theninst:AddComponent("edible")endinst.components.edible.healthvalue = healthinst.components.edible.hungervalue = hungerinst.components.edible.sanityvalue = sanityif tip theninst.components.edible.foodtype = tipendend)endSetEat("transistor",0,1,0,FOODTYPE.GENERIC)

The problem, then, is that everybody can eat them...

 

Edited by StarmanUltra
Link to comment
Share on other sites

-- hunger, health, sanity-- based on level, WX gets new stats-- level is limited by 15local function applyupgrades(inst)	-- max level is 15    local max_upgrades = 15	-- if level is higher than max, then level equals max    inst.level = math.min(inst.level, max_upgrades)	-- save percents because new max stats will keep current values    local hunger_percent = inst.components.hunger:GetPercent()    local health_percent = inst.components.health:GetPercent()    local sanity_percent = inst.components.sanity:GetPercent()	-- adjust max stats    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)	-- restore old percents    inst.components.hunger:SetPercent(hunger_percent)    inst.components.health:SetPercent(health_percent)    inst.components.sanity:SetPercent(sanity_percent)		-- example: from 50 / 100 to 100 / 200	-- NOT: from 50 / 100 to 50 / 200; this is why we save percentend-- when eatinglocal function oneat(inst, food)	-- if food is gears    if food and food.components.edible and food.components.edible.foodtype == FOODTYPE.GEARS then        -- raise level by one        inst.level = inst.level + 1		-- adjust stats        applyupgrades(inst)    endend-- we preload values so level gets restoredlocal function onpreload(inst, data)	-- if there's data, and a level saved    if data ~= nil and data.level ~= nil then		-- our level is the level saved        inst.level = data.level		-- we apply upgrades to the level saved        applyupgrades(inst)        -- re-set these from the save data, because of load-order clipping issues        if data.health and data.health.health then inst.components.health:SetCurrentHealth(data.health.health) end        if data.hunger and data.hunger.hunger then inst.components.hunger.current = data.hunger.hunger end        if data.sanity and data.sanity.current then inst.components.sanity.current = data.sanity.current end        inst.components.health:DoDelta(0)        inst.components.hunger:DoDelta(0)        inst.components.sanity:DoDelta(0)    endend-- we save the level valuelocal function onsave(inst, data)    data.level = inst.level > 0 and inst.level or nilend-- function ran in the host when the prefab is generated-- even if it's new in the server or reloading after logging offlocal function master_postinit(inst)	-- we start at 0, if we had a level saved, we cover that in preload	inst.level = 0	-- we can eat gears    inst.components.eater:SetCanEatGears()	-- we apply our level up function when eating    inst.components.eater:SetOnEatFn(oneat)	-- we set up basic values (upgrades for level 0)    applyupgrades(inst)	-- we have to save the level    inst.OnSave = onsave	-- we have to load the level    inst.OnPreLoad = onpreloadend

WX-78's level system.

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