modmain里面的代码: local function randomDo(rate, func, ...) local num = math.random(1, 100) if rate <= num then func(...) end end local function updateAtk(inst) inst.components.combat.damagemultiplier = 1 + inst.atk_level / 100 inst.components.hunger:SetRate(TUNING.WILSON_HUNGER_RATE * (1 + inst.atk_level / 200)) end local function updateSanity(inst) local sanity_percent = inst.components.sanity:GetPercent() inst.components.sanity.max = math.ceil (200 + inst.sanity_level * 1.5) inst.components.sanity:SetPercent(sanity_percent) end local function updateSpeed(inst) inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * (1 + inst.speed_level / 200)) inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * (1 + inst.speed_level / 200)) end local function updateHealth(inst) local health_percent = inst.components.health:GetPercent() inst.components.health.maxhealth = math.ceil (150 + inst.health_level * 1.5) inst.components.health:SetPercent(health_percent) end local function updateHunger(inst) local hunger_percent = inst.components.hunger:GetPercent() inst.components.hunger.max = math.ceil (150 + inst.hunger_level * 1.5) inst.components.hunger:SetPercent(hunger_percent) end local function atkLevelUp(inst) if inst.atk_level >= 100 then inst.components.talker:Say('power max!') else inst.atk_level = inst.atk_level + 1 inst.SoundEmitter:PlaySound("dontstarve/characters/wx78/levelup") end updateAtk(inst) end local function sanityLevelUp(inst) if inst.sanity_level >= 100 then inst.components.talker:Say('sanity max!') else inst.sanity_level = inst.sanity_level + 1 inst.HUD.controls.status.brain:ScaleTo(1.3,1,.7) end updateSanity(inst) end local function speedLevelUp(inst) if inst.speed_level >= 100 then inst.components.talker:Say('speed max!') else inst.components.talker:Say('speed up!'..(inst.speed_level)) inst.speed_level = inst.speed_level + 1 end updateSpeed(inst) end local function healthLevelUp(inst) if inst.health_level >= 100 then inst.components.talker:Say('health max!') else inst.health_level = inst.health_level + 1 inst.HUD.controls.status.heart:ScaleTo(1.3,1,.7) end updateHealth(inst) end local function hungerLevelUp(inst) if inst.hunger_level >= 100 then inst.components.talker:Say('stomach max!') else inst.hunger_level = inst.hunger_level + 1 inst.HUD.controls.status.stomach:ScaleTo(1.3,1,.7) end updateHunger(inst) end local function onEat(inst, food) if food then --怪物肉,10%几率升级攻击力 if food.prefab == "monstermeat" then randomDo(10, atkLevelUp, inst) --怪物千层饼,50%几率升级攻击力 elseif food.prefab == "monstermonsterlasagna" then randomDo(50, atkLevelUp, inst) --下略 elseif food.prefab == "fruitmedley" or food.prefab == "jammypreserves" then randomDo(5, sanityLevelUp, inst) elseif food.prefab == "butterflymuffin" then randomDo(30, sanityLevelUp, inst) elseif food.prefab == "stuffedeggplant" or food.prefab == "pumpkincookie" then randomDo(50, sanityLevelUp, inst) elseif food.prefab == "frogglebunwich" then randomDo(20, speedLevelUp, inst) elseif food.prefab == "turkeydinner" then randomDo(30, speedLevelUp, inst) elseif food.prefab == "fishtacos" then randomDo(50, speedLevelUp, inst) elseif food.prefab == "dragonpie" then randomDo(80, healthLevelUp, inst) elseif food.prefab == 'waffles' then randomDo(80, healthLevelUp, inst) elseif food.prefab == 'kabobs' then randomDo(10, hungerLevelUp, inst) elseif food.prefab == 'honeynuggets' then randomDo(30, hungerLevelUp, inst) end end end --刷新所有属性 local function applyupgrades(inst) updateHunger(inst) updateHealth(inst) updateSpeed(inst) updateSanity(inst) updateAtk(inst) end --预加载时,读取存档数据里面的 local function onpreload(inst, data) if data then --未使用该mod的旧档里没有等级数据,如果是旧档,则等级设置为0 inst.hunger_level = data.hunger_level or 0 inst.health_level = data.health_level or 0 inst.sanity_level = data.sanity_level or 0 inst.atk_level = data.atk_level or 0 inst.speed_level = data.speed_level or 0 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.currenthealth = 30 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) end end --存档时将人物的各项等级保存下来 local function onsave(inst, data) data.hunger_level = inst.hunger_level data.health_level = inst.health_level data.sanity_level = inst.sanity_level data.atk_level = inst.atk_level data.speed_level = inst.speed_level end local function plus(inst) --初始化各项等级为0 inst.hunger_level = 0 inst.health_level = 0 inst.sanity_level = 0 inst.atk_level = 0 inst.speed_level = 0 --设置吃东西后调用函数,根据食物不同升级 inst.components.eater:SetOnEatFn(onEat) --设置保存函数,在存档时调用 inst.OnSave = onsave --设置预加载函数,在人物预加载时调用 inst.OnPreLoad = onpreload end AddPlayerPostInit(function(inst) if inst.prefab == "wilson" then plus(inst) end end)
modinfo里面的信息: name = "wilsonplus" description = "sssssssssup wilson!" author = "Jing" version = "Public preview version" forumthread = "" -- This lets other players know if your mod is out of date, update it to match the current version in the game api_version = 6
AddSimPostInit(function(inst) if inst.prefab == "wodb" then WodbPostInit(inst) end end) 我原本是看到了贴吧里教程上的一个人物mod里面有这样一个函数 于是我想着通过用这种函数给人物附加一些东西进去 但,用这里的AddSimPostInit是不行的 通过做打印测试,发现这里注册的函数,执行时间是在人物加载完了之后 我们的修改往往要改掉人物的OnSave保存和OnPreLoad预加载函数 所以进了档了才调用到这个地方,那么加载时用的是没有修改前的系统的函数,简单地说,就是保存之后再打开,没有读取到我们存好的数据