Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

urgent question about Enum and running commands from enum name in other script

Discussion in 'Scripting' started by Flindt, Apr 23, 2020.

  1. Flindt

    Flindt

    Joined:
    Dec 28, 2016
    Posts:
    78
    Hi there

    I think I understand why I can not do what i try below - but i dont have any good alternative that i can think of - how would you do it?

    There will of cause be a lot more Enum content that i have to choose from.
    But - i dont need to be able to choose enum inside unity editor - so is there other ways to make the below work?

    why_not.png

    And another solution for this would be if i knew how i can get TheValue from this code
    Code (CSharp):
    1. TheValue = GlobalVariables.KaflAllSimulatedCO2(GlobalVariables.SceneDateTime);
    to not just contain only the actual double value - lets say 10 - but instead each time i asked for "TheValue" that it ran through the code

    Code (CSharp):
    1. "GlobalVariables.KaflAllSimulatedCO2(GlobalVariables.SceneDateTime);"

    Thanks a lot for all replies
     
    Last edited: Apr 23, 2020
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    could you please describe a bit more detailed what you want to achieve?
     
  3. Flindt

    Flindt

    Joined:
    Dec 28, 2016
    Posts:
    78
    If we use the example above - where i have "TheValue" - that is now a double variable - but I think I basicly need it to be a "command" variable - so that each time i ask for the "TheValue" - then it must run the command/code:
    Code (CSharp):
    1. "GlobalVariables.KaflAllSimulatedCO2(GlobalVariables.SceneDateTime);"
     
  4. Comrad_Squirrel

    Comrad_Squirrel

    Joined:
    May 28, 2016
    Posts:
    20
    It is still not really clear (to me at least).

    You define the enum TileContent, but you don't show a variable of that type e.g.
    Code (CSharp):
    1. TileConent tileContent = TileContent.KaflHouse...
    And why do the static functions have the same name as the enum values?

    If all you need is to call a function based on an enum value make a function that takes the enum as an input parameter.

    Code (CSharp):
    1. public static double myFunction (TileConent tileContent, DateTime PassSceneDateTime)
    2. {
    3. switch (tileContent)
    4. {
    5. case values.TileContent.KaflHouseActualCo2:
    6. ...
    7. return ...
    8.  
    9. case ... etc.
    10. }
    11. }
    Edit: I now see that ValueName is your enum, that you are trying to call as if it is a function? I have no idea.
     
    Last edited: Apr 23, 2020
  5. Flindt

    Flindt

    Joined:
    Dec 28, 2016
    Posts:
    78
    And that is my one of my main questions.

    But - if we ignore the enum - and forget that - basicly i wish to find out - how i can declare something - im not sure if it is function or what it is - but declare "TheValue"
    Code (CSharp):
    1. TheValue = GlobalVariables.KaflAllSimulatedCO2(GlobalVariables.SceneDateTime);
    to run that code - and not just be declared as the double... so will i need to declare as function or what do i need?
     
  6. Comrad_Squirrel

    Comrad_Squirrel

    Joined:
    May 28, 2016
    Posts:
    20
    What do you want to declare?

    To run WHAT code?

    If you write

    Code (CSharp):
    1. double TheValue = GlobalVariables.KaflAllSimulatedCO2(GlobalVariables.SceneDateTime);
    The code in GlobalVariables.KaflAllSimulatedCO2 executes.
     
  7. Flindt

    Flindt

    Joined:
    Dec 28, 2016
    Posts:
    78
    Thanks Conrad.
    It makes sense when i write:
    Code (CSharp):
    1. double TheValue = GlobalVariables.KaflAllSimulatedCO2(GlobalVariables.SceneDateTime);
    That it executes the code - and lets say it shows me result 10.

    But then after i run the above - i change the "GlobalVariables.SceneDateTime" - which would give me another result than 10 - and after that i wish to do:

    TheValue + TheValue but then have it execute all the code - and not just adding 10 + 10 - but addin the new numbers.

    In that way i could use it in a loop that changes the datetime in each loop.

    so im looking for a way to save the "code/funkction" in the "NewValue" - so that i can just do TheValue + TheValue anywhere - and it will look out for the latest scene time etc. and still give me correct results.

    :) that was bit long explanation but hope it makes sense
     
  8. Comrad_Squirrel

    Comrad_Squirrel

    Joined:
    May 28, 2016
    Posts:
    20
    If you just want to keep track of the total value:

    Code (CSharp):
    1. double totalValue += GlobalVariables.KaflAllSimulatedCO2(GlobalVariables.SceneDateTime);
    2.  
    If you want to access all the return values at any point in time, either store the SceneDateTime values in a list, or store the results in a list.

    Does that solve your problem?
     
  9. Flindt

    Flindt

    Joined:
    Dec 28, 2016
    Posts:
    78
    Im afraid it does not solve the issue.

    It almost feel like i need the opposite - sinse i need to access the values dynamicaly... each time i write TheValue - i need it to go and check for the latest value - based on the latest saved DateTime that is saved.

    The problem is that the DateTime is saved after "TheValue" is "saved" - so i need it to "run" the command behind the "newValue" - and not just give me that saved value...
     
  10. Comrad_Squirrel

    Comrad_Squirrel

    Joined:
    May 28, 2016
    Posts:
    20
    I still don't understand what you want...

    If you just need the value of
    GlobalVariables.KaflAllSimulatedCO2(GlobalVariables.SceneDateTime);
    , but for some reason can not or do not want to call the function but instead only use TheValue, you can use get?

    Code (CSharp):
    1. public double TheValue {
    2. get {return GlobalVariables.KaflAllSimulatedCO2(GlobalVariables.SceneDateTime);}}
     
  11. Flindt

    Flindt

    Joined:
    Dec 28, 2016
    Posts:
    78
    wow - thanks Conrad - this i can see is what i was looking for.

    Im not completely sure how it works - but i can see that if i do it like this then it gives me desired correct results.

    Now the only thing is that i wish to do what you do here:
    Code (CSharp):
    1. public double TheValue {
    2. get {return GlobalVariables.KaflAllSimulatedCO2(GlobalVariables.SceneDateTime);}}
    Inside an "if" - like this:
    Code (CSharp):
    1.       if (GlobalVariables.UnitsMode == "CO2")
    2.  
    3.         {
    4.  
    5.             public double TheValue
    6.             {
    7.                 get { return GlobalVariables.KaflAllSimulatedCO2(GlobalVariables.SceneDateTime); }
    8.             }
    9.  
    10.  
    11.              Debug.Log("this is the value" + TheValue);
    12.            
    13.         }
    But this is not possible. So im trying out how to do that. I basicly want to change KaflAllSimulatedCO2 - based on if sentences.
     
  12. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    Code (CSharp):
    1. public double TheValue
    2. {
    3.     get
    4.     {
    5.         switch (GlobalVariables.UnitsMode)
    6.         {
    7.             case "CO2":
    8.                 return GlobalVariables.KaflAllSimulatedCO2(GlobalVariables.SceneDateTime);
    9.  
    10.             default:
    11.                 return -1; // or any other "invalid" value
    12.         }
    13.     }
    14. }