How do I tidy up keypress and variable ranges?

Just started using Construct 2? Post your questions here

Post » Sat Dec 26, 2015 5:46 pm

Image

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?
Last edited by Zebbi on Sat Dec 26, 2015 7:18 pm, edited 1 time in total.
B
41
S
12
G
14
Posts: 1,117
Reputation: 11,248

Post » Sat Dec 26, 2015 6:14 pm

The image did not attach itself to the post.
Brick by brick, stone by stone.
B
51
S
23
G
14
Posts: 273
Reputation: 11,038

Post » Sat Dec 26, 2015 7:18 pm

Whoops! Image
B
41
S
12
G
14
Posts: 1,117
Reputation: 11,248

Post » Sat Dec 26, 2015 9:01 pm

For the first one if the value of CurrentWeap never exceeds 9 you could do this:
Code: Select all
every tick
--- set animation frame to int(mid("0123345", CurrentWeap, 1))


Or you could do this if you want to handle double digit values:
Code: Select all
every tick
--- set animation frame to int(tokenat("0,1,2,3,3,4,5", CurrentWeap))



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: Select all
on any key pressed
system: Keyboard.LastKeyCode is between values (49,57)
--- set CurrentWeap to Keyboard.LastKeyCode-49
B
88
S
30
G
89
Posts: 5,109
Reputation: 60,329

Post » Sat Dec 26, 2015 9:53 pm

R0J0hound wrote:For the first one if the value of CurrentWeap never exceeds 9 you could do this:
Code: Select all
every tick
--- set animation frame to int(mid("0123345", CurrentWeap, 1))


Or you could do this if you want to handle double digit values:
Code: Select all
every tick
--- set animation frame to int(tokenat("0,1,2,3,3,4,5", CurrentWeap))



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: Select all
on any key pressed
system: Keyboard.LastKeyCode is between values (49,57)
--- set CurrentWeap to Keyboard.LastKeyCode-49

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?
B
41
S
12
G
14
Posts: 1,117
Reputation: 11,248

Post » Sat Dec 26, 2015 10:37 pm

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.
B
88
S
30
G
89
Posts: 5,109
Reputation: 60,329

Post » Sat Dec 26, 2015 10:53 pm

R0J0hound wrote:
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?
B
41
S
12
G
14
Posts: 1,117
Reputation: 11,248

Post » Sun Dec 27, 2015 6:49 pm

That sounds like it would work. There isn't really a best way.
B
88
S
30
G
89
Posts: 5,109
Reputation: 60,329

Post » Sun Dec 27, 2015 7:35 pm

R0J0hound wrote: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?
B
41
S
12
G
14
Posts: 1,117
Reputation: 11,248

Post » Sun Dec 27, 2015 8:58 pm

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)
B
88
S
30
G
89
Posts: 5,109
Reputation: 60,329

Next

Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 1 guest