SC2Mapster Forums

General > General Chat

What are the consequences of having more variables?

  • 14 posts
    #1 Feb 12, 2013 at 14:44 UTC - 0 likes

    And are there remedies for it? As I develop this map, I want it to be quite optimized. But I've never really needed to go as far as having 9000 variables/arrays. I do have a plan to make a game which may require thousands of variables.

    But anyway, what are the consequences of having more variables and is there anything I could do to make it less bad?

    #2 Feb 12, 2013 at 15:39 UTC - 0 likes

    It really doesnt matter as long as you stay below the limit.

    More variables in general will slightly increase loading time of the map, since the editor initializes all variables by default. However, this is just milliseconds. Besides that it might slightly increase memory usage of the map, but im not certain on this. Basically all this doesnt matter since other factors have a much MUCH higher impact on the maps performance, such as terrain, unit count, script efficiency and data.

    Last edited Feb 12, 2013 by Mille25

    http://i55.tinypic.com/wio5t0.jpg

    Starcraft Universe lead programmer

    #3 Feb 12, 2013 at 16:24 UTC - 0 likes

    The amount alone does not mean much, even if you're using a lot. Parsing through them ineffeciently might though. For example, if you have an array of a thousand elements. Storinging it in memory only takes a few miliseconds, so doing this once will only cause the game to freeze for a frame or two, or causing the loading of your map to take a few miliseconds longer if done at map-initialization.
    Now say you want to find an unit inside this array, but you dont know at which index it is at, so you have to loop through the full index to find that unit, then afterwads you want to remove that unit from the array, but you dont want to have a null-pointer in the middle of your array as that might cause an overflow later, so you have to loop through your array again to move all units one index down. That's two calls with conditions and actions and you might want to do a lot more calculations, this takes time and will cause lag if done too many places.

    The variables themselves stored in memory doesn't cost performance, adding them, removing them and doing calculations on them does cost performance though.

    Also all heavy variables like "Unit" is only a reference, not a complete copy of that unit, so each variable which a few excetions can only take up a few bytes each. A quick estimate of 10.000 variables of 48bit each equals only 58 kilobytes total.

    #4 Feb 12, 2013 at 17:48 UTC - 0 likes

    @JakeCake26: Go

    Interesting. So if I were to find the array for a unit, how do I go about doing that? Custom values? Also, custom values, since they're kind of like variables, does anyone mind shedding light on its capabilities and limitations?

    #5 Feb 12, 2013 at 18:33 UTC - 0 likes

    It always depends on the situation. Sometimes, if there's a unit you use very often, you can simply save it's index in the array in another variable, sometimes it makes sense to store the index on the unit using custom values or alike. Sometimes running through the whole array is the way to go. This is something people use years of education to master.

    I think it's good you care about performance, but with my example of a 1000 elements I made up some untrue numbers to make my point more clear. Running through a thousand elemets, and checking if it's the right one doesn't take miliseconds, but rather a few nano-seconds at top. It's good to keep performance in mind, but when you're not working with the graphics of the game, you can take it easy about performance. Unless you make some kind of memory leak or too many periodic events it's very unlikely that you run into performance problems, even with thousands of variables.

    #6 Feb 12, 2013 at 20:53 UTC - 0 likes

    you never really want to loop over an array with an if case for every loop. unless the loop is less than 10-20 elements and doesn't occur that often.
    indexing is the way to go for anything larger.

    #7 Feb 13, 2013 at 01:36 UTC - 0 likes

    Starcraft 2 initializes all global variables during the load, rather than as they're needed (like local variables). This increases your load time, but if you use too many large arrays, it will actually cause your map to crash. It's an issue I had when programming runecraft- you get an execution time error and none of your triggers will fire. But we were using extremely large variable arrays.

    Send me a PM with any questions or comments.

    Learn about HotS mods, publishing, and regions here

    #8 Feb 13, 2013 at 05:30 UTC - 0 likes

    If I want to initialize thousands of variables how many can I initialize without causing trigger errors? Is it possible to initialize a 100 variables every 0.0625 seconds? Also will initializing a lot of variables slow down the game on slower computers? My current project has a ton of variables that I will need to initialize.

    #9 Feb 13, 2013 at 05:41 UTC - 0 likes

    Variables will all initialize at the beginning whether you want them to or not. The most i've ever had trouble with was an array of the size 2050000

    #10 Feb 13, 2013 at 09:02 UTC - 0 likes

    Well I know variables in arrays for integers for example will all initialize at 0. I need to give them other values. I assume the only way to do this is through triggers after the game has started right, or am I overlooking something?

    #11 Feb 13, 2013 at 19:26 UTC - 0 likes

    @Imperfect1987: Go

    Yes and no, if you leave the main value at 0 then arrays initialize at 0. in order to give them values other than the main default value you need to assign them in the game.

    #12 Feb 14, 2013 at 04:21 UTC - 0 likes

    So I did a test today and it appears I can change over 5000 variables per second with no performance drops on my computer (and probably higher). Anyone know if a lower end computer will have trouble processing so many variables at once?

    #13 Feb 14, 2013 at 05:47 UTC - 0 likes

    They shouldn't, changing variables isn't CPU heavy, it just takes forever to program. It's the memory allocation that you have to worry about.

    #14 Feb 14, 2013 at 07:20 UTC - 0 likes
    Quote from willuwontu: Go

    he most i've ever had trouble with was an array of the size 2050000

    You cant store more than about 500k variable in your map. The editor wont even let to save a map if it has more than 520k variable. Dont know if the script takes place from that limit (it was like that, but I think 1.5 changed it)

    You can use data tables however. You can have infinite size data tables as long you dont store string or texts in it. They do load and save a bit slower.

    Last edited Feb 14, 2013 by Hookah604

    Homepage: www.sc2goa.com

  • 14 posts

You must login to post a comment. Don't have an account? Register to get one!