Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Is it possible to make a static (global) List of structs?

Discussion in 'Scripting' started by kyle_lam, Nov 28, 2023.

  1. kyle_lam

    kyle_lam

    Joined:
    Oct 17, 2019
    Posts:
    31
    I am able to make a static List of ints,bools etc. but I need to use something like a struct to be able to lookup values associated with keys. I'm trying to use structs but I can't figure out how to make it static.

    For my game, I want a global list of Sprites as values and Keycodes as keys, so that I can show the player hotkey prompts dynamically instead of manually (for example - hotkey icons on menu buttons to tell player which gamepad button to press to interact with a button. I place an HotkeyIcon prefab down on a button, give the prefab a Keycode variable and set the desired Keycode in the inspector. At runtime, I want the prefab to look up the global hokteySprite list to see which image it should show at runtime).

    what would be a suitable data type for such a list?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,199
    Not sure what you're having trouble with. It'd just be this:

    Code (CSharp):
    1. public struct MyStruct
    2. {
    3.     public MyStruct(int keyCode)
    4.     {
    5.         KeyCode = keyCode;
    6.     }
    7.    
    8.     public int KeyCode;
    9. }
    10.  
    11. public static List<MyStruct> GlobalStructs = new List<MyStruct>();
    Though your situation seems to be describing a use-case for a Dictionary instead. Just note you can't serialise static values.
     
    CodeSmile likes this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,975
    I'm guessing OP was trying to do:
    public static struct MyStaticStruct {}

    Static structs are not a valid concept in C#.

    But for most purposes you can just use a class:
    public static class MyStaticClass {}

    Given the OP's use-case there should be no functional difference between struct and class here. But of course, once you need to add a class to a collection, it cannot be static since it needs to be instantiated.

    Otherwise I'm thinking perhaps the issue was that the static list does not appear in the Inspector? That's because of what spiney said, no serialization of statics.

    Minor correction: Unity does not serialize static-anything. Of course one could serialize anything-static but it would need to be done by custom code.
     
  4. aahent45

    aahent45

    Joined:
    Nov 28, 2023
    Posts:
    1
    Yes, it is possible to create a static (global) list of structs in certain programming languages. The ability to do so depends on the language's support for static variables and the specific rules for initializing static data.

    For example, in C or C++, you can create a global static list of structs like this:
    #include <iostream>
    #include <vector>
    -------------------------------------------------------------------------------
    // Define a struct
    struct MyStruct {
    int data;
    // Add other members as needed
    };

    // Declare a global static list of structs
    static std::vector<MyStruct> myStructList;

    int main() {
    // Now you can use myStructList globally in your program
    myStructList.push_back({1});
    myStructList.push_back({2});

    // Access and manipulate the list as needed

    return 0;
    }

    -------------------------------------------------------------------------------------------------------

    In this example, myStructList is a global static variable, and you can access and modify it from any function in the same translation unit. The static keyword here is used to indicate internal linkage, meaning that the variable is only visible within the translation unit (source file).

    Note that the specifics may vary depending on the programming language you are using. Other languages, like Java or C#, may have different mechanisms for achieving similar goals. Always refer to the documentation of the specific language you are working with for accurate and language-specific information.
     
  5. kyle_lam

    kyle_lam

    Joined:
    Oct 17, 2019
    Posts:
    31
    This seems to be the root of my issue. I want to create a List of Sprites that correlate with specified Keycodes. I need to populate the list manually since I need to choose which Sprite correlates to which Keycode (Keycode "Joystick Button 0" should show Sprite "Xbox Button A", Keycode "Joystick Button 1" should show Sprite "Xbox Button B") etc.).
    Since I need to populate the list manually, the list must be Instantiated. So I suppose that is the answer. It appears that I cannot have such a List be static. I will just have to instance the list and reference the script where necessary. It works but its not ideal.

    It makes me wonder if I am approaching this wrong altogether. Perhaps there is a better way to show gamepad hotkey icons in game...
     
    Last edited: Nov 29, 2023
  6. kyle_lam

    kyle_lam

    Joined:
    Oct 17, 2019
    Posts:
    31
    This would work if I didn't need to manually populate the list with Sprite assets. I need to choose which Sprites correspond to which KeyCodes in a project folder and so it appears that such a list must be instanced.
     
    Last edited: Nov 29, 2023
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,199
    Seems like the kind of thing where a singleton pattern is fine. A good use-case for a scriptable object singleton in fact. You can easily throw together your own implementation using pre-loaded assets: https://docs.unity3d.com/ScriptReference/PlayerSettings.GetPreloadedAssets.html

    Then you can author your data in the object while being able to access it globally.
     
  8. kyle_lam

    kyle_lam

    Joined:
    Oct 17, 2019
    Posts:
    31
    I'm not familiar with these concepts so I'll do some research on them. Thank you for the pointers