How do I tidy up keypress and variable ranges?

0 favourites
  • 12 posts
From the Asset Store
A complete set of graphical user interface (GUI) to build 2D video games. Suitable for casual games
  • As you can, this looks pretty bad, it would be so much nicer to have a keypress check that compares the range and sets the appropriate value, as would the global check. What's the correct way of doing this with expressions?

  • The image did not attach itself to the post.

  • Whoops!

  • For the first one if the value of CurrentWeap never exceeds 9 you could do this:

    every tick
    --- set animation frame to int(mid("0123345", CurrentWeap, 1))[/code:1kn81aia]
    
    Or you could do this if you want to handle double digit values:
    [code:1kn81aia]every tick
    --- set animation frame to int(tokenat("0,1,2,3,3,4,5", CurrentWeap))[/code:1kn81aia]
    
    For the second bit I'd utilize key codes.
    "1" has a key code of 49
    "2" has one of 50
    ..
    "9" has one of 57
    
    You can make a simple capx to inspect the keycode values of certain keys if you wish or there's probably a reference online somewhere.
    
    So basically you then can do this:
    [code:1kn81aia]on any key pressed
    system: Keyboard.LastKeyCode is between values (49,57)
    --- set CurrentWeap to Keyboard.LastKeyCode-49[/code:1kn81aia]
  • For the first one if the value of CurrentWeap never exceeds 9 you could do this:

    every tick
    --- set animation frame to int(mid("0123345", CurrentWeap, 1))[/code:nqvtjblm]
    
    Or you could do this if you want to handle double digit values:
    [code:nqvtjblm]every tick
    --- set animation frame to int(tokenat("0,1,2,3,3,4,5", CurrentWeap))[/code:nqvtjblm]
    
    For the second bit I'd utilize key codes.
    "1" has a key code of 49
    "2" has one of 50
    ..
    "9" has one of 57
    
    You can make a simple capx to inspect the keycode values of certain keys if you wish or there's probably a reference online somewhere.
    
    So basically you then can do this:
    [code:nqvtjblm]on any key pressed
    system: Keyboard.LastKeyCode is between values (49,57)
    --- set CurrentWeap to Keyboard.LastKeyCode-49[/code:nqvtjblm]
    

    Thanks for that! I'm still working out how to use mid, int, str, zeropad, etc, so this is really helpful to have it examplified for me. Is using every tick a good idea for this kind of thing? Would it be better practice if I only had things happen when they changed, either by putting it in a function or testing for a keypress? I could be wrong, just seeing if every tick is expensive in processing terms or not?

  • [quote:1op3hxic]Would it be better practice if I only had things happen when they changed, either by putting it in a function or testing for a keypress? I could be wrong, just seeing if every tick is expensive in processing terms or not?

    I'd say it's irrelevant for something as simple as setting a variable.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • [quote:hnctqkjx]Would it be better practice if I only had things happen when they changed, either by putting it in a function or testing for a keypress? I could be wrong, just seeing if every tick is expensive in processing terms or not?

    I'd say it's irrelevant for something as simple as setting a variable.

    That's what I thought, thanks for clarifying. Can this method be used to recall data from an aray? I was going to have a 2d array that has the ammo amount and the max ammo for each weapon. Would this be the best way of storing and recalling the info for 6 weapons, or would it be better to use 6 different variables for storing?

  • That sounds like it would work. There isn't really a best way.

  • That sounds like it would work. There isn't really a best way.

    Seems to be working well so far! Is there a way of cloning one column of cells into a duplicate second column? I want to do the old counting counter trick (usually done with two variables, one that staggers the total and adds/subtracts it in nanoseconds until it reaches the sum of the other) but I need to do it with the array, is this possible?

  • Probably something like this to set column 1 to column 0:

    set array at (1,0) to array.at(0,0)

    set array at (1,1) to array.at(0,1)

    set array at (1,2) to array.at(0,2)

    set array at (1,3) to array.at(0,3)

    set array at (1,4) to array.at(0,4)

    ...

    set array at (1,array.height-1) to array.at(0,array.height-1)

    But that just screams "use a loop", so...

    repeat array.height times

    ---set array at (1,loopindex) to array.at(0,loopindex)

  • Probably something like this to set column 1 to column 0:

    set array at (1,0) to array.at(0,0)

    set array at (1,1) to array.at(0,1)

    set array at (1,2) to array.at(0,2)

    set array at (1,3) to array.at(0,3)

    set array at (1,4) to array.at(0,4)

    ...

    set array at (1,array.height-1) to array.at(0,array.height-1)

    But that just screams "use a loop", so...

    repeat array.height times

    ---set array at (1,loopindex) to array.at(0,loopindex)

    Thank you so much!! I can't thank you enough for helping me with those loops, I've learned a lot from this. Here's the finishing code:

    That keeps a second columns (I changed it around so they're columns rather than how I had it with rows, too hard for me to visualize!) updating and I just update the score like so:

    Works well so far! Any side-effects I might encounter, performance-wise?

  • I've also sorted out the max-ammo by using the third column:

    I set the ammo amount in the first condition, and force the amount in column 0 to not exceed the amount in column 2, seems okay!

    Haven't quite worked out if all of this will work when ammo is depleted by firing, I'll find out soon enough though!

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