Jump to content

Recommended Posts

Posted

Here's how your mod can rename any reference in the game, with just F4SE and no need for RenameAnything.

You're going to need:

1) a MISC Item. Give it any name you want.


2) a MESSAGE.
Name it exactly like so: <Alias=NameItem> and set it's owner quest to the QUEST below. Do not check the Message Box flag.

3) a QUEST like so:

OEYzIYZ.png


Also create two quest stages, 10 and 20.
Check "Run on start?" for stage 10 and "Run on stop?" for stage 20.
Ensure that both stages have "Keep Instance Data From Here On" checked - Or else it won't work.

X5u6Qtl.png

For the quest itself, ensure that only "Repeated Stages" is checked, or it wont work.
Then give the quest two reference aliases:

EJOgyqf.png

Pay attention to the alias flags, they are very imporant!

The first alias will be for the Target reference you want to rename, it's called Bot in the picture above.
The second alias will be for the MISC item.

4) Create a Papyrus script like so:
 

Scriptname NISTRONRobotAssembler:Renamer extends ObjectReference

MiscObject Property nra_RenamingItem Auto Const Mandatory
Message Property nra_RenamingMessage Auto Const Mandatory
Quest Property nra_RenamerQuest Auto Const Mandatory
ReferenceAlias Property RenamingItem Auto Const Mandatory
ReferenceAlias Property Bot Auto Const Mandatory

Bool bIsRenaming = false
Bool Function Rename(ObjectReference refTarget, string newName)
	; we must use a lock so concurrent calls will not confuse the algorithm
	Int limit = 100
	While (bIsRenaming && (limit > 0))
		; wait for lock release for <limit> retries
		Utility.WaitMenuMode(0.1)
		limit -= 1
	EndWhile
	If (bIsRenaming)
		; lock not released in time
		Debug.Trace(Self + ": ERROR - Rename() failed with lock timeout; Unable to rename reference (" + refTarget +").")
		Return False
	EndIf
	bIsRenaming = true
	
	Debug.Trace(Self + ": DEBUG - Renaming (" + refTarget + ") to (" + newName + ")...")
	; set name on renaming item form
	nra_RenamingItem.SetName(newName)

	; start quest
	Bool success = nra_RenamerQuest.SetStage(10)
	If (!success)
		Debug.Trace(Self + ": ERROR - Quest failed; Unable to rename reference (" + refTarget +").")
		bIsRenaming = false
		Return False
	Else
		;Debug.Trace(Self + ": DEBUG - Quest stage set.")
	EndIf	

	; create renaming item reference
	ObjectReference refRenamingItem = PlaceAtMe(nra_RenamingItem, 1, false, true, true)

	If (refRenamingItem == none)
		Debug.Trace(Self + ": ERROR - Rename() failed to create nra_RenamingItem instance; Unable to rename reference (" + refTarget +").")
		bIsRenaming = false
		Return False	
	EndIf

	; add renaming item to NameItem alias
	RenamingItem.ForceRefTo(refRenamingItem)

	; give some time for alias logic to process
	Utility.WaitMenuMode(0.3)
	
	; add target reference to Bot alias
	Bot.ForceRefTo(refTarget)

	; wait max 5x0.3 seconds for renaming to happen
	String actualName = refTarget.GetDisplayName()
	limit = 5
	Utility.WaitMenuMode(0.3)
	While ((actualName != newName) && (limit > 0))
		Utility.WaitMenuMode(0.3)
		actualName = refTarget.GetDisplayName()
		limit -= 1
	EndWhile
		
	; shutdown quest
	nra_RenamerQuest.Stop()	
	
	;Debug.Trace(Self + ": DEBUG - Quest stopped.")
	
	; cleanup
	refRenamingItem.Delete()
	refRenamingItem = none
	
	; did it actually rename?
	If (refTarget.GetDisplayName() != newName)
		Debug.Trace(Self + ": ERROR - Failed to rename reference (" + refTarget + ") to (" + newName + "); Actual name is (" + actualName + ").")
		bIsRenaming = false
		Return False
	EndIf
	
	Debug.Trace(Self + ": DEBUG - Reference (" + refTarget + ") renamed.")
	bIsRenaming = false
	Return True
EndFunction


5) Attach the renamer script to something, configure the script properties.
Obtain a reference to the object to which the renamer script is attached (named rnr below), and then invoke the renaming script from another script in your mod:
 

Bool renamerResult = rnr.Rename(theTargetRefToRename, "The New Name")

If the result is true, the renaming worked. If false, it didn't and an ERROR message will have been written to the Papyrus log (if enabled).

6) That's all folks!
If something is unclear, you can investigate my NISTRON Robot Assembler mod and look there. It uses this technique.



Consider this a farewell gift 🙂
Happy gaming and modding to all of you!

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...