- Home /
Problem with button press
Hello. I have a small problem at the moment. I think that the issue has to do with the processing speed of the code. The problem is this: When "ButtonPressed" is bigger than "CurWeapon", the switch does not take place immediately and the weapon that was switched from is still active while the new weapon is too. Whenever "ButtonPressed" is bigger than "CurWeapon", the switch takes place immediately. Anybody know how I could fix this?
Here is my code which does not work:
void ChangeWeapon(int ButtonPressed, bool reset)
{
Debug.Log("Attempting to change weapon");
HUD hud = transform.GetComponentInChildren<HUD>();
bool didWeChange = false;
bool canSwitch = false;
foreach(Weapon wp in WeaponsList)
{
if(wp.WeaponKey == ButtonPressed)
{
Debug.Log("Button for " + wp.Name + " pressed");
if(wp.Unlocked == true)
{
canSwitch = true;
Debug.Log("Can switch!");
}
}
if(wp.isCurWeapon == true && reset == false)
{
Debug.Log(wp.Name + " is being deactivated...");
if(canSwitch == true)
{
wp.isCurWeapon = false;
wp.ActualWeapon.gameObject.SetActive(false);
Debug.Log("Deactivated " + wp.Name);
}
else
{
Debug.LogError("Cannot deactivate");
}
}
else
{
Debug.Log(wp.Name + " is not the weapon that is being switched from!");
}
if(wp.WeaponKey == ButtonPressed && didWeChange == false)
{
if(wp.Unlocked == true)
{
Debug.Log("Changing weapon..");
CurWeapon = ButtonPressed;
Debug.Log(CurWeapon + " new CurWeapon");
LastWeapon = WeaponPlaceHolder;
Debug.Log(LastWeapon + " new Last Weapon");
WeaponPlaceHolder = CurWeapon;
wp.ActualWeapon.gameObject.SetActive(true);
wp.isCurWeapon = true;
Debug.Log("Activated " + wp.Name);
hud.DisplayCurAmmo = wp.Ammo;
hud.DisplayCurBulletsInClip = wp.BulletsLeft;
hud.DisplayBulletsPerClip = wp.BulletsPerClip;
hud.DisplayMaxAmmo = wp.MaxAmmo;
if(wp.NeedsToReload == true)
{
NeedsToReload = true;
}
else
{
NeedsToReload = false;
}
didWeChange = true;
break;
Debug.Log("Weapon Changed");
}
}
}
}
Anyone know how I could tackle this?
Edit:
I updated the script sample and found out, that the problem is due to order of operations (check image). Notice how the "SMG is being deactivated..." debug is being called before the "Can switch!". This means that it actually checks what weapon is being deactivated for making sure if it can actually be deactivated. Anybody know how I can prevent this from happening?
Answer by Radivarig · Apr 15, 2014 at 08:44 PM
In your second script you didn't use your wp in foreach and you change your weapon at the first instance where curWeapon is not equal to ButtonPressed, maybe that's why it acts very quick, by the way you change it (WeaponsList.Count -1) times, add a break after changing because once you changed it you don't need the rest of the checking. This is definitely faster but I don't know how your Weapon class looks like so I couldn't test it:
void ChangeWeapon(int ButtonPressed)
{
if(CurWeapon == ButtonPressed)
return;
HUD hud = transform.GetComponentInChildren<HUD>();
bool didWeChange = false;
foreach(Weapon wp in WeaponsList)
{
if(didWeChange == false && CurWeapon == wp.WeaponKey )
{
if(wp.Unlocked == true)
{
Debug.Log("Changing weapon..");
CurWeapon = ButtonPressed;
LastWeapon = WeaponPlaceHolder;
WeaponPlaceHolder = CurWeapon;
wp.ActualWeapon.gameObject.SetActive(true);
wp.isCurWeapon = true;
Debug.Log("Activated " + wp.Name);
if(wp.NeedsToReload)
hud.DisplayBulletsPerClip = wp.BulletsPerClip;
hud.DisplayCurAmmo = wp.Ammo;
hud.DisplayCurBulletsInClip = wp.BulletsLeft;
didWeChange = true;
Debug.Log("Weapon Changed");
}
}
if(didWeChange && wp.WeaponKey == LastWeapon)
{
wp.isCurWeapon = false;
wp.ActualWeapon.gameObject.SetActive(false);
Debug.Log("Deactivated " + wp.Name);
break; //we set new weapon and deactivate last one, now it's safe to break
}
}
}
This didn't really work 100%. The break actually prevents both weapons activating at the same time, but I need to click twice to change to the weapon I actually wanted to activate.
Wow I can't believe I completely missed the whole second part of the script, must be that I scrolled too far, sorry about that. Now I see what was happening there, you were calling recursively for each weapon to turn on or off, the normal SetActive should do just fine as it automatically does for all nested objects, and second most important thing was turning off even tho they were already turned off which took a lot of time since you didn't break anywhere. Check my updated answer, and tell me if it works for you
Should reset equal true ins$$anonymous$$d of false in line 9.? It does seem strange, since they are all called at the same place, what does debug say about current hud display values vs. CurWeapon values? Also, you can replace lines 37.-44. with NeedsToReload = wp.NeedsToReload;
Actually the reason that you missed out on the second part is because I added it in later as I thought it might come in handy (sorry for that). Thanks for updating your answer though, I actually found out a lot of problems with the script. Check the question again, I updated it.
Have your answer for effort, since it works now and I don't give 2 flying f***ks about anything else. Be aware that you didn't really suggest adding a new function at all. Hope it makes you happy :D I wish you a good life, sir.
Answer by bigdaddy · Apr 15, 2014 at 08:58 PM
If I'm reading your code right, it looks like you want to loop over each weapon in the WeaponsList looking for the weapon that is unlocked and who's WeaponKey is the ButtonPressed.
The CurWeapon != ButtonPressed looks to be used to stop checking the WeaponKey if CurWeapon and ButtonPressed match; we can move that out of the loop and use the C# break command inside the loop to stop looping on a match
public void ChangeWeapon(int ButtonPressed)
{
if (CurWeapon != ButtonPressed)
{
foreach(Weapon wp in WeaponsList)
{
if(wp.Unlocked && wp.WeaponKey == ButtonPressed)
{
Debug.Log("Trying to change weapon...");
CurWeapon = ButtonPressed;
LastWeapon = WeaponPlaceHolder;
WeaponPlaceHolder = CurWeapon
Debug.Log("Weapon Changed");
break;
}
}
}
}
Hey man, thanks for your reply. I updated the question, you could look at the question again if you want to.
Your answer
![](https://koobas.hobune.stream/wayback/20220613142808im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Inventory armor wielding proplem,How to convert from derived to base 1 Answer
How can i store the Amount of an item i have in an inventory? 0 Answers
C# Inventory System: Modify a targeted character's stats 1 Answer
getting values from class 1 Answer
an object reference is required to access non static member problem 0 Answers