How do I Shorten my code? Functions and loops?

Get help using Construct 2

Post » Mon Dec 21, 2015 5:31 pm

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!
B
19
S
7
G
4
Posts: 36
Reputation: 3,625

Post » Mon Dec 21, 2015 10:44 pm

Yeah, functions
B
45
S
14
G
70
Posts: 1,950
Reputation: 41,730

Post » Tue Dec 22, 2015 12:24 am

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
B
91
S
31
G
98
Posts: 5,192
Reputation: 65,170

Post » Tue Dec 22, 2015 3:45 pm

perhaps like this:
Image

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.
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 787
Reputation: 3,786

Post » Tue Dec 22, 2015 4:14 pm

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.
Image
B
19
S
7
G
4
Posts: 36
Reputation: 3,625

Post » Tue Dec 22, 2015 4:55 pm

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):
- if / else
- use of for-loops
- use of while-loops
- data types (boolean, numeric, string, dictionary, array)
- functions
- how picking objects works
- expressions https://www.scirra.com/tutorials/77/nat ... onstruct-2 and https://www.scirra.com/manual/126/system-expressions


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++.
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 787
Reputation: 3,786

Post » Wed Dec 23, 2015 1:27 pm

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!
Image
B
19
S
7
G
4
Posts: 36
Reputation: 3,625


Return to How do I....?

Who is online

Users browsing this forum: Christmas and 2 guests