[SOLVED] ARRAY How do I check for value of an entire row?

Get help using Construct 2

Post » Sat Jan 17, 2015 4:09 am

I am planning on using an array to handle my weapon inventory, selection, and ammo for my game. I have it set up to be 6 x 3 x 1

I am new to arrays so bare with me. As I have it set up now, I have a row setup for checking if the weapon is collected. 1 being yes and 0 being no. I was planning on using this method to make it easy to cycle through weapons.

arrayr505.png


Basically I was planning this:

Global Variable :
selectedWeapon = ""

OnStart:
Set up array
On collection of weapon add to the array & update ammo on weapon selection

event.png


Weapon selection (basic)
Cycle down
on 1 pressed | subtract 1 from selectedWeapon
- selectedWeapon = 1 | set selectedWeapon to 6

Cycle Up
on 2 pressed | add 1 to selectedWeapon
- selectedWeapon = 6 | set selectedWeapon to 1

On cycle check for available weapons
Array Check for value at (0,2) = 0 | got to next weapon
Array Check for value at (0,2) = 1 | select weapon

Which is rather redundant to create a bunch of event to check for a value.

My question is how do I check all of the values in the row I have designated for setting if weapon is collected?

Thanks
You do not have the required permissions to view the files attached to this post.
Last edited by facecjf on Sat Jan 17, 2015 7:55 am, edited 1 time in total.
Image Image Image
B
61
S
19
G
6
Posts: 325
Reputation: 7,944

Post » Sat Jan 17, 2015 5:05 am

Not sure if this answers directly, but I have a few suggestions (hope you don't mind)

With your events 63-65, I suggest you use a mechanism like
weapons: Value at(selectedWeapon - 1, 2) = 1

and its action to
gun: Set gun_type to GunNames.At(selectedWeapon)
(GunNames is an array of the gun names) then you won't need separate code for every weapon.

Also in that block, the second check for Value.at(4, 2) is redundant (as in, rearrange the logic)

With the weapon number cycling, set selectedWeapon to something like
(selectedWeapon + 1) % 5


General:
Use constant values for array indexes, e.g. COLLECTED = 2, AMMO = 1, NAME = 0 then your code will be less error prone and easier to read, e.g.:
weapons: Value at( selectedWeapon, COLLECTED) = 0
or
weapons: Value at( selectedWeapon, AMMO) = 0


Finally, you might want to start your 'selected weapon' values from 0 instead of 1 to match the way arrays work. You'll find yourself doing -1 and +1 a lot, and it can be tedious and error prone.

Anyway, to check for a value in a row, use either a System: For loop or an Array: for-each loop.

Obviously, choose to use any or none of these, but if you're new to arrays then get into good practices from the start :)
Last edited by codah on Sat Jan 17, 2015 7:38 am, edited 1 time in total.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Sat Jan 17, 2015 5:45 am

@codah

It's not a direct answer, but rather informative, which really helps me understand. This seems like a far more elegant approach. I didn't even think to pass a variable as the value in the array due to my unconventional ordering ;) Thank you!

If i start my 'selected weapon' values from 0 instead of 1 to match the way arrays work, than I assume an event mechanism like this :
weapons: Value at(selectedWeapon - 1, 2) = 1


would become this: ?

weapons: Value at(selectedWeapon, 2) = 1


Thanks again
Image Image Image
B
61
S
19
G
6
Posts: 325
Reputation: 7,944

Post » Sat Jan 17, 2015 5:58 am

facecjf wrote:If i start my 'selected weapon' values from 0 instead of 1 to match the way arrays work, than I assume an event mechanism like this :
weapons: Value at(selectedWeapon - 1, 2) = 1


would become this: ?

weapons: Value at(selectedWeapon, 2) = 1


Thanks again


exactly

Basically if you arrange your values and code to suit arrays from the start, you can possibly avoid writing LOTS of code. Like an order of magnitude.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Sat Jan 17, 2015 7:30 am

@codah

Thank you for your help!

https://vine.co/v/OjiLqtmuOea
Image Image Image
B
61
S
19
G
6
Posts: 325
Reputation: 7,944

Post » Sat Jan 17, 2015 7:37 am

facecjf wrote:@codah

Thank you for your help!

https://vine.co/v/OjiLqtmuOea


that's awesome!
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596


Return to How do I....?

Who is online

Users browsing this forum: Blendi1223, gskunk, richcoz81, tarek2, Wartexnik and 43 guests