Have two issues; simplifying events and return value not returned correctly.

0 favourites
  • 3 posts
From the Asset Store
2D fighting template based in the game that defined the fighting games genre.
  • 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?

    Tagged:

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • This is quite hard to grasp without a c3p file actually. I can give some general tips though

    1- Why is the function IsIngredientAvailable returning FALSE even though inside the function the return value is reported TRUE?

    ==> watch out with rules here. I always use a local variable called "result_value" or something, and in the very last action of the function (outside of any loop or condition) I just return that value. That's handy because you could debug this using console.log for example, and see what your result value becomes as you loop through. Debugging loops is always a bit of a hassle, but I usually use either a debug label and append some text as the loop goes on, or use console.log to track what variables become all through the loop.

    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?

    I presume that what you mean is where the "hasValue" needs to check if a recipe contains an ingredient? I would use a separate function for that returns true or false

    3- Anyway to structure the code so I don't have 3 loops over the same data just to get to what I want?

    Hard to see without the c3p file, but I get the feeling that you should first loop through the crafting stations and add the ingredients to the array. And then UNINDENTED go loop trough the choosen ingredients. If you have 3 stations, and they all have 1 ingredient... You loop through the stations: in the first iteration, the ingredient array will have one value, and will never match a recipe, the second time round, still not, the third time round, maybe.

    So, again, I don't know exactly the circumstances, and game logic, but I would think that you would first do the filling of the array. and then, NOT as a sub event, on the same level as the for each loop, do the rest?

    Personally, I'm a huge fan of functions and separating parts of the logic into functions even though they only get used once.

    If you're interested you can check out my youtube channel with a lot of useful examples. In most games I use a lot of functions, some of them even recursive.

    youtube.com/channel/UCZ6QjvqEs9dR2miRnfFqIpQ

    Hope my tips help.

    If you send the c3p file, maybe I can be of some help debugging it.

  • ==> watch out with rules here. I always use a local variable called "result_value" or something, and in the very last action of the function (outside of any loop or condition) I just return that value. That's handy because you could debug this using console.log for example, and see what your result value becomes as you loop through. Debugging loops is always a bit of a hassle, but I usually use either a debug label and append some text as the loop goes on, or use console.log to track what variables become all through the loop.

    Handy advice thank you very much. I ended up finding out the issue a couple of hours after posting. Apparently there are two Function systems in C3 and I was using the black one not the blue one (the new one). However, I didn't think they were completely different and had my return value code as the blue one while the function is the black one.

    I presume that what you mean is where the "hasValue" needs to check if a recipe contains an ingredient? I would use a separate function for that returns true or false

    I already do. The entire thing is divided into 3 main functions. The first one checks what kind of dictionary (potion) this is, the second checks if the ingredients are available. The third does the deduction and then returns to the first to make the actual crafting process.

    I ended up finding out that the interchanging use of actions and conditions from both Functions system is what messed up the whole thing.

    Hard to see without the c3p file, but I get the feeling that you should first loop through the crafting stations and add the ingredients to the array. And then UNINDENTED go loop trough the choosen ingredients. If you have 3 stations, and they all have 1 ingredient... You loop through the stations: in the first iteration, the ingredient array will have one value, and will never match a recipe, the second time round, still not, the third time round, maybe.

    So, again, I don't know exactly the circumstances, and game logic, but I would think that you would first do the filling of the array. and then, NOT as a sub event, on the same level as the for each loop, do the rest?

    That is exactly what I am doing right now. First I loop over the slots and get the ingredients there and add it to an array. Then I loop again (unindented) over the array (now populated with the ingredients player chose) and find out if the ingredients have enough to craft the potion. Then I loop a third time (unindented) to actually do the deduction if the previous loop checks out.

    My question is, is this normal or common that you end up looping a few times over the same data?

    Personally, I'm a huge fan of functions and separating parts of the logic into functions even though they only get used once.

    I am trying to do the same. Right now that process is divided into 3 main functions. But it gets a bit messy with the two functions systems and I did't know their rules yet (I knew the old one only) so I messed up a few things.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)