Jump to content

Scripting question: Getting the base item returned by a leveled item?


bjornk

Recommended Posts

Posted

I'm trying to modify the ore mining script and my modification to the code below doesn't seem to work for some reason.

 

Original code:

If lItemGems10
    (game.getPlayer()).addItem( lItemGems10 )
EndIf

Modification:

If lItemGems10
    (game.getPlayer()).addItem( lItemGems10, Utility.RandomInt(1, 3) )
EndIf

lItemGems10 is a LeveledItem.

 

I want the code to return a random amount (1 to 3) of gems when a gem is available, but it still returns a single gem. The following doesn't seem to work either.

Form gem = lItemGems10 as Form ; Hoping to get the base object

If gem
    (game.getPlayer()).addItem( gem, Utility.RandomInt(1, 3) )
EndIf

Any ideas on how to do that?

 

 

Posted

Nope. I've already tried that one. It didn't even compile.

 

It was something like this...

MiscObject gem = lItemGems10.GetBaseObject() as MiscObject
If gem
    (game.getPlayer()).addItem( gem , Utility.RandomInt(1, 3) )
EndIf

I couldn't think of anything else to use for gems other than MiscObject.

Posted

Okay, most of my experience with scripting is in more ephemeral stuff but this what I think is happening: the form picked up by the LeveledItem property is a single instance generated from the levelled list tied to that instance of the script. Whatever the script is bound to would have to be reset to generate a new instance and thus a new gem.

 

If that is the case then adding two other LeveledItem properties for the extra gems should work and then a simple conditional for adding the other two.

Posted

I'm not sure if adding two more would make any difference. One should be enough anyway, all I want is to store the form it returns. 

Posted

Wouldnt the creation of complete crafting overhaul know? Seeing how in his MCM there is an option to grant an random gem everytime you mine.

 

You could ask him in PM in case he knows.

Posted

to grant an random gem everytime you mine.

That's easy to do. My problem is giving the player two or more of the same random gem.

Posted

I think I see the problem.

 

A levelled list will return three separate items for addItem(gem,3) so it's not what I suspected.

 

But the "10" in lItemGem10 is referring to the chance modifier on the levelled list that it will actually produce an item. That is, it has a 90% chance to return no gem at all. So maybe you get a gem, maybe you get none, but it's very unlikely you will get three.

 

You could use the lItemGems list instead which has a 100% chance of an item. But this will bypass anything that modifies the lItemGem10 list like Crafting Overhaul.

 

Edit: Might be worth looking at the LeveledItem script.

Posted

I think the vanilla code works like this...

 

If lItemGems10 <- assume that it contains a garnet here
    (game.getPlayer()).addItem( lItemGems10 ) <- this might add a garnet, or might add a different gem, or none at all
EndIf

 

I need to convert the leveled item to a (single) gem if it's not null and store it, in order to add that gem multiple times.

Posted

I was misunderstanding. Sorry, should have paid more attention to the subject line. Try this:

Form gem = (lItemGems10 as Form)
(Game.getPlayer()).addItem(gem,3)

That should give you three of whichever gem the lItemGems10 returns (which will be none 90% of the time unless you switch the property to lItemGems or use the LeveledList Script to alter the chance).

Posted

It doesn't work because the leveled item is not generated at that point yet. You can't add more than one at a time, so a solution might be:

 

int count = Utility.RandomInt(1, 3)
Actor plr = Game.GetPlayer()
while(count > 0)
count -= 1
plr.AddItem(lItemGems10)
endwhile
Posted

I was misunderstanding. Sorry, should have paid more attention to the subject line. Try this:

Form gem = (lItemGems10 as Form)
(Game.getPlayer()).addItem(gem,3)

 

 

int count = Utility.RandomInt(1, 3)
Actor plr = Game.GetPlayer()
while(count > 0)
count -= 1
plr.AddItem(lItemGems10)
endwhile

 

Believe it or not, I've already tried both of these and neither worked.

 

First of all, lItemGems10 is already a form, so casting it to a form would give you another LeveledItem and makes no difference and adding items in a loop gives you different gems, which isn't what I want.

 

If you'd like to try it yourself, you can get the source code here.

Posted

Perhaps this is not exactly what we are looking for, but it's closer:

Form gem = lItemGems10.GetNthForm(Utility.RandomInt(0,lItemGems10.GetNumForms() - 1))

This just selects a random gem from the leveled list. It ignores the chance of none and also the item count in the list so it is much more likely to return items that should be rare.

Posted

I wonder if using the leveled list in a quest alias would do it? Would starting the quest generate an item or would it still be a leveled list until an item is spawned in a container or in the world?

 

It does seem like there should be an easier way to collect an item rendered from a list but I'm not turning anything up.

Posted

I guess you could try adding an item to dummy container. Get first item from the container and then get base form, delete the item from container and add this base form item to player x count.

Posted

I'm pretty sure we can find more than one cumbersome ways to do it, but I don't want it to be computationally expensive, not worth it just to give a bunch of gems. I think I'll leave it as it is for the time being. I might even remove the "if" block.


If lItemGems10
    (game.getPlayer()).addItem( lItemGems10, Utility.RandomInt(1, 3) )
EndIf
If lItemGems10
    (game.getPlayer()).addItem( lItemGems10 )
EndIf
(game.getPlayer()).addItem( lItemGems10, Utility.RandomInt(1, 3) )
(game.getPlayer()).addItem( lItemGems10, 5 )

All the above have some randomness to them, the last one is the fastest so I'll probably use that.

 

Posted

I'm pretty sure we can find more than one cumbersome ways to do it, but I don't want it to be computationally expensive, not worth it just to give a bunch of gems. I think I'll leave it as it is for the time being. I might even remove the "if" block.

 

I really wouldn't worry about performance in this case. Unless you are running this continuously it really won't be an issue. And even then I imagine it's only running in a single instance (not attached to 20+ NPCs or being called via RegisterForUpdate) so it's not going to be capable of having a noticeable impact on the game.

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

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