Which is better?

0 favourites
  • 14 posts
  • Which is better? Global variables, local variables, or instance variables? Currently most of the advanced stuff I do with movement and shooting has one or two global variables. Is it better to do that, set up instance variables, or local variables? Based on everything I have read I don't see any of those options making an overall impact on the speed or efficiency of my game. I guess at that point it comes down to ease of use? What are you using or what or your thoughts on the matter?

  • It's not a matter of which is better. It's a matter of what is appropriate to use in a given context.

    I strongly advise you read the manual on Global variables, local variables, and instance variables.

  • I have. Doesn't really do much for me other than rule out local variables for most things I am doing. I Guess, at least in the context of my game, it doesn't really matter if I do it globally or as an instance variable. Given you didn't hint at there being any real performance benefit between them, I will just keep things the way they are since it currently works and that is what matters in this stage of the game. Thank you.

  • I'm not commenting on performance, but there there is the obvious difference with instance variables vs global/local, which is that each object instance has its own copy of an instance variable. So if you need a value that can be different for each instance, then an instance variable is your first choice. I'm not ruling out other possibilities for tracking per-instance data, such as an array that might store instance UIDs along with the associated data.

    For the other decision, there is a rule of thumb called locality of reference, which says that you should declare something close to where you use it. This would mean using local variables where you need a value only in a particular 'scope', e.g. within a single event and/or sub-events. This is a kind of 'data hiding', which is generally a useful thing and helps to modularise your code. For everything else, there are global variables.

  • Excellent information codah

  • beardedeagle Read this tutorial it explains lots

    http://stackoverflow.com/questions/8840 ... obal-varia

  • reading up is always good and the best source of info, but a quick summary is as follows

    local variables: within a single event structure, I often use them for intermediate calculations, etc. they can only be changed within that event and using them means that you don't affect things outside of your event. so it keeps things constrained

    global variables: very useful for information that passes between events (on the same sheet as well as between sheets and layouts). they are also useful for long-term storage of information... eg scores, progress. global variables could be used for all cases of local variables but in my humble opinion it is bad practice as you should keep the number of globals to a minimum and use them only when you need them

    instance variables: these are variables that are connected to an object and are very useful for picking out a specific object from a group, or for having one object do multiple things depending on the value of an instance variable.. or could be used as the health indicator for a baddie (that was in one of the tutorials). so instance variables do not replace globals or locals, they are different things.

    for those not comfortable with instance variables, you could hack your way through global variables but then you are really missing a powerful tool.


  • Thanks Lordshiva1948 and rho, those are great pieces of information. Gives me more to think about.

  • i like global varibles because of events passings

  • beardedeagle you welcome

  • beardedeagle Read this tutorial it explains lots

    http://stackoverflow.com/questions/8840 ... obal-varia

    I think you've posted the wrong link.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Codah you are missing the point read it it say

    What is the difference between local variables, instance variables, global variables, and class variables?

  • Lordshiva1948 you referred to a tutorial, and it's not a tutorial. Anyway the stackoverflow question talks about classes, inheritance, class variables, etc. and so doesn't really apply to C2. But if he got something out of it, that's great.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)