Jump to content

[LE] Script for adding items randomly to an inventory?


huggibee

Recommended Posts

Hello! I'm looking for some help in the creation of a script that's maybe attatched to an object (notebook) and when placed in an npc inventory, spawns objects (paper entries) at random intervals. Is this possible? If so, would anyone be willing to help me?

 

Thanks in advance!

Link to comment
Share on other sites

I am also a beginner, so for learning purposes, I will give this one a shot.

 

EDIT - The script has one major flaw, apparently its completely impossible to make inventory items persistent, even forcefully. So OnUpdate() never calls, I am not sure how to work around that without using while loops, and I am not sure if that's even ok to do, for threading reasons.

 

 

 

Scriptname MyScriptName extends ObjectReference  
{Spawns inventory items, in inventory, at random intervals. Written by Wilbur Cobb / smashballsx88}

Keyword property ActorKW auto	;Set this to ActorTypeNPC to filter for NPCs.

FormList property MyItems auto	;Create a form list of all the items you want spawned.

ObjectReference addRef	;The container to add the items to.

int flSize	;Int to assign the form list size to.

bool inInv	;Ensures termination on inventory removal, all bool values check true by default.

Event OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)
	if akNewContainer.HasKeyword(ActorKW)	;Checks if the container matches the key word.
		addRef = akNewContainer	;Assigns the current container, to the container reference.
		inInv = true
		gotoState("Looping")	;Goes into the looping state to start item spawning.
	else	;If the container is anything that doesnt match the keyword, terminate.
		inInv = false
		UnregisterForUpdateGameTime()
		gotoState("")
	endif
endEvent

state Looping	;This state will only fire once until the state changes. This is to prevent update stacking for this script.
	Event OnBeginState()
		flSize = MyItems.GetSize()	;Assigns form list size to an int
		RegisterForSingleUpdateGameTime(1)	;This starts the update loop in 1 in-game hour.
	endEvent
	
	Event OnUpdateGameTime()
		if inInv	;Prevents the update from running. True to run, false to terminate.
			if flSize >= 0	;Stops once the form list has been gone through
				Form pEnt = MyItems.GetAt(flSize)	;Gets the paper entry, based on flSize for the chosen index.
				addRef.AddItem(pEnt)
				flSize -= 1	;Subtracts 1 from total list size to track what was already spawned.
				int ranHr = Utility.RandomInt(1, 24)	;You can set these 2 values to what ever you like, think of it as dice.
				RegisterForSingleUpdateGameTime(ranHr)	;Sets the random in-game hour to spawn an item again.
			else
				gotoState("")	;Makes sure to terminate once all the items are spawned.
			endif
		endif
	endEvent
endState

 

 

 

If anyone wants to make it better, and explain somethings, please do. I am in this too.

 

This basically spawns a certain number of items in order, starting from the highest index number. It will do it at random intervals, for each container (NPC) it is placed it. This also means the cycle starts over every time its in the players inventory. If you want to make it a finite number, just use a global variable.

Edited by smashballsx88
Link to comment
Share on other sites

Thank you very much for your help! I appreciate it greatly!

Its no problem, I've got to work on my own mod for now, since I haven't even uploaded my first one yet! Hopefully though using the notes and stuff you at least have a general idea on what to do. The Papyrus wiki will always be your absolute best friend.

 

Once you really start to grasp the concept of Papyrus, I would double down on these 4 topics that will pave the way of making you a competent script writer. They are actually gravely important, I wouldn't ship a mod until you begin to implement and practice these concepts.

 

Threading Notes

 

Persistence

 

Using Functions

 

A map of all the Script Objects and their extensions/relationships

Link to comment
Share on other sites

  • Recently Browsing   0 members

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