Jump to content

Recipe That Spawns A Random Item


Jatta Pake

Recommended Posts

I've spent eight hours this weekend reading through the forums and trying to reason through the lua that would allow my mod to spawn a random item as part of a recipe.  I think the problem could be a limitation of the OnCreate: Additem code but I'm just not sure.  Or I could be really close and just off with my code a tiny bit.  Here is what I have so far.

 

recipe.txt

module MIBrecipe /* ========= Unwrap Gift Box ========= */ {imports{Base}  recipe Unwrap Gift Box{Spiffo, Result:Rubbish,Time:50.0,OnCreate:AddVariant_OnCreate,}}

 

AddVariant.lua

function AddVariant_OnCreate(items, result, player)    local SpiffoNumber = ZombRand(3) +1;   if SpiffoNumber = 1 then   player:getInventory():AddItem("ListSpiffo.RubySpiffo");elseif SpiffoNumber = 2 thenplayer:getInventory():AddItem("ListSpiffo.SapphireSpiffo");elseif SpiffoNumber = 3 thenplayer:getInventory():AddItem("ListSpiffo.JetSpiffo");end  end

 
Link to comment
Share on other sites

Shouldn't it be

if SpiffoNumber == 1 then...

?? Double signs? Single equal sign assigns a value, use double equal signs in if loops. Also, print() your random number to see its value.

 

Edit:

Here's a little function that will spawn 5 Spiffos and print a random number to the console:

local function AddSpiffo(_keyPressed)    local key = _keyPressed;    local numberOfItems = 5;    if tostring(key) == tostring(Keyboard.KEY_M) then        local player = getSpecificPlayer(0);        local inventory = player:getInventory();                for loopCount = 1, numberOfItems do            local SpiffoNumber = ZombRand(3) +1;            print("*** Spiffo" .. SpiffoNumber);            inventory:AddItem("Base.Spiffo");        end    endend-- AnyKeyPressEvents.OnKeyPressed.Add(AddSpiffo);

Press M ingame to spawn the Spiffos.

Edited by kerbholz
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...