Hi everyone,
I am working on a crafting game. I spent the past few days trying to figure out how to deal with data manipulation (i.e. how to store crafting recipes and how to work with them). From my little search, I found that most tutorials related to data manipulation are very basic "find all data and display it". Maybe I didn't search well, I don't know.
Anyway, I ended up going with a dictionary for each recipe. I loaded it up with AJAX (created as a file from the Files section in the project) when a player crafts something.
So I ended up with this messy bit of events:
First the player drags and drops the ingredients into the crafting slots (there are a total of 5 and player can use 2 or more in a recipe so no set number of ingredients per recipe). After that player hits the craft button and I load the first recipe using AJAX in a dictionary like below
https://i.imgur.com/7AKJu3W.png
I check the ingredients the player used and the dictionary keys and see if the length matches and if the keys (names of ingredients) match:
https://i.imgur.com/BbOhZw8.png
https://i.imgur.com/oLBoSuY.png
And here is the first problem -- as you can see in the second screenshot I have it where if the elementAmount (parameter) is less than or equal the element I am targeting it should return TRUE (a global constant variable set to 1) if not it returns FALSE (another global constant variable set to 0). The problem here is that no matter what happens inside the function itself it shows that the return value is true (I set it up that way and gave the elements very high numbers while the requirements are low for testing) but when outside of the function (i.e. when I store the return value) it is false for some odd reason. I don't know why this happens.
I am setting the variable called "ingredientAvailableResult" to the result of the function IsIngredientAvailable and that variable gets set to 0 ALWAYS. Even if the element I am checking has a value of 50 and the amount I am requesting is only 2. I don't even know why this is happening.
Anyone knows what is wrong?
That is the first issue. The second issue is the events themselves. For some reason I have to store a return value before I test it (as you can see each time I call a function) if I don't, the test is always false. Again, don't know why...
The final issue is related to code structure. Right now the way I am testing all of this is:
- Check which sprite is overlapping the slot sprite. Pick those from the ingredients family.
- Iterate over them and find if they are present in the dictionary keys.
- If they are iterate over the dictionary a second time to see if the ingredient amounts are enough to be able to deduct the amount.
- If they are enough, I iterate over the dictionary a third time to actually deduct the amount I want.
Here is the final loop to deduct the needed amounts:
https://i.imgur.com/bRFOsUi.png
I feel this is just wasted resources and coming from C# this could've been done in one loop pretty easy. Any ideas how do I fix these?
One that is all done and say if this isn't the correct recipe I load the next dictionary from the Files folder to the same dictionary I had before (after clearing it of course) and then run the entire thing again.
So TLDR/Summary of issues:
1- Why is the function IsIngredientAvailable returning FALSE even though inside the function the return value is reported TRUE?
2- Why is it that I have to store the return value of a function in a variable and then run conditions over that instead of directly running conditions over the function?
3- Anyway to structure the code so I don't have 3 loops over the same data just to get to what I want?