Hi,
I’m a newbie of 2 days to GDev coming from a Lua & Java environment and I am loving it.
However, I’m struggling to get to grips with variables for multiple instances and wondered if someone could be so kind to put me on the right track,(i have checked the forums and tutorials but to no avail).
I have a balloon as an object which has a variable attached to it called “Total”.
In the event manager when the game starts I create 10 of these balloons in the scene.
What I struggle with is, how I change the variable “Total” for each balloon in the action section. Is the main object variable considered an array for each instance within the core coding or do I have to declare it as an array and then add each balloon’s variable to that array as it is created? - when i choose ‘balloon’ as the object is this the reference object or is it the particular instance?
Each instance of an object is its own entity. Changing the variable value of one of the instances does not change another. For want of a better example, think of each object as an instance of a class. There is no overarching object that controls the instances.
Each event has a scope for the objects it references. This scope is determined by the event’s conditions and some actions, and ancestor event’s conditions.
So say your balloon object has a named animation for each balloon colour.
Then in these events :
the variable “Total” is changed to the same random value for all Balloon objects in the last event because the conditions or actions don’t define the scope and therefore all objects are referenced.
Contrast that with this event:
where the variable “Totals” of all red balloons are assigned the same random value, and all other balloons are left unchanged.
And then in this event:
the variable “Total” is only changed for each balloon that’s just been created, because the action has changed the scope. In this case each balloon will be assigned a different random value.
I really appreciate your reply MrMen. Does the scope of the variable stay for the whole event or can it be lost once another action is made in that event. In the attached example, in the start event I am creating seven balloons at random places along the bottom of the screen, each with a random ‘Balloon Total’ variable. In that same event I create a text box for each balloon and I try to fill that text box with the referenced balloon’s ‘Balloon Total’ variable but it seems to lose scope and doesn’t find the variable.
I then try to run through each balloon instance every frame to move each text box with its parent balloon but it looks like all the text boxes link to the first balloon created.
i’m confused!
I read in the docs about linking so i tried this as seen in the attached picture which seems to have appropriately assigned a text box to each balloon but sadly not the variable; any thoughts?
I seem to have nailed it;
So for any other newbies who have the same issue; the line should read:
ToString(Balloon.Variable(BalloonTotal))
At the point it is called I am in an event scoped to the TextBox—GDevelop is looking for the variable of BalloonTotal
in the TextBox or as a scene variable. By default, that is why it is zero.
I think a variable scope tutorial would be very useful; I may make one
Variables are one of the trickier subjects for a lot of people. This has the basics.
https://wiki.gdevelop.io/gdevelop5/all-features/variables/#variables
Object and scene variables retain their values within the scene they’re created. Global are global. The newer local variable are temporary and only exist for the event they’re added to and any direct subevent. They’re handy for setup and other one-time uses.
The scope of a variable doesn’t change. A variable will retain its value until changed (within its scope)
IDK if you read or watched a tutorial but GD no longer requires the Variable() and VariableString() expressions. You can type the variable name or pick it from the drop down autocomplete like list.
Thanks Keith - I got my head around it ni the end!