Jump to content

[LE] Script - Calling a random integer assigned to items in a form list


Recommended Posts

Hello,

I'm working on a spell to conjure food, which is easy enough if I keep it limited to only 1 item. What I'm curious about is if anyone has a relatively easy method to have a script generate a random integer which I then use to summon a corresponding food item. An example being if it rolls a 1, it spawns a Sweet Roll, if it rolls a 2 it spawns Cooked Beef, and so on and so on.

 

Is there a way to do it without assigning a Potion property to every possible food option I want to have available to summon, perhaps with a FormList or such?

 

As always, any assistance is appreciated and thank you.

Link to comment
Share on other sites

Create a formlist of the items you wish to use. You can mix types such as food, potions and poisons for this application. Then use RandomInt to obtain a random integer which will then correspond to an index entry in the formlist.

 

Example:

 

 

Formlist property myList Auto
Actor property PlayerRef Auto
 
Form Function GetAnItem(FormList TheList)
  Int MaxIndex = TheList.GetSize() - 1
  Int index = RandomInt(0,MaxIndex)
  Return TheList.GetAt(index)
EndFunction
 
Event OnActivate(ObjectReference akActivator)
  If akActivator == PlayerRef
    Form myItem = GetAnItem(myList)
    PlayerRef.AddItem(myItem,1)
  EndIf
EndEvent

This script assumes an activator of some type that the player interacts with. When they do, it calls the function passing in the desired formlist. A random number is obtained from within the range of 0 to the max index value within the formlist. It then returns the form from the list so that you may do something with it. In this case, add one of the item to the player.

 

FYI - the function is not necessary, the code can be placed directly within the desired event. However, as a function you can utilize it with multiple formlists without having to retype the code in different locations.

 

 

 

Link to comment
Share on other sites

I managed to get it all working later last night. I did use a FormList to achieve what I was looking for. I did some tweaking to what Ishara supplied as an example to make it all work so that way it also removes the randomly spawned food after the effect runs out.
Link to comment
Share on other sites

@IsharaMeradin: I'd just use a leveled item directly instead. It allows for more flexibility and requires less script work.

Wouldn't a leveled list item only work if all items within were of the same type? What if the OP wants to include a variety of object types? I think the formlist approach would be more flexible in that regard.

Link to comment
Share on other sites

 

@IsharaMeradin: I'd just use a leveled item directly instead. It allows for more flexibility and requires less script work.

Wouldn't a leveled list item only work if all items within were of the same type? What if the OP wants to include a variety of object types? I think the formlist approach would be more flexible in that regard.

 

 

Leveled lists can have most kinds of form in them (I've never tried including a static), including other leveled lists, in any mixture. AddItem(LeveledItem) will evaluate the list and generate the same output as directly including it in a container's inventory.

Edited by foamyesque
Link to comment
Share on other sites

  • Recently Browsing   0 members

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