[item coding request] custom book for custom character


MrHazard

Recommended Posts

hello there, I'm no lua coder,but i make art which means i need some help from  the more experienced folks here with code.

 

i'd like to give my character a custom book similar to maxwells book, this book will consume Niter and 25 sanity per use to spawn gunpowder right next to the player.

 

seems simple enough right? well, not for me...

 

i want it to be based around the waxwelljournal anim files for easy texture swapping.

 

 

 

what do i need this book for?

this(stats are just placeholders):

https://d1zqrvc06emslq.cloudfront.net/media/originals/feedpost/53/68/4b/99946e1fd4e7b33c5d828cfd4efd3c6a.jpg

 

 

hope someone can help me, i want to release this character mod soon.

Link to comment
Share on other sites

@MrHazard, I'm not very experienced in making characters/prefabs (yet), but I think you could probably just copy most of the code from the existing waxwelljournal prefab, and change just the things you want.

"..\scripts\prefabs\waxwelljournal.lua" contains the original code.

 

Theoretical example:

 

Something like this in your modmain.lua to determine the fuel/sanity costs:

GLOBAL.TUNING.YOUR_PREFAB_SANITY_PENALTY = 25GLOBAL.TUNING.YOUR_PREFAB_FUEL_COST = 1

In your custom prefab file ("..\mods\your_mod_name\scripts\prefabs\your_prefab_name.lua):

local assets =	{		Asset("ANIM", "anim/book_maxwell.zip"),	}local prefabs =	{		"gunpowder",		"waxwell_book_fx"	}--local function doeffects(inst, pos)--    SpawnPrefab("statue_transition").Transform:SetPosition(pos:Get())--    SpawnPrefab("statue_transition_2").Transform:SetPosition(pos:Get())--endlocal function canread(inst)	return (inst.components.sanity:GetMaxSanity() >= TUNING.YOUR_PREFAB_SANITY_PENALTY)endlocal function onread(inst, reader)	--Check sanity	if not canread(reader) then		if reader.components.talker then			reader.components.talker:Say(GetString(reader.prefab, "ANNOUNCE_NOSANITY"))			return true		end	end	--Check reagent	if not reader.components.inventory:Has("nitre", TUNING.YOUR_PREFAB_FUEL_COST) then		if reader.components.talker then			reader.components.talker:Say(GetString(reader.prefab, "ANNOUNCE_NOFUEL"))			return true		end	end	reader.components.inventory:ConsumeByName("nitre", TUNING.YOUR_PREFAB_FUEL_COST)	--Ok you had everything. Make the image.	local theta = math.random() * 2 * PI	local pt = inst:GetPosition()	local radius = math.random(3, 6)	local offset = FindWalkableOffset(pt, theta, radius, 12, true)	if offset then		local image = SpawnPrefab("gunpowder")		local pos = pt + offset		image.Transform:SetPosition(pos:Get())		-- doeffects(inst, pos)		-- image.components.follower:SetLeader(reader)		-- reader.components.health:DoDelta(-TUNING.SHADOWWAXWELL_HEALTH_COST)		reader.components.sanity:RecalculatePenalty()		-- inst.SoundEmitter:PlaySound("dontstarve/maxwell/shadowmax_appear")		return true	endendlocal function fn()	local inst = CreateEntity()	local trans = inst.entity:AddTransform()	local anim = inst.entity:AddAnimState()	local sound = inst.entity:AddSoundEmitter()	anim:SetBank("book_maxwell")	anim:SetBuild("book_maxwell")	anim:PlayAnimation("idle")	MakeInventoryPhysics(inst)	inst:AddComponent("inventoryitem")	inst:AddComponent("characterspecific")	inst.components.characterspecific:SetOwner("your_character")	-----------------------------------	inst:AddComponent("inspectable")	inst:AddComponent("book")	inst.components.book.onread = onread	MakeSmallBurnable(inst)	MakeSmallPropagator(inst)	return instendreturn Prefab("common/your_prefab", fn, assets)

Remember to change "your_prefab" and "your_character" according to what you want to name them!

 

Link to comment
Share on other sites

i did this, wrote what you wanted me to write in modmain.lua, listed assets in modmain, listed the book in modmain too, but the game keeps freezing on startup...

 

heres the code:

local assets =    {        Asset("ANIM", "anim/book_maxwell.zip"),    } local prefabs =    {        "gunpowder",        "waxwell_book_fx"    } --local function doeffects(inst, pos)--    SpawnPrefab("statue_transition").Transform:SetPosition(pos:Get())--    SpawnPrefab("statue_transition_2").Transform:SetPosition(pos:Get())--end local function canread(inst)    return (inst.components.sanity:GetMaxSanity() >= TUNING.BOOK_EXPLOSIVE_SANITY_PENALTY)end local function onread(inst, reader)     --Check sanity    if not canread(reader) then        if reader.components.talker then            reader.components.talker:Say(GetString(reader.prefab, "ANNOUNCE_NOSANITY"))            return true        end    end     --Check reagent    if not reader.components.inventory:Has("nitre", TUNING.BOOK_EXPLOSIVE_FUEL_COST) then        if reader.components.talker then            reader.components.talker:Say(GetString(reader.prefab, "ANNOUNCE_NOFUEL"))            return true        end    end     reader.components.inventory:ConsumeByName("nitre", TUNING.BOOK_EXPLOSIVE_FUEL_COST)     --Ok you had everything. Make the image.    local theta = math.random() * 2 * PI    local pt = inst:GetPosition()    local radius = math.random(3, 6)    local offset = FindWalkableOffset(pt, theta, radius, 12, true)    if offset then        local image = SpawnPrefab("gunpowder")        local pos = pt + offset        image.Transform:SetPosition(pos:Get())        -- doeffects(inst, pos)        -- image.components.follower:SetLeader(reader)        -- reader.components.health:DoDelta(-TUNING.SHADOWWAXWELL_HEALTH_COST)        reader.components.sanity:RecalculatePenalty()        -- inst.SoundEmitter:PlaySound("dontstarve/maxwell/shadowmax_appear")        return true    endend   local function fn()    local inst = CreateEntity()    local trans = inst.entity:AddTransform()    local anim = inst.entity:AddAnimState()    local sound = inst.entity:AddSoundEmitter()     anim:SetBank("book_maxwell")    anim:SetBuild("book_maxwell")    anim:PlayAnimation("idle")     MakeInventoryPhysics(inst)     inst:AddComponent("inventoryitem")    inst.components.inventoryitem.atlasname = "images/inventoryimages/book_explosive.xml"     inst:AddComponent("characterspecific")    inst.components.characterspecific:SetOwner("mr_hazard")     -----------------------------------    inst:AddComponent("inspectable")    inst:AddComponent("book")    inst.components.book.onread = onread     MakeSmallBurnable(inst)    MakeSmallPropagator(inst)     return instend return Prefab("common/book_explosive", fn, assets)
Link to comment
Share on other sites

@MrHazard, please upload your log.txt file (immediately after the crash, because it resets when you re-run the game). It contains information on why the game crashed.
 
Also, if you use this in your post (without the spaces), it will give a notification to the member, depending on their notification settings:

[ member='member_name_here' ]

Alternately, you can select a portion of someones post and click "mention". Not sure if there's something like that on mobile version of the site, though.

 

Sometimes posts get lost down in the forum when more posts come in, this is a good way to let someone know that you are replied.

 

 

Edit: Whoops. I think you were supposed to change this at the top. I forgot to do it in my code above.

local prefabs =

{
-- "shadowwaxwell",
"your_prefab",
"gunpowder",
}

 

Edit2: Actually, I apparently only forgot to add in "your_prefab", but it still needs to be fixed.

 

Edit3: Scratch the last 2 edits. I mixed up the prefab names in my head while looking through waxwelljournal.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.