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

0 favourites
  • 11 posts
From the Asset Store
Pick Up Items Sound effects for your game, Take them for a ride right now and you will worry no more.
  • 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.

  • 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..

  • 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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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

  • 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.

  • Telyko: I already set the variables inside the buttons with (Button.value+1)%3 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.

  • I think it would be much easier to have 9 buttons with 9 different names and 9 variables.

    But that's just my opinion.

  • [quote:2yalq32w]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!

    [attachment=0:2yalq32w][/attachment:2yalq32w]

  • 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!

  • You're welcome! I hope you manage to do what you want.

  • I solved it thanks to sub-events. I post this just to share my solution.

    The layout is this:

    [attachment=1:hbkgxuk4][/attachment:hbkgxuk4]

    And my events are:

    [attachment=0:hbkgxuk4][/attachment:hbkgxuk4]

    This way each button cycles on each click through 3 possible states: 0 (empty), 1 (triangle) and 2 (squarircle). Sybols are invisible initially.

    I have only one definition for button and nine instances of it (instead of nine definitions with nine names, button1, button2...). The same for the symbols. Inside each element (buttons and symbols) I define an instance variable: order. Sub-events manage the symbol switch thanks to this variable. Maybe I should group things and so, but it's ok for now.

    Finally, enter key (the yellow square) works as ramones suggested with sub-events and concatenation.

    Hope this helps other people and thanks both of you again!

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