Help using Globalvars

aL BeasTie

Well-known member
So I got the ammo script working again, it checks if player has pickup up the backback and changes the max ammo count.

I'm just unsure about where to declare the
Code:
setglobalvar("backpack", 0);

At the moment to test I have it in level.c ...but this would make it reset every level?

I just want to set the variable somewhere when the game first starts.

Cheers,


level.c
Code:
  int iPack = getglobalvar("backpack");
  
  if (iPack == 0){ //NO Backpack
  setglobalvar("maxbullet", 200);
  setglobalvar("maxshell",  50);
  setglobalvar("maxrocket", 50);
  setglobalvar("maxcell", 300);
  }
  else if (iPack == 1){ //player has backpack, increase max ammo
  setglobalvar("maxbullet", 400);
  setglobalvar("maxshell",  100);
  setglobalvar("maxrocket", 100);
  setglobalvar("maxcell", 600);

didhitscript (for weapon pickup)
Code:
#import "data/scripts/weapons.c"

void main()
{
  void self = getlocalvar("self");
  //void opponent = getentityproperty(self, "opponent");
  void opponent = getlocalvar("damagetaker");
  int iSnd2 = loadsample("data/sfx/dswpnup.wav");
  int iAmmo = getglobalvar("maxshell");
  playsample(iSnd2, 0, openborvariant("effectvol"), openborvariant("effectvol"), 120, 0); //itemup sfx

  setShotgun_wOwned(opponent, 1); //Means now that the player has picked-up and owns this weapon from now on.
    if ( getShotgun_wAmmo(opponent)+8 < iAmmo ) {
  setShotgun_wAmmo(opponent, getShotgun_wAmmo(opponent)+8);//Add to ammo stock
  } else { setShotgun_wAmmo(opponent,iAmmo); }
  
  setCurrentWeapon(opponent, 2);
  changeentityproperty(opponent, "weapon", 2);
  
}
 
Ah thanks dude, I actually tried something like but I incorrectly typed it,
I was looking at some of your script for examples, noticed that you used NULL in statements, I thought this was what I needed.

I think it's working now :D, I'll have to confirm with more testing.

Cheers!

I should be able to do same thing for health powerups and Beserk Mode :)
 
yeah, WD said about NULL() to me. I use it mainly in @CMD dasher or something like that, because if you set 0 in y and z, when the char does it on top of a platform, he will lose velocity on the mid air, which is strange
 
I just want to set the variable somewhere when the game first starts.

Have you considered possibility of player exiting game and load a saved game?
Global variables are persistent, they are carried even to main menu so if player loads saved game, they will carry all set global variables.

I reset global variables at main menu to prevent players from carrying weapons via saved game in Contra mod
 
Yeah we have considered this, I have command/setting already made by MatMan that clears them.

I'll add it to menu like you said thou.  I can just add it in menu.c?
 
Another problem, when the stage first starts and I draw/display the max ammo count in the hud it shows 
"{vt empty} unitialized" again instead of zero.  The problem doesn't happen after ammo pickups.

Is menu.c run when player enters main menu?
I haven't made one yet, but I have updated.c that checks when plays is in the menu screen.

Edit: If I end my game and start again it works.
 
BeasTie said:
Another problem, when the stage first starts and I draw/display the max ammo count in the hud it shows  "{vt empty} unitialized" again instead of zero.  The problem doesn't happen after ammo pickups.

You need to set starting value for max ammo even if it's zero. Use onspawnscript to set starting value.
 
Yeah that's what I tried, it's not working thou.  I'm not sure how I should write it. :/

I  just had this -
Code:
  setglobalvar("maxbullet", 200);
  setglobalvar("maxshell",  50);
  setglobalvar("maxrocket", 50);
  setglobalvar("maxcell", 300);
 
You need to add void main so it becomes this:

Code:
void main(){
  setglobalvar("maxbullet", 200);
  setglobalvar("maxshell",  50);
  setglobalvar("maxrocket", 50);
  setglobalvar("maxcell", 300);
}
 
yeah I knew that part, it's in an existing script.

Code:
#import "data/scripts/weapons.c"

void main()
{
  void self = getlocalvar("self");
  int iBullet = getglobalvar("maxbullet");
  int iShell = getglobalvar("maxshell");
  int iRocket = getglobalvar("maxrocket");
  int iCell = getglobalvar("maxcell");
  //int iArmor = getglobalvar("maxarmor");
  setglobalvar("maxbullet", 200);
  setglobalvar("maxshell",  50);
  setglobalvar("maxrocket", 50);
  setglobalvar("maxcell", 300);

 
  log("\n Spawn:");
  void weaponnumber;
  log(" :1");
  //settingsload(self);
  //log(" :2");
  //weaponnumber = getCurrentWeapon(self);
  //log(" :getCurrentWeapon(self)=" + getCurrentWeapon(self));
  //changeentityproperty(self, "weapon", weaponnumber);
  //log(" :4 getShotgun_wAmmo=" + getShotgun_wAmmo(self));

  //setChaingun_wAmmo(self, getChaingun_wAmmo(self)+50);//DOOM give player starting ammo for pistol x50
  
    
  //SET HUD - WEAPON NUMBER FOR PISTOL #1
  //settextobj(20, 376, 4, 1, 1, "2"); //Adding this line for testing purposes

  //SET HUD - AMMO COUNTS
  settextobj(1, 185, 14, 3, 1, ""+getChaingun_wAmmo(self)); //Adding this line for testing purposes only
  settextobj(0, 720, 13, 2, 1, ""+getChaingun_wAmmo(self)); //Adding this line for testing purposes
  settextobj(2, 720, 23, 2, 1, ""+getShotgun_wAmmo(self)); //Adding this line for testing purposes
  settextobj(3, 720, 33, 2, 1, ""+getRocket_Launcher_wAmmo(self)); //Adding this line for testing purposes only
  settextobj(4, 720, 43, 2, 1, ""+getPlasma_Gun_wAmmo(self)); //Adding this line for testing purposes only
    
  //SET HUD - MAX AMMO COUNTS
  settextobj(6, 760, 13, 2, 1, ""+iBullet);
  settextobj(7, 760, 23, 2, 1, ""+iShell);
  settextobj(8, 760, 33, 2, 1, ""+iRocket);
  settextobj(9, 760, 43, 2, 1, ""+iCell);


}

No sure exactly what MatMan's code does, the rest of this script is just displaying the HUD (just for testing, needs to be moved to a new menu script)
 
Back
Top Bottom