How do I streamline this?

Just started using Construct 2? Post your questions here

Post » Fri Feb 13, 2015 5:16 pm

Very new at Construct 2.

I'm working on a tool for playtesting a card game prototype for an RPG combat system.

So far, I've cobbled together my code mostly through trial and error. I'd like to know if there are places where I could use things like arrays or functions to make my life easier, and use fewer lines of code.

Basically, my problem is that I don't know when I'm being inefficient, and when I just need to soldier on, and input that data line-by-line.

PI Calculator.capx


You need to run beginning with the 'Mission Select' layout to allow everything to function properly, such as it is.

Thanks in advance for any help!
You do not have the required permissions to view the files attached to this post.
B
3
Posts: 4
Reputation: 183

Post » Fri Feb 13, 2015 5:46 pm

I've only had a quick look at it but yeah it looks like you could clean things up a lot. Repetition like this can usually be reduced with loops/functions:
repetition.PNG

Also when you have sequential variables like player1, player2, player3, player4,etc it's often better to use an array.

I'll look it more detail later/tomorrow.
You do not have the required permissions to view the files attached to this post.
B
55
S
28
G
18
Posts: 1,520
Reputation: 25,285

Post » Sat Feb 14, 2015 8:57 pm

Starting with the Hero Select sheet:
I added a heroes array to hold the selected heroes. Then when you select a hero you just add them to the end of the array if they're not already in it:
Image
Removing a hero is just removing the last value from the array:
Image
I have a function for displaying all the values in the array:
Image
And at the top of the skills event sheet I set the global party member variables to the array values (just to keep everything else working for now).
Image
You do not have the required permissions to view the files attached to this post.
B
55
S
28
G
18
Posts: 1,520
Reputation: 25,285

Post » Sat Feb 14, 2015 10:10 pm

Thanks so much for doing all of this! Do you mind if I take a closer look over the next day or so, and get back to you with some follow-up questions?
B
3
Posts: 4
Reputation: 183

Post » Sat Feb 14, 2015 10:27 pm

Sure, go for it. I went on to the skills select sheet. To remove all those duplicate actions in the screen shot I posted in my first post I created a function updatePreview(type, frame). It takes the type of skill ("attack", "block", "magic") and the frame number and shows that card:
Image

Then you can replace each group of 5 actions with a call to the function:
Image

Still lots of duplication but it's a start. I like to refactor in small chunks, hopefully keeping everything working in between changes.
You do not have the required permissions to view the files attached to this post.
B
55
S
28
G
18
Posts: 1,520
Reputation: 25,285

Post » Sun Feb 15, 2015 8:03 pm

I've added an array called skills with an instance variable heroName to store all of the heroes skills for the skill select screen. I create an instance of the array for each hero and have a function addSkill() that adds a skill to the skills array.

The function adds a skill name, type and frame number to the end of the array:
Image

The skills array looks like this:
Image

Now updating the preview can be reduced down to two events.
Select the skills array for the current hero, find the index of the skill name in the array, and then call the updatePreview() function with the skill name and frame number from the skills array:
Image

Setting up the skill name textboxes can be reduced to a loop as well.
Select the skills array for the active hero, loop through the array selecting the skillName text for each skill, setting its text to the skill name in the array and setting its color based on the skill type in the array.
Image

In fact this block of events is the same for every hero except for the line that sets the animation frame so I just took that part out and put the whole thing into a function called displaySkills(), to be called whenever you switch heroes.
Image
You do not have the required permissions to view the files attached to this post.
B
55
S
28
G
18
Posts: 1,520
Reputation: 25,285

Post » Wed Feb 18, 2015 3:43 am

Wow. You have totally overhauled this thing. I'm still going over everything you've done, trying to understand what I can before I come back with questions, but thank you so much for your help thus far.
B
3
Posts: 4
Reputation: 183

Post » Fri Feb 20, 2015 8:06 pm

Okay, I've adjusted my code for ES-Skill Select, and as far as I can tell, it's identical to yours. This is clearly not the case, however, because mine's not working properly. Also, I have tons of questions.

Problems:
1) In Hero Select, the "members" array in your example shows up blank. Mine displays "Text" both when I'm working on the layout, and when I run it. Otherwise it works fine.

2) In Skill Select, preview functionality isn't working. For some reason, the "frame" parameter doesn't change from its initial value, and always shows frame 0... actually, this problem seems to have magically remedied itself since I began writing this response. Could Construct 2 confuse your capx (with edits) with my project when I open them both at once? Weird.

Questions (apologies, my notes are a little scattered, so these might jump around a bit):
1) Line 5:
a. Why do you destroy the initial "skills" array?

b. I assume that I'll just need to add a new array (with concomitant "addSkills" data) for each additional character I want the Skill Select layout to recognize. Is this correct?

2) Function.Param: I think when you set local variables equal to this expression, you're essentially creating a slot for information needed in order for the function to run. For example: when setting the parameters for the "addSkill" function, you set heroName = Function.Param(0), which means that, on calling this function, a heroName value will have to be supplied in order for the function to do the stuff it's supposed to do. Is my understanding correct?

3) Line 46: I think this line is taking the information provided when addSkills is called to populate the active member's "skills" array. The first action puts the skillName (supplied in the "addSkills" section beginning with Line 6) into the skills array. The next two lines seem to be filling in the type and frame data in the array, but I don't understand why their location in the array is expressed at (self.Width-1, 1) and (self.Width-1, 2).

4) Is it correct to think of each line of data in the array (ie "Arthur", "attack", 0) as a point in a Cartesian coordinate system? Is there a better term for describing this than "line of data in the array"?

5) What establishes the CurX value for the skills array? (relevant to Line 53) What is the condition which sets the value for skillNumber doing?

6) Line 37: By the transitive property of equality, the parameters given for the "updatePreview" call are skills.At(skillName.Name, 1), (skillName.Name, 2). I think these correspond to the type and frame values in "the line of data" in the "skills" array that corresponds with that particular skillName.Name. Is that correct?

7) As you may have surmised from my previous questions, I am having trouble understanding the relationship between various elements in arrays. Is ["Arthur", "attack", 0] a single data point, or three data points? Which (if any) of these correspond with Width, Height, Depth? Why, when establishing the parameters for a function call, is it written (self.Width-1, 1) or (skillIndex, 1)? These remind me of (x,y) coordinates, which is perhaps causing some confusion.

Thanks again for your help.
You do not have the required permissions to view the files attached to this post.
B
3
Posts: 4
Reputation: 183

Post » Fri Feb 20, 2015 11:03 pm

I have to try remember what I did now...

Problems:
1) In Hero Select, the "members" array in your example shows up blank. Mine displays "Text" both when I'm working on the layout, and when I run it. Otherwise it works fine.

That's just the default text showing before you set it to anything. You can change that in the text properties in the editor. Or on start of layout set the text to "".
2) In Skill Select, preview functionality isn't working. For some reason, the "frame" parameter doesn't change from its initial value, and always shows frame 0... actually, this problem seems to have magically remedied itself since I began writing this response. Could Construct 2 confuse your capx (with edits) with my project when I open them both at once? Weird.

Dunno...
Questions (apologies, my notes are a little scattered, so these might jump around a bit):
1) Line 5:
a. Why do you destroy the initial "skills" array?

Good question. I could've just used that array for the first hero.
b. I assume that I'll just need to add a new array (with concomitant "addSkills" data) for each additional character I want the Skill Select layout to recognize. Is this correct?

Yes but maybe the next step would be to put the hero data into separate project files and then loop through a list of heroes, creating a skills array for each one and loading the data from the matching file.
2) Function.Param: I think when you set local variables equal to this expression, you're essentially creating a slot for information needed in order for the function to run. For example: when setting the parameters for the "addSkill" function, you set heroName = Function.Param(0), which means that, on calling this function, a heroName value will have to be supplied in order for the function to do the stuff it's supposed to do. Is my understanding correct?

When you call a function like this:
Image
The parameters you set there can be accessed inside the function as Function.Param(0), Function.Param(1), etc.
The local variable are just used to give them more readable names.
It's nicer to say "Set array value to frameNumber" rather than "Set array value to Function.Param(3)".
3) Line 46: I think this line is taking the information provided when addSkills is called to populate the active member's "skills" array. The first action puts the skillName (supplied in the "addSkills" section beginning with Line 6) into the skills array. The next two lines seem to be filling in the type and frame data in the array, but I don't understand why their location in the array is expressed at (self.Width-1, 1) and (self.Width-1, 2).

Yeah so the array starts with a width of 0 and a height of 3. After 3 skills are added it looks like this:
Image
When you call the function to add another skill, first it pushes the skill name onto the array. That creates a new column:
Image
Then you want to put the type in the last column (width - 1) and row 1
And the frame number goes in the last column, row 2:
Image

4) Is it correct to think of each line of data in the array (ie "Arthur", "attack", 0) as a point in a Cartesian coordinate system? Is there a better term for describing this than "line of data in the array"?

Yeah, ignoring the negative axes, you can imagine each element in the array having an x, y, z position. The different heroes are along the x axis and their properties are along the y axis. Or a 2d array just think of a spreadsheet :)
5) What establishes the CurX value for the skills array? (relevant to Line 53) What is the condition which sets the value for skillNumber doing?

The "skills: For each X element" condition is looping through the x axis of the array and it sets the CurX value to the current x value as it loops.
The other condition is picking the skillName text object that for each skill. So for the first element in the array, CurX = 0, it picks the text object with skillNumber = 1. Next element, CurX = 1, pick text with skillNumber = 2...
6) Line 37: By the transitive property of equality, the parameters given for the "updatePreview" call are skills.At(skillName.Name, 1), (skillName.Name, 2). I think these correspond to the type and frame values in "the line of data" in the "skills" array that corresponds with that particular skillName.Name. Is that correct?

Almost, only it's getting the index of the skillName. So yeah you have the name and you need to find the type and frameNumber to pass on to updatePreview().
skills.IndexOf(SkillName.Name) gets the position of the name in the array and you can get the type and frame at that position.
In the image above, if the skill was "Empathy" then skills.IndexOf("Empathy") = 3, and skills.At(3, 1) = "magic", skills.At(3, 2) = 8.

7) As you may have surmised from my previous questions, I am having trouble understanding the relationship between various elements in arrays. Is ["Arthur", "attack", 0] a single data point, or three data points? Which (if any) of these correspond with Width, Height, Depth? Why, when establishing the parameters for a function call, is it written (self.Width-1, 1) or (skillIndex, 1)? These remind me of (x,y) coordinates, which is perhaps causing some confusion.

Maybe it makes more sense now? Width, Height, Depth = X, Y, Z. If the Depth is set to 1 then you have a 2D array, like a 2D graph or a spreadsheet. If the Height is set to 1 as well then you have a 1D array; that's just a list.

Also, self just refers to the current object. So the action:
skills: Set value at (self.Width - 1, 1) to type
is the same as
skills: Set value at (skills.Width - 1, 1) to type
B
55
S
28
G
18
Posts: 1,520
Reputation: 25,285


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 0 guests