Skip to content
ryobg edited this page Feb 15, 2018 · 8 revisions

Setting up

Motivating Example

Note that this is not complete Begining->Got-working-mod tutorial.

It is known that the racemenu command, command that changes Players race also resets his skills to their defaults. Thus before race change, we can backup these skills by writing them into a file and later read them back restoring their actual values.

A high-level part of a script looks like:

function changeRace(objectreference plr, Race rc)
    backupSkills(plr)
    plr.SetRace(rc)
    restoreSkills(plr)
endfunction

Now let assume we have a file SkyrimDir/Data/SkillList.json containing following lines (not going to list all skills, but note that the last element, "illusion", has no coma, always, and not because the coma is invisible - this is JSON syntax rule):

[
    "archery",
    "sneak",
    "illusion"
]

The function backupSkills writes the Player skills into the My Games/Skyrim/JCUser/Utilities/skillbackup.json file:

function backupSkills(objectreference plr)
	-- read SkillList.json file containing Skyrim skills
	-- jSkillList is ["archery","sneak", "illusion"] array.
	-- JValue.readFromFile parses the file contents and
	-- (since the file is an array of strings) creates and returns an array (instance of JArray) containing strings
	-- jSkillList is unique number which identifies the array
	int jSkillList = JValue.readFromFile("Data/SkillList.json")

	-- instantiate (create) a map container (JMap instance) to fill it with {skill: number} pairs later
	-- jBackup is unique number which identifies the map
	int jBackup = JMap.object()

	-- cycle through jSkillList's elements (e.g. skills, strings)
	-- JValue.count returns 3
	int i = JValue.count(jSkillList)
	while i > 0
		i -= 1
		-- retrieve skill-string at index 2, 1 and 0 of jSkillList array
		string skill = JArray.getStr(jSkillList, i)
		int value = plr.getAV(skill)
		-- fill backup with {skill: value} associations
		JMap.setInt(jBackup, skill, value)
	endwhile

	-- create the backup file in _My Games/Skyrim/JCUser/Utilities/skillbackup.json_
	JValue.writeToFile(jBackup, JContainers.userDirectory() + "Utilities/skillbackup.json")
endfunction

So, as a result of executing backupSkills we will got a file at My Games/Skyrim/JCUser/Utilities/skillbackup.json containing:

{
    "archery": 30,
    "sneak": 25,
    "illusion": 30
}

A function for restoring the skills:

function restoreSkills(objectreference plr)
	-- read the backup file in _My Games/SKyrim/JCUser/Utilities/skillbackup.json_
	int jBackup = JValue.readFromFile(JContainers.userDirectory() + "Utilities/skillbackup.json")

	-- retrieve first JMap's key, skill
	string skill = JMap.nextKey(jBackup)
	while skill
		-- retrieve skill value
		int value = JMap.getInt(jBackup, skill)
		plt.setAV(skill, value)
		-- retrieve next JMap's key
		skill = JMap.nextKey(jBackup, skill)
	endwhile
endfunction

A bit of variation

The example above is redundant a bit. It writes a data into a file and reads it back, while we can simply pass JMap instance containing {skill: number} pairs into the restoreSkills function:

function changeRace(objectreference plr, Race rc)
    int jskills = getSkills(plr)
    plr.SetRace(rc)
    restoreSkills(plr, jskills)
endfunction

int function getSkills(objectreference plr)
	int jSkillList = JValue.readFromFile("Data/SkillList.json")
	int jBackup = JMap.object()

	int i = JValue.count(jSkillList)
	while i > 0
		i -= 1
		string skill = JArray.getStr(jSkillList, i)
		int value = plr.getAV(skill)
		-- fill backup with {skill: value} associations
		JMap.setInt(jBackup, skill, value)
	endwhile

	return jBackup
endfunction

function restoreSkills(objectreference plr, int jBackup)
	-- retrieve first JMap's key, skill
	string skill = JMap.nextKey(jBackup)
	while skill
		-- retrieve skill value
		int value = JMap.getInt(jBackup, skill)
		plt.setAV(skill, value)
		-- retrieve next JMap's key
		skill = JMap.nextKey(jBackup, skill)
	endwhile
endfunction

Conclusion

The example above shows only the basic usage and does not cover all the features. Where you can go from here: