Sign in to follow this  
Lucemodok

[TUTORIAL] How to Add Talking Fonts to the Game

Recommended Posts

Cunning fox    807

The loading solution will crash if you leave the server/reload the current game, because the font needs to be loaded before mod assets are loaded 
My solution:

local env = env
_G.setfenv(1, _G)

TALKINGFONT_WAYNE = "talkingfont_wayne"

env.AddSimPostInit(function()
	TheSim:UnloadFont(TALKINGFONT_WAYNE)
	TheSim:UnloadPrefabs({"wayne_fonts"})

	local Assets = {
		Asset("FONT", resolvefilepath("fonts/talkingfont_wayne.zip")),
	}
	local FontsPrefab = Prefab("wayne_fonts", function() return CreateEntity() end, Assets)
	RegisterPrefabs(FontsPrefab)
	TheSim:LoadPrefabs({"wayne_fonts"})
	TheSim:LoadFont(resolvefilepath("fonts/talkingfont_wayne.zip"), TALKINGFONT_WAYNE)
end)

It's pretty hacky, so maybe there's other way to resolve reload crash? I've tried to add loading/unloading new font to global functions LoadFonts/UnloadFonts (see mainfunctions.lua), but then the font never even gets loaded.
Maybe @zarklord_klei knows a better way of doing it? :spidercowers:

Edited by Cunning fox
  • Like 1

Share this post


Link to post
Share on other sites
Cunning fox    807
2 minutes ago, TheSkylarr said:

What exactly does resolvefilepath() do? I've seen it thrown around, along with softresolvefilepath(), and I'm clueless.

Resolves relative file path into absolute path
This is used if you add asset that is not from the game, so you need to tell the engine where it is stored
Example: resolvefilepath("anim/poop.zip") will return "../../mods/workshop-00000/anim/poop.zip"

Edited by Cunning fox

Share this post


Link to post
Share on other sites
Garamonde    67

Yes, I used this font code and my mod crashes the game upon world regen, which is a big problem because me and a friend I play with regenerate our worlds all the time. I reeeally hope someone comes up with a cleaner solution! Also, inserting that "hacky" code (and changing all the names to my character of course) gained this crash upon trying to create a new world:

image.png.1953cb8c0f816e98610e303803b0d828.png

@Cunning fox

Edited by Garamonde
Inserted a few missing words.

Share this post


Link to post
Share on other sites
Cunning fox    807
7 hours ago, Garamonde said:

Yes, I used this font code and my mod crashes the game upon world regen, which is a big problem because me and a friend I play with regenerate our worlds all the time. I reeeally hope someone comes up with a cleaner solution! Also, inserting that "hacky" code (and changing all the names to my character of course) gained this crash upon trying to create a new world:

image.png.1953cb8c0f816e98610e303803b0d828.png

@Cunning fox

Add _G = GLOBAL 

7 hours ago, Garamonde said:

Yes, I used this font code and my mod crashes the game upon world regen, which is a big problem because me and a friend I play with regenerate our worlds all the time. I reeeally hope someone comes up with a cleaner solution! Also, inserting that "hacky" code (and changing all the names to my character of course) gained this crash upon trying to create a new world:

image.png.1953cb8c0f816e98610e303803b0d828.png

@Cunning fox

The code I used changes mods's env to a global one, so you shouldn't use it if you're not experienced enough. Solution without changing env is:
 

local _G = GLOBAL
local TheSim = _G.TheSim

_G.TALKINGFONT_WAYNE = "talkingfont_wayne"

AddSimPostInit(function()
	TheSim:UnloadFont(_G.TALKINGFONT_WAYNE)
	TheSim:UnloadPrefabs({"wayne_fonts"})

	local Assets = {
		Asset("FONT", _G.resolvefilepath("fonts/talkingfont_wayne.zip")),
	}
	local FontsPrefab = _G.Prefab("wayne_fonts", function() return _G.CreateEntity() end, Assets)
	_G.RegisterPrefabs(FontsPrefab)
	TheSim:LoadPrefabs({"wayne_fonts"})
	TheSim:LoadFont(_G.resolvefilepath("fonts/talkingfont_wayne.zip"), _G.TALKINGFONT_WAYNE)
end)

 

Edited by Cunning fox
  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
On 3/5/2021 at 3:56 AM, Cunning fox said:

Add _G = GLOBAL 

The code I used changes mods's env to a global one, so you shouldn't use it if you're not experienced enough. Solution without changing env is:
 


local _G = GLOBAL
local TheSim = _G.TheSim

_G.TALKINGFONT_WAYNE = "talkingfont_wayne"

AddSimPostInit(function()
	TheSim:UnloadFont(_G.TALKINGFONT_WAYNE)
	TheSim:UnloadPrefabs({"wayne_fonts"})

	local Assets = {
		Asset("FONT", _G.resolvefilepath("fonts/talkingfont_wayne.zip")),
	}
	local FontsPrefab = _G.Prefab("wayne_fonts", function() return _G.CreateEntity() end, Assets)
	_G.RegisterPrefabs(FontsPrefab)
	TheSim:LoadPrefabs({"wayne_fonts"})
	TheSim:LoadFont(_G.resolvefilepath("fonts/talkingfont_wayne.zip"), _G.TALKINGFONT_WAYNE)
end)

 

Is there a certain place it's supposed to go? I tried this and it crashed when going into a cave.

 

Looked into it, the log says that "AddSimPostInit" isn't declared.

Edited by icantevenname

Share this post


Link to post
Share on other sites
iiChriSii    0

I open the font  BMFont and my languague symbols (á à ả ã ạ....) disappeared. is that okay?

 

 

Edited by iiChriSii

Share this post


Link to post
Share on other sites
iiChriSii    0
4 hours ago, iiChriSii said:

I open the font  BMFont and my languague symbols (á à ả ã ạ....) disappeared. is that okay?

 

 

I figured out ;)

Share this post


Link to post
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
Sign in to follow this