Jump to content

NVSE Scripting Help


TheGodfoster5

Recommended Posts

Hi Everyone,

Relatively new modder here, but I'm currently working on a mod that allows the player to operate a caravan.  It would basically work like this: the player would give anything that they want to sell to a specific NPC, that NPC would then disappear and then reappear in a given number of days with their total inventory value converted to caps.  I'm currently trying to get the basics of the script working, but so far I'm having no luck.  If someone can tell me where I'm going wrong with the below script, I would appreciate it!

scn AACaravan1Script

array_var aCaravan
short iCaravanStatus
int iIndex
int iCaravanValue
int iItemValue
ref rItem

Begin Gamemode

if iCaravanStatus == 1
	Let aCaravan := MojaveOutpostCaravaneerREF.GetAllItemRefs 0 0 0 1
	let iIndex := ar_size aCaravan
	while (iIndex -= 1) >= 0
		let rItem := aCaravan[iIndex]
		let iItemValue := rItem.GetItemValue
		let iCaravanValue += iItemValue
		MojaveOutpostCaravaneerREF.RemoveItem rItem 1
		loop
	endif
player.additem caps001 iCaravanValue
set iCaravanStatus to 0
set iCaravanValue to 0
MojaveOutpostCaravaneerREF.enable

End

Once I get this working, I'm planning on adding a delay of a few in-game days, and the caps will be added to a container to be collected by the player manually. 

 

Thanks in advance for anyone's help! 

  • Like 1
Link to comment
Share on other sites

@TheGodfoster5It is 2 years ago I did code obse but what I do recall is that when you use arrays, is that you must initiate them before you can use them, create them. So you cannot add anything to them until they are created. I will try to find how to do it.

 

This is how you do it: As there are 3 different kinds of Arrays, you must include in your script which type you plan to use and I always used "let MyArray := ar_Construct array" so I bet that one is what you will be using and that must be used before you will be able to use your array at all. I do think it only needs to be done at 1 time only, so do it within a DoOnce if, like this...

 

short DoOnce

If (DoOnce < 1)
	let aCaravan := ar_Construct array
	Set DoOnce to 1
EndIf

 

1. Explicitly initializing it using ar_Construct

When initializing an array with ar_Construct, the type of array must be provided as the argument:
let MyArray := ar_Construct array
let MyArray := ar_Construct map
let MyArray := ar_Construct stringmap


2. Assigning the value of another array_var to it

In this case MyArray will be of the same type of SomeArray (actually, both refer to the same array)
let SomeArray := ar_Construct array
Let MyArray := SomeArray

 

3. Assigning it the return value of a command returning an array such as GetItems.

Let MyArray := GetItems
 
Edited by Pellape
Link to comment
Share on other sites

@TheGodfoster5Later if you want to use anything stored inside in an array, you must add it back to a Ref, short or float as you cannot use the data inside it in a string or compare the data with anything nor use it for calculations. Arrays are so damn great and I used them in almost every script I made for my Oblivion mod. But they are nothing else than a database and nothing if you do not inlude in your script which type of array you will use. In your case, you store references, which is most common really, at least for myself, so therefor is "ar_Construct array" the obvious choice to use. I do not really know when it is more proper to use the other 2 variants, as I have never used them but we sure had a debate about them in the Oblivion forum and some suggested maps but it does not really matter honestly.

In the debate, some suggested that we skip to use set at all and always use let := instead, but I am not so sure about that but I do know that if we use obse or nvse specific functions, let := is the only way to do it. I see that you use Let for ordinary shorts and I would avoid that, because the compiler will not give a damn about if you do a typo there, so it will then be extremely hard to find bugs, if you do any typos that is. With the Set command at the right place, is something that the compiler will check and will give you an error message.

Edited by Pellape
Link to comment
Share on other sites

@Pellape Thanks for the reply! 

 

Wouldn't this line below from my script initialize my array?  I'm assigning it the value of a command like the 3rd method you mentioned.

Let aCaravan := MojaveOutpostCaravaneerREF.GetAllItemRefs 0 0 0 1

3. Assigning it the return value of a command returning an array such as GetItems.

Let MyArray := GetItems
 
 
 
  • Confused 1
Link to comment
Share on other sites

@TheGodfoster5No not at all. I will put the initiating at the right spot (I hope) Initiate is maybe the wrong word as Constructing the array is more proper, creating it as arrays are extremely special and can be used i 3 different ways, so we must tell the script which of the 3 we will use.

  • array
  • map
  • stringmap

I also took the liberty in formatting your script with proper tabs as this is how all do it that I know of...  😉   I guess your will be an array, as that is most common. You can also read more about the different ones here but it is not strings you store, as it is refs and then array is most suitible but stringmap can also be used. It all depends how you plan to get the data or rather access it later? I use shorts to get data from the proper array, therefor a stringmap is not applicable for me and shorts or ints are easiest to use and most common in all languages. map or stringmap are maybe useful in other types of databases, who knows? I almost failed in my database class when I did study to Computer technician 1998 and that was the hardest class I ever been to as well, well except a course in assembler at our technician University.  😄  We did assembler the Motorola 8-bit 6408 CPU and that was not easy, but easier than coding Intel 386 CPU, as that was a pain in the rear. I love C however. But still, databases set up in the most proper way, is extremely advanced and hard, well never easy to do for sure, as they are setup in a 3D type of structure. An nvse array in the other hand is a simple 2D setup, like a simple spreadsheet and that we can control easy, mostly, hopefully.  😄

 

array_var aCaravan
short iCaravanStatus
int iIndex
int iCaravanValue
int iItemValue
ref rItem
int PekDoOnce

Begin Gamemode

	If (PekDoOnce != 1)
		let aCaravan := ar_Construct array 
		Set PekDoOnce to 1
	EnIf

	if iCaravanStatus == 1
		Let aCaravan := MojaveOutpostCaravaneerREF.GetAllItemRefs 0 0 0 1
		let iIndex := ar_size aCaravan
		while (iIndex -= 1) >= 0
			let rItem := aCaravan[iIndex]
			let iItemValue := rItem.GetItemValue
			let iCaravanValue += iItemValue
			MojaveOutpostCaravaneerREF.RemoveItem rItem 1
		loop
	endif

	player.additem caps001 iCaravanValue
	set iCaravanStatus to 0
	set iCaravanValue to 0
	MojaveOutpostCaravaneerREF.enable

End

 

As you see, the creation is only done ones in a lifetime, as when it has been created, it will exist until the mod is no longer in use. There is commands that will destroy it if you want to get rid of it, but that is rare.

Edited by Pellape
Link to comment
Share on other sites

  • Recently Browsing   0 members

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