for the user to be able to purchase the woodCuttingAxe if they have the 25 goldCoins.
Observed outcome:
I get an error that says this…
Assets/TextAdventureControl.cs(167,61): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Code that I am currently using:
void buy_0()
{
text.text = "What would you like to buy today? \n\n" +
"press A to buy woodcutting Axe for 25 Gold Coins \n\n" +
"press S to go back to shop.";
if(Input.GetKeyDown(KeyCode.A && goldCoins >= 25)) {goldCoins - 25 && woodCuttingAxe + 1;}
else
text.text = "you Don't have enough coins, come back when you do.";
}
the error says that it is on the {goldCoins - 25 && woodCuttingAxe + 1;} area.
Things I have tried:
doing some google searches,
changing the - coins and + axe to -= and +=
fiddling around with the other parts of the code, but that just messed things up more.
Thought that I had an understanding on how this worked, but I am obviously spacing something simple.
from what ive looked up and read i feel like the issue is with the goldcoins being the start of the peice of code after the if statement.
any help or direction would be appreciated, thank you for taking the time to look over my question.
goldCoins - 25 && woodCuttingAxe + 1 is boolean logic that’s going to evaluate to true or false. That’s how “&&” is used. Just like you’re using it in your if condition to see if the A key is pressed AND the player has 25 or more coins. If both are true, the condition evaluates to true.
In this case, it doesn’t really make sense and that’s not what you want to do. Do something like:
seems to still be giving me the
sets/TextAdventureControl.cs(165,118): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
error when i fixed that.
void buy_0()
{
text.text = "What would you like to buy today? \n\n" +
"press A to buy woodcutting Axe for 25 Gold Coins \n\n" +
"press S to go back to shop.";
if(Input.GetKeyDown(KeyCode.A) && goldCoins >= 25) {goldCoins - 25 && woodCuttingAxe + 1;}
else
text.text = "you Don't have enough coins, come back when you do.";
}
I will keep looking at it. thanks for giving me the start of what was wrong.
the -= adds just fine, but the += throws an error. Atleast I have a basic idea of whats going wrong though, thank you! ill post back here tonight or tomorrow if i haven’t worked out the issue by then.
okay, ill look up in the documentation on that thank you. in the past i have done the && and it has checked to see if both conditions were met. It was probably in a different situation though. thanks for giving me some direction in exactly what area i need to look up
goldCoins - 25 && woodCuttingAxe + 1 is the same as saying:
if “goldCoins - 25” is true, AND “woodCuttingAxe + 1” is true, then overall the whole thing is true. So you ultimately would end up with, if it made sense,
{true} or {false} in those { }s that you have.
But that’s not what you’re trying to do. You’re trying to subtract 25 from gold coins and add 1 to woodCuttingAxe.
Saying: goldCoins - 25 doesn’t change goldCoins at all. You have to assign it like:
thanks, i feel like i am understanding that better now. so the && is basically just making it into a bool value, and a void function or method cannot have a bool value because voids do not return anything. is that correct?
I’d like to step back to your full function posted at the beginning and discuss the basic logical flow of that function.
Because it is not an IEnumerator (that is, a coroutine), and there are no ‘yield’ statements, everything in that function is being executed, immediately, in one frame. If you’re calling buy_0 from Update, it’s being executed every frame. Input.GetKeyDown doesn’t wait for the user to press a key; it just tells you, yes or no, did that button get pressed just now.
So, in the space of one frame, you:
Set the text to the prompt to buy a thing
Check to see if the user has pressed a button AND has enough money
If EITHER of those conditions are not met, tell the user they don’t have enough money.
Do you see the logical problem? For every frame the user is not pressing ‘A’, they’re being told they don’t have enough money. And since they’re being told that in the same text field as the prompt was, they’ll never see that prompt until they’re already pressing the button.
Now, on to your if statement:
Yep, that’s exactly what it does - if you have a boolean (yes or no) on the left and a boolean on the right, it checks to see that both of those are true. But that’s not what you have. We already did the “checking to see if they have enough money” part in the first half of the if statement; we already know they do. Now you’re subtracting the money; and you need to reassign that subtracted value back to the money variable. That’s not a boolean, not a question; that’s more like a command. So, in that part of the function, you don’t use &&. Instead, use ; to separate the statements.
I think part of your problem is that you’re treating Unity like the type of code you’ll find in your beginner level programming class - you set the text, you wait for input, you set other text. It’s possible to get Unity to behave that way, but not the way you’re doing it. To get Unity to do that, you’ll have to use a coroutine - you have to make your function type IEnumerator, use ‘yield’ to tell Unity to wait for something, and you have to call it a special way.
Here’s an example of how this might look, with your function:
void Start() {
StartCoroutine(buy_0());
}
IEnumerator buy_0() {
text.text = "Buy a thing by pressing A!";
while (true) {
if (Input.GetKeyDown(KeyCode.A) ) {
// At this point, the user has just pressed A. Only now do we check for gold coins
if (goldCoins >= 25) { // CHECK to see if they have the money
goldCoin -= 25; // DEDUCT the money
woodCutter += 1;
text.text = "You bought the thing!"
}
else {
text.text= "You don't have enough gold!";
}
break; //this breaks out of this input-detecting loop
}
if (Input.GetKeyDown(KeyCode.S) ) {
text.text = "Leaving shop";
break;
}
yield return null; // in a coroutine, this means "Wait for a frame"... in this case, it means "...then check the input again"
}
//...and whatever other logic comes after that.
}
Thank you, what i am attempting to do is expand upon the class project of creating an adventure text game by adding some clicker/incremental elements to see if i understood how it worked. (the class is setting up a statemachine, and using then using that to adjust where the person has gone and what they have done.) I am continuing the class as well, but I wanted to see if the basic idea of using the statemachine could be used to expand upon and make more options and things such as purchasing items, using those items to do actions and so on. I appreciate the time you took to explain that post. I am Indeed in the begining stages of learning programming, but have been shocked as to how much code i can now get working with the basic things that I have learned through the class. I really thought that what i was attempting to do was something simple i was mixing up. I have a much better understanding of how that part of programming works now with C#. I have also been trying to spend alot longer working out my own problams via searching the internet and looking up certain things. For some reason I was just not quiet getting this concept. Thanks again! have a great day.