Ask about "Variable"

Hi! I’m totally new to GD and I don’t have any experience in making a game at all (so I need help…)

I was trying to make an enemy move back and forward at a position. I looked up to GD’s tutorial videos and did exactly what they did (and it worked well). But I was curious “what if I remove the “object variable”, will it work the same?”. So I tried it and the answer was no. Here’re my questions:

  1. Why the first one works and the second one doesn’t?

  2. When I add a behaviour to an object, when should I consider adding “Variable” to something to make it works?
    (I ask this because when I first come to GD, I thought “Variable” is just used for things like health bar or score and nothing else. When I make any action for a specific object, I just simply think about what behavior I’m going to add without thinking of “variable” at all. But this example makes me confused and worried: “what did I miss?”)

Not totally sure but it could be that you haven’t added a number to the y axis and both movements are the same.

this is the code I use to move enemies

Because you are applying an instant force every frame for the first enemy. Whereas for the second enemy you are only applying the instant force when it’s in collision with an arrow button. Instant forces are valid/apply for only the frame they are used.

You could add a permanent force instead of an instant one for the second enemy, that may make if work the same.


You use a variable to keep track of values that can change (i.e they can vary in value). The idea of the variable Direction is to keep track of which way the enemy is heading. And you need a Direction variable for each enemy, as they can be moving in different directions to each other.

1 Like

The event group “Enemy’s movement 2” doesn’t use the direction variable and it doesn’t make my ghost move around, but the “Enemy’s movement 1” does. (you may look closely)

What I’m trying to ask is why the “direction” variable makes the object move? Because in my mind, I just think about adding behaviour, not variable.

Hi,
I think variables, to write and read them, to use them as conditions and write them in actions, are the main thing in programming. Gdevelop with behaviors and a lot of stuff give the opportunity to have already made things that we can use directly. However variable remains very important, can be use for character’s health, but also for many things that the player doesn’t see, but that’s are needed in the logic of the program.

In your case, the second ghost moves only once, everytime he reaches the collision with an arrow. Cause nothing tells him to keep on moving after the collision is over.
But for the first ghost, the variable records that the ghost is in his “left to right” state for example, and then, as long as the variable is so, the ghost moves.

Every frame, the computer reads your whole program once, then it goes for another frame, and this a lot of time per seconds. So with the variable recording the state of the ghost, at every frame the ghost will move.

It’s just like MrMen said, I just try to say it differently.

2 Likes

I get it now. Thanks a lot.

But I’m still worry when I try to make other actions for my game besides enemy. May I ask when you decide to make an action for your object to behave as you want it to be, when will you add a variable to keep track of it and when will you know you could just add behaviour and doesn’t care about variable?

(I’m sorry, I blew my mind away of the concept “Variable” and still doesn’t get the whole meaning and use of it)

Thank you for your explanation. I’ll consider using variables a lot more after this example.

1 Like

Good question. A general answer that springs to my mind is that if you need to retain extra information to make a movement decision, use a variable.

1 Like

I would see at least 3 main ways of variable use.

You can have variable that store datas at the game opening, like datas that the program will use but are not necessarily here to be changed. For example attack of a monster, their names, speed of this car or that one, etc. All datas.

There are variables that will be modified during the game, do be displayed to player, or for the game logic. Like current health, variable that record if the player already came to this room, already killed this guy, variable that are the characters inventory, the current time, or more hidden stuff for the player but that build the game logic. Like a variable that would store the current selected object, etc.

There are also small variables that would be used as a counter in a loop, as helps in other variable calculations, etc. These variable could be named “n”, “x”, “counter”, etc. They can usually be “scene variables” and reused later. Like in a loop (while event) where we want to check which enemy is already dead, you can start with n=1, check if enemy.n is dead, then set n to n+1, and so on, the loop keeps on till n>number of enemies.

Of course it’s always the same object, a variable. But I just noticed here different situations where using them.

1 Like