1
\$\begingroup\$

I am trying to make a clicker game where one button makes the item and then one button sells the item. My problem is that I don't know how to make the one button stop selling when it reaches 0 items to sell. My code for the selling of the item as of right now looks like this in game maker:

///Sell items 
global.money+=global.PC 
global.items-=global.PC 

It works to subtract per click, but once it reaches 0, it keeps going in to negatives. I am very new to this so forgive my asking probably simple questions.

\$\endgroup\$
2
  • \$\begingroup\$ Not sure about the syntax, but check if it's above 0 before subtracting. Look for the if instruction. Basically: if the value is above 0, perform the 'transaction'. \$\endgroup\$
    – Vaillancourt
    Sep 28, 2016 at 22:59
  • \$\begingroup\$ Take a look at my C# implementation: codereview.stackexchange.com/questions/122959/… \$\endgroup\$
    – Krythic
    Sep 30, 2016 at 0:34

3 Answers 3

-1
\$\begingroup\$

I would go with most short and simple method.

//Sell items 

global.money+=global.PC * (global.items > 0)

global.items-=global.PC * (global.items > 0)
\$\endgroup\$
1
  • 3
    \$\begingroup\$ While a valid possibility, this is not something I would suggest to a beginner. \$\endgroup\$
    – LukeG
    Sep 29, 2016 at 16:45
2
\$\begingroup\$

Use a conditional IF statement.

// First, check IF there are enough items left to sell
if (global.items >= global.PC) {
    // Condition passed -- Sell items now
    global.money+=global.PC 
    global.items-=global.PC 
}

If the condition fails, the code will not execute.

\$\endgroup\$
0
\$\begingroup\$
global.items = 10
if (global.items >= 1) {
    // There are more than one item, so it passes.
    global.money+=global.PC 
    global.items-=global.PC 
} else
{
draw.text"No more items" //Im at work, so i cant get on game maker to test the draw. text function, but you get the idea here.
}

This would be my solution to your problem, i have implemented systems like this before in my games. I hope it helps.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .