How do I pick all values from an instance var in an object?

Get help using Construct 2

Post » Fri May 16, 2014 8:50 am

Hi, guys.

I'm just starting with C2 Free Edition, just prototyping and evaluating the software. So maybe my question is pretty naive.

I have a Layout representing a 3x3 keyboard. This layout loads many times in my proto, because there are keyboards of this kind in many layouts ("rooms"). Each key is a Sprite ("Button"), which has an instance variable called "value". When you press each button its value cycles from 0 to 2 and back to 0 (0, 1, 2, 0, 1, ...). Players can introduce visual codes in the keyboard this way.

I have another button ('enter') to validate the entered code and I want to store it in a global as a single string, return to the original Layout and decide what to do, depending on the code entered. My problem is that I don't know how to generate, for example, a concatenated string from the nine independent values from each button. I want to store a combination like "012210022", with row after row, for example.

It's like I need a "for-each" loop, but not as an event, but as an action, and I have no clue about how to do this.

Any idea? :)

Thanks a lot in advance.
B
5
Posts: 7
Reputation: 239

Post » Fri May 16, 2014 9:07 am

I'm not sure I have understood exactly what you are doing with those multiple virtual 3X3(?) keyboards but I think I get the general idea.
You want to push a key on the keyboard, pushing it once moves that value from 0 to 1, pushing it again moves it from 1 to 2, and again, 0 to 1.
By hitting enter you want to store that value somewhere after all the other values of the previous pushed buttons?

I'd do hit like this:
On layout startup:
Set Global Text Variable RESULT = "" (empty)

on button "A" pressed:
if Global Number Variable A = 0 set it to 1
Sub event: Append Variable RESULT with: ""&VariableA (1)
if Global Number Variable A = 1 set it to 2
Sub event: Append Variable RESULT with: ""&VariableA (2)
if Global Number Variable A = 2 set it to 0
Sub event: Append Variable RESULT with: ""&VariableA (3)

on button "B" pressed:
etc..
B
17
S
3
G
3
Posts: 89
Reputation: 2,825

Post » Fri May 16, 2014 9:18 am

Thanks a lot, Telyko!

This solution writes directly on the result each new value, but it is not exactly what I want. I can do something similar by using this method, but is not very practical if I have to change something in my code in the future.

Your method appends new results as the buttons are pressed, I understand. I do not want to append on each button pressed: I need to have a 9 digit string each time the enter is pressed. This 9 digit string represents the state of the keyboard.

With your solution, if i'm not wrong, if you continuously press one single button, RESULT will store something like 0120120120120120..., because it keeps appending on each push. I want to update just the instance variable of each object and collect them as a single 9 character string when the "enter" button is pressed.

Thanks anyway! Fast answer :D
B
5
Posts: 7
Reputation: 239

Post » Fri May 16, 2014 9:25 am

Oh I see.
Well if you just have 9 keys then you could create nine Variables (VarKey1 VarKey2,etc..) , one for each key, and when you push "Enter"

Set RESULT text variable = VarKey1&VarKey2&VarKey3&VarKey4&VarKey5etc...

Use the above method to set the numbers, just remove the sub events
B
17
S
3
G
3
Posts: 89
Reputation: 2,825

Post » Fri May 16, 2014 9:26 am

You could give each button a number variable (1-9) and then use 'for each: ordered' to loop through each button ordered by number and append it's value to a text variable.
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,670

Post » Fri May 16, 2014 9:34 am

@Telyko: I already set the variables inside the buttons with (Button.value+1)%3 :D I just wanted not to use independent variables for this, but instance variables. And, if I have 9 variables and only one object "Button", I can't tell which instance of the button is being pressed each time.

@ramones: The problem with that is that the "for each: ordered" (which I tried using UID as test) is an event structure, not an action, so it is not triggered by a button or any other event but it keeps running all the time (useful for update collections of entities inside the game loop, I suspect). If I could do this as a triggered event after a click, It would be perfect.
B
5
Posts: 7
Reputation: 239

Post » Fri May 16, 2014 9:50 am

I think it would be much easier to have 9 buttons with 9 different names and 9 variables. :)
But that's just my opinion.
B
17
S
3
G
3
Posts: 89
Reputation: 2,825

Post » Fri May 16, 2014 9:53 am

The problem with that is that the "for each: ordered" (which I tried using UID as test) is an event structure, not an action, so it is not triggered by a button or any other event but it keeps running all the time (useful for update collections of entities inside the game loop, I suspect). If I could do this as a triggered event after a click, It would be perfect.

Sounds like you haven't discovered... sub-events!

buttonValues.PNG
You do not have the required permissions to view the files attached to this post.
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,670

Post » Fri May 16, 2014 10:04 am

@telyko: I have just done that and it works :) It's just that, as a programmer, it hurts me to repeat declarations instead of instances xD I can use this as a learning example, but I'd rather prefer another solution...

@ramones: ... as this! xD Yep. Exactly. I have just discovered sub-events. Thanks to both of you. In the first example from @telyko I understood 'sub-events' as additional conditions. Oh. I love being a n00b :)

I'm sure that a mix of your answers is the correct answer for me, so thanks a lot for the effort and the explanations!
B
5
Posts: 7
Reputation: 239

Post » Fri May 16, 2014 10:13 am

You're welcome! I hope you manage to do what you want. :)
B
17
S
3
G
3
Posts: 89
Reputation: 2,825

Next

Return to How do I....?

Who is online

Users browsing this forum: 8tomo8, brunopalermo and 10 guests