0 Favourites

using local variables?

  • if i change global variable into local, i can't find where i can change it, i went to system > under global en local, but it isn't in the dropdownmenu? did it miss something?

  • If you put it in a sub-event and not in the first 'branch' it should automatically turn into a local variable for that 'branch'.

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • yeah but that i know, but where can you use it or change it, or is it suppose to be only a static number

    if you do a set var... where can i find that local number

  • A local variable is only reachable in the event you set it. (that's the point of local variable).

    For example, if you have a local variable in a group, you can only reach it in the events and actions of that group.

    That's why it is called "local" in opposition to "global" variable that you can access from anywhere in your event sheet.

  • i realise that, i put it inline like a subevent under a condition, then i expect that i can change it somewhere in that condition group, have you been able to use it?

  • ahh thx for the example, i needed to put everything in a subevent and then place the local above the subevent, i was placing it on the same level as my condition

    thankyou

  • I have a question about local variables:

    Can anyone give some practical examples, how to use local vars effectively?

    Why is it not possible to assign a default value to some parameter like Sprite.X?

    For most of the cases I have to use global variables, because local vars change they value every tick to the default. May be I don't get something.

  • an anyone give some practical examples, how to use local vars effectively?

    They are handy for temporarily storing values from one event to another. The key word being "temporary".

    hy is it not possible to assign a default value to some parameter like Sprite.X?he values of the first instance of a sprite is where the initial values come from.

  • Kyatric - So I checked out the example you posted (many moons ago).

    I have a group set up to control the horizontal movement of my character. I then create a local variable (we'll call it GroupVar). GroupVar is set up to control the speed of the character --> Set X to PlayerBox.X+GroupVar*dt (PlayerBox being the invisible platform object).

    All the Events following GroupVar's declaration inside that group remain on the same level as GroupVar.

    At the push of a button, I add or subject a value from GroupVar, ultimately adjusting the speed of the character as it moves.

    That's the idea, anyway. This works when I set GroupVar as a global variable. But (using an on-screen text test) I noticed that when I declare GroupVar locally at an initial value of 120, no matter how many times I push the proper button, it remains at 120. Again, as a global variable, it worked perfectly, and I saw the text display the proper value and the character moved accordingly (even to the point of moonwalking at negative values! lol)

    What I don't quite understand (though I believe I have an inkling as to the reason) is why a global variable seems to not be reset every tick while a local variable is. IS THIS because every tick the local variable is essentially re-declared within the group/event it's associated with, whereas the global variable remains open to any and all events/actions that may call on it?

    As always, thank you!

  • Rhindon:

    IS THIS because every tick the local variable is essentially re-declared within the group/event it's associated with, whereas the global variable remains open to any and all events/actions that may call on it?

    Yes.

    Global variables are even global to the project, and their value is "fixed" over time.

    Local variable has a smaller scope (a group for example) and is reset every tick.

    Since this post though features were added to variables: static and constant.

    A static local variable won't be reset and will hold a fixed value over time, but its scope remains unchanged.

    A constant is a variable which value will never change during the execution of the game.

  • Kyatric - AH! Okay, so if I want to isolate the GroupVar to its group, I just need to set the variable to static so it doesn't get reset every time?

  • Rhindon: Well the static will prevent it resets every tick.

    Otherwise, making a local variable in the group "isolates"/sets the scope to only this group.

  • Kyatric - I think I got it. Thanks. I tested it and everything seems to be working.

  • How can i reset local variables?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
163 20,229
iBarin's avatar
iBarin
Unread hot topic
76 9,273
Pandadoor's avatar
Pandadoor
Unread hot topic
0 Favourites
[plugin] 3D using Copperlicht
72 25,982
Reken's avatar
Reken