Author Topic: Check if there is X amount of Y, and if so, give the player Z  (Read 1451 times)

since the title was too short for me to describe it, and i literally have no idea how to describe it in general, i have plenty of space here

So what I'm essentially trying to doing is create a cooking addon. For instance, when a player wants to create something called a "Fruit Salad," they put in a bowl 5 strawberries and 5 blueberries minimum. When they have enough, they will receive a Fruit Salad that will add a minimum of %15 to a player's hunger bar, and each additional ingredient will add %1 extra hunger point to a player. But how do I check if the player can actually make the recipe?

 I was also planning to also create a registerRecipe function with the args (%name, %thirst, %hunger, %difficulty, %preparation, %ingredients), so how could I make it work with the function that actually checks if a valid recipe could be made?

Sorry if this was hard to convey through your mind, I can't explain anything well :P (I apologize for grammer mistakes too)

id structure it like this:

recipies would be stored with the format
$foodname[ingredient] = number;
and
$foodname_ingredients = string of ingredients (tab delimited) (or space, whatever works for you);
other variables can be saved that way too; make sure to export the variables after registering a recipe so you dont lose all of them on server restart.
probably should format it like $Cooking::foodname[etc] so you can just do export("$cooking*", "directory");

then you can get the ingredients required from $foodname_ingredients, then check if %client.ingredient >= %foodname[%ingredient] by looping through the ingredients string.
« Last Edit: June 19, 2016, 03:01:07 AM by Conan »

(tab delimited) (or space, whatever works for you);
whats the difference (if there actually is one)?
i've always just used spaces because getWord feels like it makes a little more sense rather than getField

whats the difference (if there actually is one)?
i've always just used spaces because getWord feels like it makes a little more sense rather than getField
you can have spaces in your words. useful for if you have like [object] [position] [rotation] information which involves strings with spaces

also certain things like the undo stack use tab delimited format so its good practice to match the formatting if you make a custom undoable action.

I was bored so I made a thing:



The addRecipe function takes the name and ingredients (two arguments only). ingredients are separated by spaces.
Ingredients with spacing in their names will use an underscore instead of a space, notice the Blue_berries_5:

$recipes.addRecipe("Salad Bowl","Blue_berries_5 Lettuce_10 Apples_2 Carrots_3 Crutons_7");

I'm not sure how you're storing the variables to the client, but something like %client.ingredient["INGNAME"] -> amount is how I'd go about it.

From there, you call the gameConnection::canCookRecipe function (see the image) and that'll return if they have the proper ingredients (assuming you're storing the client's ingredients how i mentioned before).

This is a little base you can look through. You can use it if you want and add onto it, or leave it be and not use it all.

Click for Code

script objects can get slow if you have a bunch though, and have to be exported differently if you want to preserve recipes past server reset. nevertheless the approach makes sense, just not necessarily the most optimal method.

it would help with storing recipes in an easier to read format

Adding recipes is the same as adding an RTBPref through code. For example, you set it in a file of your choosing maybe called Recipes.cs and execute that file.


Thank you so much for this! I also forget to include that recipes were stored bricks as %brick.ingredients["blah blah blah"].

Ok cool, but how does a client get ingredients? Do they gather ingredients from clicking a specific brick?? The most compatible way with the code I posted would be to store it in an array to the client.

a brick gets ingredients added by having the ingredients literally thrown at it (it's charged like a snowball, and it IS a snowball since I need models :P)

the client gets the ingredients by spawning it in, since it's an item

Ahh ok, well hopefully everything works out. Let us know if you have any more problems.

ok, so, I modified some things (like the variable for the ingredient array name), and fixed it so it'd work with my script.

But that's not the case


This is the recipe I'm trying to make. For testing purposes, it's set to 1 of each.


As you can see here, I changed it from GameConnection to fxDtsBrick so it can work with the break.


This is how the ingredients are stored. In this case, %this would be the brick.


And this is the check to make sure the ingredients are enough.


This doesn't seem to be working, even with the minimum amount of ingredients and a high amount of ingredients added. Do you know the problem?

Yeah, either change your var name from obj.ingredients [] to obj.ingredient []. Or vice versa. Notice the script I provided uses ingredient, how you are storing vars to bricks is ingredients. Try changing that and let me know if it works.

thanks, now that fixed it. I just have one more question. How could I add total number of surplus food and give the player health when the recipe could be cooked?

What do you mean by surplus food exactly?? Like if a recipe requires 5 strawberries and they throw 6 at the brick??