GameMaker Global Variable not working??

conman06

Member
Ok, so I'm making a game in which you click the trees, then they give you wood. I need to make an object to show how much wood the player has.
In the tree object, I have these scripts for the Global Variable "Wood":
GML:
global.Wood = 0;

//When Tree clicked, add +1 wood and disappear.
if (mouse_check_button_pressed(mb_left))
{
    global.Wood = global.Wood + 1
    visible = false;
}
In the WoodIcon object, the scripts are:
GML:
if global.Wood > 0
{
    sprite_index = sWood1;
}
But, when I play it, the Wood Icon doesn't change the sprite. That makes me think if there is an error with the Global Variable "Wood"
here is a video of the problem: https://gyazo.com/1f2c2d96bfb091213547bd70073ce3b4
as you can see, when I click the trees, the Wood Icon isn't changing to equal 1.
 

conman06

Member
You are setting global.Wood to 0 every step. It will never not be 0. Set global.Wood = 0 in the Create Event.
It was already in the Create Event. My bad if I didn't make it clear.
script "global.Wood" was already in the Create Event, and the "if" Statement for the Tree is in the Left Down Event.
 
Last edited:

Yal

🐧 *penguin noises*
GMC Elder
Step = "all the time", so remove it from the step event and only set it in Create.
 

conman06

Member
Step = "all the time", so remove it from the step event and only set it in Create.
so, is it ok that the "if tree clicked" event is in a Left down event? When I put that script into create, it doesn't work.
BTW I meant Create Event, not Create Step. My brain just isn't working today.
 

conman06

Member
So... which one, Create or Step? ;P (Kidding, the image shows where it is... just make sure you don't also have it in Step.)

Where and how are you drawing the displayed wood counter?
The wood amount is an object that I put on the "Village" room. It displays how much wood by changing sub Images:
 

FrostyCat

Redemption Seeker
so, is it ok that the "if tree clicked" event is in a Left down event? When I put that script into create, it doesn't work.
BTW I meant Create Event, not Create Step. My brain just isn't working today.
Have any of the tutorials or guides you used taught you what each of the 6 basic events do, and how they are NOT interchangeable?

Code in a Create event runs only once when an instance of the object is created. If you put a keyboard or mouse check here, it will only check at creation time, not while the instance is alive. If you put a condition here, again it will only check at creation time, not while the instance is alive.

Code in a Step event recurs every frame. This is where both your mouse button check and your global.Wood check belong, because they are ongoing conditions.

Redo all the tutorials and guides you have used thus far, and this time pay special attention to their choice of events. This is too important a skill to neglect, and it's clearly being neglected. You are wasting code and time whenever you put stuff in a clearly inappropriate event, then need to be bailed out later.

Start paying attention to where you put your code, not just what your code is.
 

conman06

Member
Have any of the tutorials or guides you used taught you what each of the 6 basic events do, and how they are NOT interchangeable?

Code in a Create event runs only once when an instance of the object is created. If you put a keyboard or mouse check here, it will only check at creation time, not while the instance is alive. If you put a condition here, again it will only check at creation time, not while the instance is alive.

Code in a Step event recurs every frame. This is where both your mouse button check and your global.Wood check belong, because they are ongoing conditions.

Redo all the tutorials and guides you have used thus far, and this time pay special attention to their choice of events. This is too important a skill to neglect, and it's clearly being neglected. You are wasting code and time whenever you put stuff in a clearly inappropriate event, then need to be bailed out later.

Start paying attention to where you put your code, not just what your code is.
Thank you for the feedback! I will have to look at the tutorials again. (also congrats on 3rd place)
 

Yal

🐧 *penguin noises*
GMC Elder
so, is it ok that the "if tree clicked" event is in a Left down event?
It's "OK" as long as your game compiles when it's there (instead of GM giving you error messages), but just because something is a valid operation doesn't mean it does what you want it to do. This is a common misconception in budding programmers.

Always remember Astley's Three Maxims:
  1. There's more than one way to solve any problem.
  2. A big problem is solved by solving many small problems.
  3. Solving a problem always creates a new problem, but you can adjust the new problem's size by adjusting your solution.
Or in summary, the best solutions are when you break down the thing you want to do into small steps, and make sure to implement each step in a way that has no harmful side effects. You can only do that if you know how every step works, so make sure to read documentation and pick whatever solution sounds the more suitable for your problem based off of that.

TL;DR: RTFM
 
Top