• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

still cant wrap my head around 2d arrays

F

fxokz

Guest
I know what a 2d array is... its a variable that holds multiple values inside a grid. I have that all figured out. The problem arises when i actually try to use them.

Im trying to make a system where there are different types of weapons dropped on the map that a player can pick up and use. For now im just trying to implement an automatic gun so rather than a semi auto that the player starts off with. HOW... HOW i can make a 2d array that holds some values but then what?
 
R

renex

Guest
Dude... you need to be more specific.

Do you mean you want to have a column with the weapon properties? Or a column that tells you what weapons the player has already picked up? Maybe you also want a column to hold the amount of ammunition for each weapon.

While we're at it, I suggest you look into enums; they make using arrays easier, because you can call each position a literal name that is readable.

Code:
enum weapon {
    knife,
    pistol,
    machinegun,
    bazooka,
    nuke
}
enum weaponProperty {
    have,
    ammo
}
You can put this code anywhere, so you might aswell create a script called "enums" and have all your enums in there. You don't even have to call it. With this set up, you can now call array positions like this:

Code:
playerCharacter.weaponArray[ weapon.machinegun, weaponProperty.have ]=true;
playerCharacter.weaponArray[ weapon.machinegun, weaponProperty.ammo ]=30;
or something like that. This is easier than just using the array raw. The above code is functionally identical to
Code:
playerCharacter.weaponArray[ 2, 0 ]=true;
playerCharacter.weaponArray[ 2, 1 ]=30;
but you don't need to remember all those numbers.

Now, when you actually want the player character to use a weapon... you can access the array in a sequence as usual.

Code:
currentWeapon=weapon.machinegun;
weaponArray[ currentWeapon, weaponProperty.ammo ]-=1;
 
Last edited:
F

fxokz

Guest
Dude... you need to be more specific.

Do you mean you want to have a column with the weapon properties? Or a column that tells you what weapons the player has already picked up? Maybe you also want a column to hold the amount of ammunition for each weapon.

While we're at it, I suggest you look into enums; they make using arrays easier, because you can call each position a literal name that is readable.

Code:
enum weapon {
    knife,
    pistol,
    machinegun,
    bazooka,
    nuke
}
enum weaponProperty {
    have,
    ammo
}
You can put this code anywhere, so you might aswell create a script called "enums" and have all your enums in there. You don't even have to call it. With this set up, you can now call array positions like this:

Code:
playerCharacter.weaponArray[ weapon.machinegun, weaponProperty.have ]=true;
playerCharacter.weaponArray[ weapon.machinegun, weaponProperty.ammo ]=30;
or something like that. This is easier than just using the array raw. The above code is functionally identical to
Code:
playerCharacter.weaponArray[ 2, 0 ]=true;
playerCharacter.weaponArray[ 2, 1 ]=30;
but you don't need to remember all those numbers.
I can make a column with weapon properties such as what bullets to fire and whether the weapon is auto or semi but yeah then i have trouble making the array work. Like for example
Code:
Current_gun = weapons[what the hell do i write, this is so confusing]
How do i use the array to determine what gun i have if the array only holds the properties of the weapon. Also how am i supposed to tell gamemaker that a gun is auto. Im rambling much more than usual because im using mobile at thr moment. When i get home ill try to implememt what you wrote above.
 

John Andrews

Living Enigma
I can make a column with weapon properties such as what bullets to fire and whether the weapon is auto or semi but yeah then i have trouble making the array work. Like for example
Code:
Current_gun = weapons[what the hell do i write, this is so confusing]
How do i use the array to determine what gun i have if the array only holds the properties of the weapon. Also how am i supposed to tell gamemaker that a gun is auto. Im rambling much more than usual because im using mobile at thr moment. When i get home ill try to implememt what you wrote above.
Man, lemme see if I can help ya, an array is a grid, a matrix, a cuadricle, etcetera, right? So, if you want to have an array with a series of weapons and their properties, it's very easy just do this for example, I will do this example with 3 weapons, 3 properties each:
Code:
weapons[1,1] = "Pistol" //Name of the 1st gun
weapons[1,2] = "Semi Automatic" //type of the 1st gun
weapons[1,3] = 10 //damage of the 1st gun

weapons[2,1] = "SMG" //Name of the 2nd gun
weapons[2,2] = "Automatic" //type of the 2nd gun
weapons[2,3] = 7 //damage of the 2nd gun

weapons[3,1] = "Rifle" //Name of the 3rd gun
weapons[3,2] = "Automatic" //type of the 3rd gun
weapons[3,3] = 12 //damage of the 3rd gun
See what I did there? if not, tell me so I can draw a picture of what I did so you can understand.
 

FrostyCat

Redemption Seeker
I can make a column with weapon properties such as what bullets to fire and whether the weapon is auto or semi but yeah then i have trouble making the array work. Like for example
Code:
Current_gun = weapons[what the hell do i write, this is so confusing]
How do i use the array to determine what gun i have if the array only holds the properties of the weapon. Also how am i supposed to tell gamemaker that a gun is auto. Im rambling much more than usual because im using mobile at thr moment. When i get home ill try to implememt what you wrote above.
renex added this example after you posted.
Code:
currentWeapon = weapon.machinegun;
weaponArray[currentWeapon, weaponProperty.ammo] -= 1;
The gun you have is determined by a single row number. The array is for retrieving the properties of the gun after you have that row number set.
 

TheouAegis

Member
renex added this example after you posted.
Code:
currentWeapon = weapon.machinegun;
weaponArray[currentWeapon, weaponProperty.ammo] -= 1;
The gun you have is determined by a single row number. The array is for retrieving the properties of the gun after you have that row number set.
Seems like one of your mellowest replies in a long time. I want whatever drugs you were on today. o_O


Current_gun = weapons[what the hell do i write, this is so confusing]
What you put in the first index and what you put in the second index depend on how you want to build your system.

If you're using a 2D array for all the gun properties, you'd do what renex suggested of assigning an enumerator to each possible gun and use that enumerator to define the first index. The second index would then be the properties of that gun, also defined by enumerators for your easy reference. You'd call the array by first saving in a variable in the player the enumerator for whichever gun he has, then passing that variable as the first index to the array. The second index will be the enumerator for whichever property you need to retrieve right then. In other words, if you need 3 properties, you have to read the array 3 times. If your player can have multiple weapons, like in Half-Life, you could use a 1D array to hold all the weapon enumerators to pass on to the 2D properties array as above.
 
Top