How do I Shorten my code? Functions and loops?

0 favourites
  • 7 posts
From the Asset Store
5 levels with simple coding Source-code (.c3p) + HTML5 Exported
  • Good day everyone,

    Let me at first justify my post on this busy forum: I searched everywhere and tried to fix the problem myself, but, well, it didn't work. I am using free version of Construct 2, right now just testing the possibilities, and I have basic code experience - and that is what the questions is going to be about. I was able to implement so far all ideas I had in mind, and now I am trying to shorten my events amount and learn more advanced event handling.

    So, long story short:

    On game field there are n-number of substances. Picking two different substances creates a new potion. That potion moves automatically to the 2-container-inventory, and gets ready to be used (by a player).

    A couple of rules:

    1. There cannot be more than two potions in inventory.

    2. Substances cannot be gathered if both inventory slots are taken.

    3. If you already have substanceN, you cannot take another substanceN.

    These all described above already works in the following event system. Now, I am trying to shorten my code and try learning/using advanced options Construct 2 has to offer.

    Description of the code (.capx - I canot post URLs due to my low reputation, so please, just remove spaces in the following: http:// 1drv.ms /1OhN5iz ). It will lead to my cloud with access only to this file.

    (First group - Dictionaries)

    I used two dictionaries to store amount of potions and substances. Global variables were used before, and moving to a dictionary helped to decrease the amount of events roughly by 20.

    (Second group - substanceGathering)

    On collision with player substances get destroyed and are moved to the dictionary. System checks the total amount of potions in 2 container inventory (an array), and permits gathering if the amount is 2 or more.

    (Third group - potionsBrewing)

    System checks whether there is a possible combination of two substances, and immediately creates/brews a potion, removing two substances used.

    (Fourth group - inventoryGetsPotions)

    System checks whether there is a potion in the dictionary and moves it to the 2-container-inventory (an array), removing it from the dictionary. Here an IF-Else statement helps to move potion to an empty space.

    Wuh, that is quite some read up here! So, the questions to users:

    1. Second group - has a lot of similar code, is it possible to shorten it down to one? Would functions help out here?

    2. Fourth group - I was thinking to use a loop here, to shorten the code, but I am not sure how to do it. I do can formulate it in a sentence, but just can't make the concept in Construct 2. So, should be a loop be like this:

    1. System checks if any of PotionN Dictionary entries are equal to 1.

    2. System moves that very same PotionN to the array, using same If/Else statement.

    3. System sets that PotionN entry to 0.

    3. If a second Player comes into the play, is there a way to optimize this whole code for it, using again, functions?

    I am User Interface, User Experience designer, and games are my passion. I think you can see that I am not a great coder, but constant practicing should lead to a good output, I hope

    I really appreciate your help and feedback on the code, thank you for your time!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Yeah, functions

  • Some functions, but you can also make things simpler by not using a sprite per substance, instead have one substance object, but multiple instances and use a an instance variable to distinguish the two. If you do that you can reduce the amount of events down to five even if you have hundreds of substances.

    For multiple players I'd just add multiple instances of your player object and add a variable to distinguish between the two. The dictionaries relate to a single player so I'd put them in a container so each player has their own. Then adding a "for each player" to the last three events should get you most of the way there. The function will need to be modified so the player.uid is passed as well, then the first thing the function should do is pick by uid the passed value. You can do it without a container with variables but I don't feel like working that out.

    OBJECTS:

    Dictionaries: sub_dict, potion_dict

    Sprite sub_sprite with text variable "name". Set the value of "name" to the name of the substance.

    Spritevplayer with text variables "slotA" and "slotB". Both should start with a value of "".

    EVENTS:

    player: on collision with sub_sprite

    [inverted] sub_dict: has key sub_sprite.name

    system: compare: player.slotA+player.slotB <2

    --- sub_dict: add key sub_sprite.name with value 1

    on function "make potion?"

    sub_dict: has key function.param(0)

    sub_dict: has key function.param(1)

    --- potion_dict: add key fuction.param(3) with value 1

    --- sub_dict: delete key function.param(0)

    --- sub_dict: delete key function.param(1)

    every tick

    --- call function "make potion?" ("substance1","substance2","potion1")

    --- call function "make potion?" ("substance1","substance3","potion2")

    --- call function "make potion?" ("substance2","substance3","potion3")

    --- ... and so on

    Potion_dict: for each key

    player: slotA = ""

    --- player: slotA set to Potions.CurrentKey

    --- sub_dict: delete key Potions.CurrentKey

    Potion_dict: for each key

    player: slotB = ""

    --- player: slotB set to Potions.CurrentKey

    --- sub_dict: delete key Potions.CurrentKey

  • perhaps like this:

    https://copy.com/q764O1j4hVDisxEY

    If you have multiple players, you would need to put the inventory and playersubstances array into the container of player.

    Doing stuff every tick like checking if any of the potions can be made or mobed really isn't what you should do, performancewise. Code should only run when you collect a substance imo.

  • plinkie, thanks for your input, that was quite... informative

    R0J0hound, thank you so much for your reply! I was able to create exactly what I was thinking about, and learned more about functions and loops. Also, that is a great way of using instant variables on cloned instances, didn't know it was possible.

    mindFaQ,

    I just opened your .capx and it took quite some time to understand how this works. It is definitely a completely different way of making the exact thing, it is fascinating!

    And, as you said it would be better for performance, since the first prototype is already lagging. I will try to redo with the way you showed, and see how that works out.

    Could you also, if possible, advice on more code learning? Basics are covered already, but I dont know where to start with more advanced level.

    Again, thank you for your time and efforts, I will be back when i get the second prototype working.

  • Your example didn't lag for me. But I think it is good practice to only run code when necessary.

    I'm not very familiar with the current learning ressources besides the manual. I think all the functionality besides own javascript plugins etc. is covered there. The examples of C2 give some additional hands on experience.

    I can't give the best tipps, because I'm not a pro myself.

    Important things to know (in no particular order):

    The hard thing currently for me is organizing and writing my code in a way, so that it is not (strongly) intertwined with itself. That's why currently I'm looking at some programming design patterns ( http://gameprogrammingpatterns.com/contents.html ). It might be hard to follow, if you are not comfortable with a programming language similar to C++.

  • mindFaQ,

    Thank you again, it is very helpful. Well, i am off to study the possibilities, and see what I can mak2e with them.

    PS: The prototype I was talking about is not the one above, but the one I made after R0J0hound's advice.

    R0J0hound,

    I used your code, though, changed it a bit so loops run only when a certain condition is met first. Thus, decreasing the load the game puts on the system. It works smoothly!

    Thank you guys again, the community here is brilliant!

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