Skip to content
SilverIce edited this page Jun 24, 2015 · 8 revisions

Setting up Environment

Motivating Example

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

It's known that racemenu command, command that changes PC's race also resets skills to defaults, thus before race change, we'll backup the skills by writing them into a file and will read them back and restore.

That's how high-level part of a script looks like:

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

Let's 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):

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

Function backupSkills backups, writes PC's skills into 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, in a result of execution of backupSkills we'll got a file at My Games/Skyrim/JCUser/Utilities/skillbackup.json containing:

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

Restore 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

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 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: