Jump to content

OnCanPerform & OnCreate


Hugo Qwerty

Recommended Posts

Does anyone know, for a given recipe X, is the OnCanPerform function in recipe X guaranteed to be the last OnCanPerform function called before the OnCreate function of recipe X?

In other words, if PZ is calling OnCanPerform functions for all available recipes (?), is it guaranteed that the last OnCanPerform that runs before the player triggers OnCreate for a recipe will be be the OnCanPerform for that recipe?

 

I'm trying to access a recipe name within an OnCreate function, and the only way I can think of is either storing the recipe in an OnCanPerform that stores the recipe name and returns true, or write a bespoke function for every OnCreate and hard-code the recipe name.  It would be much easier if I could do the former.

Link to comment
Share on other sites

Why do you need the recipe name?

 

You can add whatever extra info you need on the end result item you are creating:

    item CarLights
    {
        DisplayCategory = CarWanna,
        Weight  = 0.1,
        Type    = Normal,
        Icon    = AutoTitle,
        DisplayName = PinkSlip: Chevalier Nyala,
        VehicleID = Base.CarLights,
        WorldStaticModel = CW.AutoTitle,   
        Tooltip = Tooltip_ClaimOutSide,	
        Condition = 100,
        GasTank = 100,
        HasKey = true,
    }

And then use one function on the recipe to create the item, heck in this case im using the input item to get my extra data:

    recipe Claim Vehicle
    {
       CarLights/CarLightsPolice/CarLuxury/CarNormal, //truncated for post
       Result: Base.CarKey,
       Time: 50.0,
       OnCanPerform:Recipe.OnCanPerform.CW_ClaimVehicle,
       OnCreate:Recipe.OnCreate.CW_ClaimVehicle,
       RemoveResultItem:True,
    } 

then in OnCreate i can access the data in "VehicleID" using getModData()

function Recipe.OnCreate.CW_ClaimVehicle(items, result, player)
    local pinkslip = items:get(0)

    if not player:isOutside() or player:getZ() > 0 then
    --This should be caught with Recipe.OnCanPerform.CW_ClaimVehicle, but if we get here somehow give player back the pinkslip.
        player:Say("This wont work unless im standing on the ground outside...")
        player:getInventory():AddItem(pinkslip)
    else 
		local modData = pinkslip:getModData()
		local requestedVehicle = { type = modData.VehicleID }
		
		if (type(modData.Condition) == "number") then
			requestedVehicle.condition = modData.Condition
		end
		if (type(modData.GasTank) == "number") then
			requestedVehicle.gastank = modData.GasTank 
		end	
		if (type(modData.FuelTank) == "number") then
			requestedVehicle.fueltank = modData.FuelTank
		end		
		if modData.HasKey then
			requestedVehicle.makekey = true
		end  
		if modData.Upgraded then
			requestedVehicle.upgrade = true
		end 
        requestedVehicle.dir = player:getDir();
        requestedVehicle.clear = true
        
        sendClientCommand(player, "CW", "spawnVehicle",  requestedVehicle ) 
    end

I did use the input items but i could have just used the result and pulled the data the same way with getModData()

Edited by Xyberviri
Link to comment
Share on other sites

Thanks for the reply.

I'm trying to be clever... I'm trying to auto calculate the nutrition values of a food item using 1 generic OnCreate function.  It's easy for simple food recipes - where it just takes whole items and combines them.  However, when you start adding partial food, e.g. "Base.OilVegetable;1" the item available within OnCreate is the food item after the vegetable oil has been taken, and not the 1 hunger of vegetable oil actually taken from the bottle, i.e. if you take 1 hunger of oil from a full bottle the item passed into OnCreate is the almost full bottle - so you can't take the calories from it to calculate the total.

However, if I have the recipe name (assuming it's unique) then I can determine how much vegetable oil the recipe specifies, then calculate the correct calories accordingly.

Maybe if I create a dummy 'Written Recipe' item, put some modData on it as you've done in the example, then require that as an ingredient in the recipe, that should work.  Or just use the result, as you suggest.

FWIW, the convoluted version I wrote does seem to work...
 

	recipe Make Bowl of Cereal
	{
		Bowl,
		[Recipe.GetItemTypes.Cereal];5,
		[Recipe.GetItemTypes.Milk];2,
		Result:CerealBowl,
		Time:50.0,
		Category:Cooking,
		OnGiveXP:Recipe.OnGiveXP.Cooking3,
		OnCanPerform:Recipe.OnCanPerform.Hydrocraft.RememberThisRecipe,
		OnCreate: Recipe.OnCreate.Hydrocraft.CreateComplexFood,
		Override: true,
	}

Thanks for the help.

Hugo

 

Link to comment
Share on other sites

On 6/3/2022 at 8:53 PM, Hugo Qwerty said:


FWIW, the convoluted version I wrote does seem to work...
 

sadly I'm finding more and more you will need to do some convoluted work around because code just doesn't work the way you think it should.

Edited by Xyberviri
Link to comment
Share on other sites

I've got the convoluted work around code written already, it's just automating it that I was struggling with.  I could just create bespoke OnCreate functions that explicitly look up recipes by name (so 1 OnCreate for each food item, which then calls a generic calculate function and passes in the recipe name), but 1 recipe that could do it all without needing to do anything extra was my objective.

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...