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

Setting Array to Global Value

K

Kyle Conway

Guest
Well I have a problem but not really sure where to start. Basically I have a weapon system set up in an array with ds_maps in the create even of oWeapon.
Code:
//AR
weapons[2] = ds_map_create();
ds_map_add(weapons[2],"sprite",sAR);
ds_map_add(weapons[2],"recoil",25);
ds_map_add(weapons[2],"recoil_push",0);
ds_map_add(weapons[2],"damage",1);
ds_map_add(weapons[2],"projectile",oPistol_Bullet);
ds_map_add(weapons[2],"startup",0);
ds_map_add(weapons[2],"length",10);
ds_map_add(weapons[2],"cooldown",10);
ds_map_add(weapons[2],"bulletspeed",50);
ds_map_add(weapons[2],"automatic",true);
and in the bottom I have code to determine which weapon is out at a given time.

Code:
global.currentweapon = 0;
weapon = global.currentweapon;
ChangeWeapon(weapon);
ChangeWeapon is a script:
Code:
weapon = argument0;

var wp_map = weapons[weapon];
sprite = ds_map_find_value(wp_map, "sprite");
recoil = wp_map[? "recoil"];
recoil_push = wp_map[? "recoil_push"];
damage = wp_map[? "damage"];
projectile = wp_map[? "projectile"];
startup = wp_map[? "startup"];
length = wp_map[? "length"];
cooldown = wp_map[? "cooldown"];
bulletspeed = wp_map[? "bulletspeed"];
automatic = wp_map[? "automatic"];
QUESTION: Everything works fine, but when I set up a save system, I attempted to save "global.currentweapon". When I reload the game it works, (I have a string listing current weapon and draws to the screen). So the game is saving the variable and replacing it when loaded as I would hope. However my weapon is not switching to the weapon that was out. Instead it defaults to weapon[0]. It may be important to note that oWeapon is persistent, and if I click (fire a weapon) after loading before switching to another weapon, it breaks the game.

I assume the problem to be in this code:
Code:
global.currentweapon = 0;
weapon = global.currentweapon;
ChangeWeapon(weapon);
...as shown before. If i had to guess I would say the global.currentweapon = 0; is changing it to 0 after loading, or something like that. the global variable was previously in another object that stores all my global variables. however without it there, I get an error of the variable not existing before being read. Ultimately I guess I'm not really sure what the problem is, if anyone knows a solution or work around it would be greatly appreciated!
 
D

Danei

Guest
Why don't you make a new variable called saveCurrentWeapon or something, and then load that into global.currentweapon?
 
K

Kyle Conway

Guest
Why don't you make a new variable called saveCurrentWeapon or something, and then load that into global.currentweapon?
I will give that a shot, I'm not the best coder but to be honest I couldn't imagine it being any different than just using one variable. I discovered the weapon works and does not crash the game if I swap weapons first. So my current work around is code at the beginning of the loaded game that simulates the weapon swap button being pressed until it cycles back to the weapon I had out. With the transition effect I have in place, you cant even notice :D. But of course, I will keep trying when I can instead of relying on workarounds haha. Thank you for the suggestion!
 

PlayerOne

Member
Why don't create a ds_grid then when picking up a new weapon you loop the data from the weapon you picked up into that grid? It would be far better then using a ds_map.
 
D

Danei

Guest
to be honest I couldn't imagine it being any different than just using one variable.
Well the difference would be that you'd be saving and loading a variable which would be independent of global.currentweapon, so you could load it in from your saved data, and then replace global.currentweapon with it at any point, without worrying about setting it back to 0 before using it. It sounds like maybe that wasn't the problem, though.

It could be that the crash is caused by trying to fire a weapon when you have no weapon. None of those variables, recoil, damage, etc. are going to be set to anything until you pull them out of the DS, so unless your loading code does that too, that would explain why you're needing to swap before you can use a weapon. Your workaround is probably fine, but you might not need to simulate multiple swaps if you make sure all your data is set up properly in the loading code. It might help to post the text of your crash message, as well.
 
Top