Jump to content

[LE] Containers spawn items conditionally


Recommended Posts

Hi all,

 

I am relatively new to modding. I have experience in creating custom weapons and armor. But never had to do much scripting myself.

 

I am trying to create a chest in the initial Helgen sequence, from where i can place a few weapons i have created. The weapons are unique leveled items, and i wan them to be race specific. I.e i want the chest to spawn that particular item only if player belongs to a specific race.

 

I have duplicated the unowned chest, placed it in the Helgen dungeon. I also added the weapons to the item list. But i cannot figure out how to add a condition that the weapon is spawned in the chest only if the player is of a certain race.

 

I would really appreciate any help.

Thanks.

Link to comment
Share on other sites

Hi all,

 

I am relatively new to modding. I have experience in creating custom weapons and armor. But never had to do much scripting myself.

 

I am trying to create a chest in the initial Helgen sequence, from where i can place a few weapons i have created. The weapons are unique leveled items, and i wan them to be race specific. I.e i want the chest to spawn that particular item only if player belongs to a specific race.

 

I have duplicated the unowned chest, placed it in the Helgen dungeon. I also added the weapons to the item list. But i cannot figure out how to add a condition that the weapon is spawned in the chest only if the player is of a certain race.

 

I would really appreciate any help.

Thanks.

 

There's a few possible approaches. I think the one I'd take would be to attach a script to the chest to do the following:

 

1. Detect when the chest has been loaded for the first time;

2. When it has been, get the player's race;

3. Add the appropriate items to the chest;

4. Set a flag to indicate the chest has been loaded and populated, so that it doesn't respawn every time it loads.

EDIT: This means not setting the chest's inventory in the editor.

Edited by foamyesque
Link to comment
Share on other sites

 

Hi all,

 

I am relatively new to modding. I have experience in creating custom weapons and armor. But never had to do much scripting myself.

 

I am trying to create a chest in the initial Helgen sequence, from where i can place a few weapons i have created. The weapons are unique leveled items, and i wan them to be race specific. I.e i want the chest to spawn that particular item only if player belongs to a specific race.

 

I have duplicated the unowned chest, placed it in the Helgen dungeon. I also added the weapons to the item list. But i cannot figure out how to add a condition that the weapon is spawned in the chest only if the player is of a certain race.

 

I would really appreciate any help.

Thanks.

 

There's a few possible approaches. I think the one I'd take would be to attach a script to the chest to do the following:

 

1. Detect when the chest has been loaded for the first time;

2. When it has been, get the player's race;

3. Add the appropriate items to the chest;

4. Set a flag to indicate the chest has been loaded and populated, so that it doesn't respawn every time it loads.

EDIT: This means not setting the chest's inventory in the editor.

 

Thanks a lot for your reply.

I am new to papyrus scripting. But i have managed to find out about almost all the steps you mentioned. But i have a doubt.

Since my weapons are leveled items, how should i add it to the chest so that it will automatically choose the appropriate level to spawn?

Edited by vikigenius
Link to comment
Share on other sites

 

 

Hi all,

 

I am relatively new to modding. I have experience in creating custom weapons and armor. But never had to do much scripting myself.

 

I am trying to create a chest in the initial Helgen sequence, from where i can place a few weapons i have created. The weapons are unique leveled items, and i wan them to be race specific. I.e i want the chest to spawn that particular item only if player belongs to a specific race.

 

I have duplicated the unowned chest, placed it in the Helgen dungeon. I also added the weapons to the item list. But i cannot figure out how to add a condition that the weapon is spawned in the chest only if the player is of a certain race.

 

I would really appreciate any help.

Thanks.

 

There's a few possible approaches. I think the one I'd take would be to attach a script to the chest to do the following:

 

1. Detect when the chest has been loaded for the first time;

2. When it has been, get the player's race;

3. Add the appropriate items to the chest;

4. Set a flag to indicate the chest has been loaded and populated, so that it doesn't respawn every time it loads.

EDIT: This means not setting the chest's inventory in the editor.

 

Thanks a lot for your reply.

I am new to papyrus scripting. But i have managed to find out about almost all the steps you mentioned. But i have a doubt.

Since my weapons are leveled items, how should i add it to the chest so that it will automatically choose the appropriate level to spawn?

 

 

The AddItem function will accept a LeveledItem as an argument. The game will then calculate the output of it and add them to the chest (including quantities, etc) without any need for you to intervene.

Link to comment
Share on other sites

 

 

 

Hi all,

 

I am relatively new to modding. I have experience in creating custom weapons and armor. But never had to do much scripting myself.

 

I am trying to create a chest in the initial Helgen sequence, from where i can place a few weapons i have created. The weapons are unique leveled items, and i wan them to be race specific. I.e i want the chest to spawn that particular item only if player belongs to a specific race.

 

I have duplicated the unowned chest, placed it in the Helgen dungeon. I also added the weapons to the item list. But i cannot figure out how to add a condition that the weapon is spawned in the chest only if the player is of a certain race.

 

I would really appreciate any help.

Thanks.

 

There's a few possible approaches. I think the one I'd take would be to attach a script to the chest to do the following:

 

1. Detect when the chest has been loaded for the first time;

2. When it has been, get the player's race;

3. Add the appropriate items to the chest;

4. Set a flag to indicate the chest has been loaded and populated, so that it doesn't respawn every time it loads.

EDIT: This means not setting the chest's inventory in the editor.

 

Thanks a lot for your reply.

I am new to papyrus scripting. But i have managed to find out about almost all the steps you mentioned. But i have a doubt.

Since my weapons are leveled items, how should i add it to the chest so that it will automatically choose the appropriate level to spawn?

 

 

The AddItem function will accept a LeveledItem as an argument. The game will then calculate the output of it and add them to the chest (including quantities, etc) without any need for you to intervene.

 

Sorry to bother you again.

But I have made the script and theoretically it seems like it should work. I added this script to the container i created in the CK.

Scriptname SicariusChestPopulate extends ObjectReference  
{Populates the chest with magical items}

Race Property RequiredRace Auto
LeveledItem Property item1 Auto
LeveledItem Property item2 Auto

Auto State DoOnceState

Event OnActivate(ObjectReference akActionRef)
Race agentRace = akActionRef.GetActorOwner().GetRace()
If agentRace == RequiredRace
self.addItem(item1, 1)
self.addItem(item2, 1) 
GoToState ("DoNothingState")
EndIf
EndEvent

EndState
State DoNothingState
EndState

I am setting the Race and Item properties in the CK correctly.

However the chest shows up as empty even though i belong to the correct race. Can you point out what i am doing wrong?

Edited by vikigenius
Link to comment
Share on other sites

Maybe the next is what you want!

 

AddItemsToChestScript

 

Scriptname AddItemsToChestScript extends ObjectReference  
{written by ReDragon 2017}    ; SicariusChestPopulate: Populates the chest with magical items

; https://forums.nexusmods.com/index.php?/topic/5836202-containers-spawn-items-conditionally/
; vikigenius wrote: "trying to create a chest in the initial Helgen sequence, from where i can place a few weapons.. to be race specific"

  FormList PROPERTY raceList auto       ; all the available player races you like to give weapons, for example: BretonRace, OrkRace

; group weapons by race and create new formlists by CK (as much as you need)

  FormList PROPERTY weapList0 auto        ; raceList.GetAt(0) = BretonRace
  FormList PROPERTY weapList1 auto        ; raceList.GetAt(1) = OrkRace
 ;FormList PROPERTY weapList2 auto        ; etc.


; -- EVENTs --

;======================
state Done    ; do it once only
;=========
EVENT OnActivate(ObjectReference akActionRef)
ENDEVENT
;=======
endState


EVENT OnActivate(ObjectReference akActionRef)
    IF (akActionRef == Game.GetPlayer() as ObjectReference)
        gotoState("Done")        ; ### STATE ###
        myF_Action( (akActionRef as Actor).GetRace() )        ; get current Player race as parameter
    ENDIF
ENDEVENT


; -- FUNCTION --

;--------------------------
FUNCTION myF_Action(Race r)
;--------------------------
int i = 0
WHILE (i < raceList.GetSize())
    IF (raceList.GetAt(i) == r as Form)
        formList tmp                ; dummy list
        IF     (i == 0)
            tmp = weapList0            ; set dummy to weapons of race 1
        ELSEIF (i == 1)
            tmp = weapList1            ; set dummy to weapons of race 2
        ENDIF

        int n = 0
        WHILE (n < tmp.GetSize())
            self.AddItem(tmp.GetAt(n), 1)    ; fill chest with items depends on player race
            n = n + 1
        ENDWHILE
        RETURN    ; - STOP -    race has been found, weapons should be placed
    ENDIF
    i = i + 1    ; check next race
ENDWHILE
ENDFUNCTION

 

 

Edited by ReDragon2013
Link to comment
Share on other sites

One issue is probably that OnActivate tends to be finicky with containers. You would be a lot better off using the OnOpen event.

 

From there you should set a global variable with an OnClose event that detects the first time the chest is closed. I suspect that part of the issue with your script is that it is going to DoNothingState out of order.

 

So your script could look something like this:

 

Properties:

Global1 (whatever you make it)

Race1 (NordRace)

(Other Race Properties)

Item1 (What to add if your a Nord)

 

Event OnOpen(ObjectReference akActionRef)

if (akActionRef == Game.GetPlayer())

if (Global1 == 0)

 

if Game.GetPlayer().GetRace() == Race1

Self.Additem(Item1)

EndIf

 

(Other race add item scenarios)

 

EndIf

EndIf

EndEvent

 

Then add a second script for the OnClose Event

 

Event OnClose(ObjectReference akActionRef)

if (akActionRef == Game.GetPlayer())

Global1.SetValue(1)

EndIf

EndEvent

 

(I think this requires SKSE for changing a global variable, but you can accomplish the same thing by using the second script to enable/disable a marker instead and using IsEnabled and IsDisabled rather than if Global1 == 0)

Link to comment
Share on other sites

I think you're making this much harder than it needs to be. The player'e race changes almost never in the game, pretty much only when becoming a werewolf or vampire, so you just have to get the player's race once really. Then you have each of your levelled lists have a global that determines chance none, and all of them default to 100, and whichever race the player is you set that global to 0.
Link to comment
Share on other sites

  • Recently Browsing   0 members

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