Jump to content

Recommended Posts

I am making a character mod where every day it chooses a random transformation. I know the basics of making a transformation, but i don't know how to randomly choose between them.

Here is my character prefab code.

local MakePlayerCharacter = require "prefabs/player_common"



local assets = {
    Asset("SCRIPT", "scripts/prefabs/player_common.lua"),
}
local prefabs = {}

-- Custom starting inventory
local start_inv = {
"mandrake",
"mandrake",
"mandrake",
"mandrake",
"mandrake"
}

inst.chance = math.random()
-- When the character is revived from human
local function onbecamehuman(inst)
	-- Set speed when not a ghost (optional)
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "cham_speed_mod", 1)
end

local function onbecameghost(inst)
	-- Remove speed modifier when becoming a ghost
   inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "cham_speed_mod")
end



local function SetRandomColor(inst)
	if TheWorld.state.isday then
	
		
        if inst.chance == .1 then
             inst.PushEvent:SetRed()
        elseif inst.chance == .2 then
             inst.PushEvent:SetBlue()
        elseif inst.chance == .3 then
            inst.PushEvent:SetPurple()
		elseif inst.chance >= .3 then
            inst.PushEvent:SetNormal()
        end
	end
end

local function SetNormal()
	inst.components.health:SetMaxHealth(125)
	inst.components.hunger:SetMax(125)
	inst.components.sanity:SetMax(150)
	inst.components.combat.damagemultiplier = .75
	inst.components.hunger.hungerrate = .75 * TUNING.WILSON_HUNGER_RATE
	inst.AnimState:SetBuild("cham")
end

local function SetRed()
	inst.components.health:SetMaxHealth(75)
	inst.components.hunger:SetMax(150)
	inst.components.sanity:SetMax(100)
	inst.components.temperature.inherentsummerinsulation = 3
    inst.components.combat.damagemultiplier = 3
	inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE
	inst.components.health.fire_damage_scale = 0
	inst.AnimState:SetBuild("cham_red")
end
local function SetBlue()
	inst.components.health:SetMaxHealth(150)
	inst.components.hunger:SetMax(125)
	inst.components.sanity:SetMax(100)
	inst.components.temperature.inherentinsulation = 3
    inst.components.combat.damagemultiplier = 2
	inst.components.hunger.hungerrate = 1.5 * TUNING.WILSON_HUNGER_RATE
	inst.components.temperature.mintemp = ( 5 )
	inst.components.health.fire_damage_scale = 1.5
	inst.AnimState:SetBuild("cham_red")
end
local function SetPurple()
	inst.components.health:SetMaxHealth(75)
	inst.components.hunger:SetMax(125)
	inst.components.sanity:SetMax(100)
    inst.components.combat.damagemultiplier = 3
	inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE
	inst.AnimState:SetBuild("cham_red")
end
local function SetBlack()
	inst.components.health:SetMaxHealth(60)
	inst.components.hunger:SetMax(100)
	inst.components.sanity:SetMax(100)
    inst.components.combat.damagemultiplier = .15
	inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE
	inst.AnimState:SetBuild("cham_red")

end


local function onload(inst)
    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
    inst:ListenForEvent("ms_becameghost", onbecameghost)

    if inst:HasTag("playerghost") then
        onbecameghost(inst)
    else
        onbecamehuman(inst)
    end
end



local common_postinit = function(inst) 
	inst.MiniMapEntity:SetIcon( "cham.tex" )
end

local master_postinit = function(inst)
	-- choose which sounds this character will play
	inst.soundsname = "maxwell"
	
	inst:ListenForEvent( "daytime", SetRandomColor(inst), GetWorld())
	-- Stats	
	inst.components.health:SetMaxHealth(125)
	inst.components.hunger:SetMax(125)
	inst.components.sanity:SetMax(150)
    inst:AddTag("monster")	
	-- Damage multiplier (optional)
    inst.components.combat.damagemultiplier = .75
	inst.components.locomotor.triggerscreep = false
	-- Hunger rate (optional)
	inst.components.hunger.hungerrate = .75 * TUNING.WILSON_HUNGER_RATE
	
	inst.OnLoad = onload
    inst.OnNewSpawn = onload
	
end


return MakePlayerCharacter("cham", prefabs, assets, common_postinit, master_postinit, start_inv)

 

Edited by MikeyBrikey

Lua MathLibrary Tutorial

Scroll down to math.random()

local stateToUse = Random(1, yourNumberOfStates)

if stateToUse == 1 then
	-- blabla
elseif stateToUse == 2 then
	-- blabla
elseif .........

If you want to have different chances for each of the states, then you have to look at distribution algorithms.

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
×
  • Create New...