Jump to content

Blocking access to crafting apparatus unless player has certain items. [Solved.] Any possible improvements?


mattrk

Recommended Posts

Hello all,

 

I have created a script that blocks access to crafting apparatus unless player has certain items:

 

 

 

Scriptname AAINEScript Extends Quest

; 'IsharaMeradin' played a significant part in the construction of the OnCrosshairRefChange() part of this script.
; The script was integrated/modified/extended by Matthew R. Karlsen (quixotic-cynic, quixoticynic on Nexusmods)

Actor Property playerRef Auto
Keyword Property forgeKeyword Auto
Keyword Property workbenchKeyword Auto
Keyword Property anvilKeyword Auto
Keyword Property smelterKeyword Auto
MiscObject Property BlacksmithHammer01 Auto
MiscObject Property BlacksmithTongs01 Auto
MiscObject Property Shovel01 Auto
MiscObject Property Shovel02 Auto
LeveledItem Property LItemBlacksmithMinimum Auto

Form refBase
Form hammerAsForm
Form tongsAsForm
Form shovelOneAsForm
Form shovelTwoAsForm

Event OnInit()
	Debug.Notification("An Anvil Is Not Enough v0.6.1. initialized.")
	Debug.Trace("An Anvil Is Not Enough v0.6.1. initialized.")
	Maintenance()
EndEvent

Function Maintenance()
	Utility.Wait(1)
	Debug.Notification("Maintenance performed for An Anvil Is Not Enough v0.6.1.")
	Debug.Trace("Maintenance performed for An Anvil Is Not Enough v0.6.1.")
	UnregisterForCrosshairRef()
	RegisterForCrosshairRef()
	hammerAsForm = BlacksmithHammer01 as form
	tongsAsForm = BlacksmithTongs01 as form
	shovelOneAsForm = Shovel01 as form
	shovelTwoAsForm = Shovel02 as form
EndFunction	

Bool Function playerHasItem(Form item)
	Bool hasItem = False
	If(playerRef.GetItemCount(item) > 0)
		hasItem = True
	EndIf
	Return hasItem
EndFunction

Event OnCrosshairRefChange(ObjectReference ref)
	If (playerRef.IsInCombat() == False)
		If ref ; i.e. if ref is not 'None'
			refBase = ref as form
			If refBase.HasKeyword(forgeKeyword)
				;Debug.Notification("Ref is forge.")
				If (playerHasItem(hammerAsForm) && playerHasItem(tongsAsForm))
					ref.BlockActivation(false)
				Else
					ref.BlockActivation(true)
					;Debug.Notification("You need a hammer and tongs to use this apparatus.")
				EndIf
			ElseIf (refBase.HasKeyword(workbenchKeyword) || refBase.HasKeyword(anvilKeyword))
				;Debug.Notification("Ref is workbench or anvil.")
				If (playerHasItem(hammerAsForm))
					ref.BlockActivation(false)
				Else
					ref.BlockActivation(true)
					;Debug.Notification("You need a hammer to use this apparatus.")
				EndIf
			ElseIf refBase.HasKeyword(smelterKeyword)
				;Debug.Notification("Ref is a smelter.")
				If (playerHasItem(shovelOneAsForm) || playerHasItem(shovelTwoAsForm))
					ref.BlockActivation(false)
				Else
					ref.BlockActivation(true)
					;Debug.Notification("You need a shovel to use this apparatus.")
				EndIf
			EndIf
		EndIf
	EndIf
EndEvent

 

 

 

I would like to release it soon as part of my "An Anvil Is Not Enough" mod. I was wondering if anyone would be prepared to read through it and let me know what they think. Am I taking the correct approach or is there a much better way?

 

PS: This is not the only script -- I am using message boxes to tell the player that an apparatus is blocked if they try to activate a blocked apparatus.

 

Thanks & regards,

--QC

Link to comment
Share on other sites

I would use a perk to do this. Look at the werewolf perk for an example. I am currently using this method to block access to skill books. In my opinion it's better because all you need is a tiny script in the perk fragment telling the game what to do instead of the default activation, rather than a long script with register and unregister ect. do a search for perk entry point on the cs wiki for more info.
Link to comment
Share on other sites

I've not worked with perks (haven't been able to understand them yet) so I cannot say whether lofgren's approach will work or not.

 

Using your script approach, I would add one thing in case the player chooses to uninstall your mod. When the player does not have the appropriate tools and you block activation via script, it may remain blocked and prevent users from using the workstations after the mod has been uninstalled mid-game. You'll want to test for that behavior. If that is indeed the behavior, you may wish to add all blocked references to a formlist. Then have some means (either MCM menu, hotkey or something) for the player to initiate a cycle of that formlist where you have all those references unblocked.

Link to comment
Share on other sites

Hello Lofgren and IsharaMeradin,

 

Thank you very much for the suggestions. I will definitely think about uninstall and I am looking at the werewolf perk right now. I need to look at it in more detail before I decide whether to try implementing it (the complex parts of perks seem strange to me).

 

Iofgren, would you mind telling me in which of your mods do you block access to skill books -- I want to take a look! :smile:

 

Edit: Ok, I can see that the werewolf perk uses a "Fiter Activation" entry point to do this. Doesn't make sense to me at the moment, but I'll spend some more time on it.

 

Edit 2: I think I can see how it works. Thanks for the pointer.

 

Edit 3: The documentation for Filter Activation states that it "Can be used to prevent (or enable) the perk owner, activating (or in the players case not receiving a activate prompt as well.) the targeted object." but it doesn't explain how. Does anyone know somewhere that does? Google has failed me on this one.

 

Edit 4: Awesome. Here are the rules:

 

1 perk entry, with rank 1, priority 0, type 'Entry Point', data 1 'Filter Activation', data 2 'Add Value' and data 3 as '1'.

 

The perk entry has a conditional for Perk Owner: target 'S', function name 'GetItemCount', function info "Misc Item: 'BlacksmithHammer01'", comp '<', value '1.00', AND

 

The perk entry also has a conditional for Target: target 'S', function name 'GetIsID', function info "Furniture: 'CraftingBlacksmithForge'", comp '==', value '1.00', AND

 

Complex to work out (for me), but once you have it, it is much simpler than what I had before.

 

Thanks & regards,

--QC

Edited by quixoticynic
Link to comment
Share on other sites

That mod is not uploaded yet, but here is what the perk looks like:

 

 

 

The mod will allow the player to toggle training from skill use on and off. However a side effect of this is that skill increases from books are also disabled. This perk essentially says "If the object the player activates is a book, then turn training back on until the player is done reading."

 

For you, the smelter perk entry might look like this:

 

 

 

Where "NeedShovel" is a message box that pops up to let the player know they cannot use that crafting station unless they have a shovel. If the player has at least one of either of the two types of shovel, then the crafting station activates as normal. The script is never even triggered. The target tab would look like this:

 

 

 

Since perks are designed to be added and removed, a player who uninstalled your mod wouldn't have any issues. The perk, and all associated functionality, would simply be gone.

 

Link to comment
Share on other sites

Thank you very much for taking the time to show me that, Lofgren. I'd worked out how to block the smelter e.t.c. but I had not managed to work out the message box yet -- that really helps!

 

Edit: The next version of AAINE is now released.

 

Thanks & regards, --QC

Edited by quixoticynic
Link to comment
Share on other sites

  • Recently Browsing   0 members

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