Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

Question regarding Variables

Discussion in 'Programming Questions and Suggestions' started by Paracelsus, May 12, 2015.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Paracelsus Trainee Engineer

    Messages:
    15
    Hi

    I have been struggling with an issues for a few hours now. Keep in minds I have about 50h of experiences in c# I am learning it and I love the learning curve challenge when you start from scratch.

    My issues is the following :

    I would like to include in my program a class that would hold all variable I will be using in my Void Main. Idea behind this is to have a table of value that will hold date that would be changed throught the program procress. In other word , I would like to have the variable declared an UINT to hold the information on the amount of a particular item. But when I declare my UINT ( public ) I get and error and I cannot recall them in the void main because it tell me they don't exist.

    Those number need to be used in different math function and It would make my life easier if I could declare them one time and use them anywhere in the programm. I am intending to make some sort of data base .

    Don't answer me with a pre made script or a link telling me you get a script that do this take it. I want to make my own and script and understand so later I can use this knowledge of C# and creat some small programm in other field to help me in the daily base in IRL.

    So please include an explanation in your answer , I am in hooked on C# , I know it will take time to master and I am willing to give it all the necessary time.

    Thank you
     
  2. Xentor Junior Engineer

    Messages:
    869
    Would be easier to see your issue if you posted a little code, but I'll give this a shot anyway.

    A class is meant to represent a type of object (Doesn't have to be an actual game object, of course). For example, you could have a class called "Car". This class has several properties, for example "Color" and "Brand". So once you have that, you can look at a car and see what color it is. Of course, you need to know WHICH car you're looking at. Doesn't make sense to ask "What color is a car?" without saying which car you're referring to, right?

    So back to C#... If you want to pull data out of a class, you have to know WHICH of that class you're referring to. That's what we call an "Instance" of that class. You're probably getting an error because you're trying to pull the variables from the class instead of the instance.

    Now if you just want to use that class to help organize things, instead of as an actual class, you can mark the class and everything inside it as "static" (No instances). Then you just refer to NameOfYourClass.NameOfTheVariable.

    On the other hand, if you don't need that kind of extra organization, and just need a variable stored outside of main, you can literally just put it right outside of the main() function. It'll keep its value as long as SE is running and you don't recompile (It will be reset if you leave the world and load it back up). Make sure to declare it as "static", so you can refer to it without an instance.
     
  3. Paracelsus Trainee Engineer

    Messages:
    15
    This is what I have wrote for the moment , no point in writing more until I can figure out the issue because if I cant do it this way I will have to review this and change everything.




    Code:
     public class Initialisation
    
    
    
    
    {
    
    IMyAssembler Production = (IMyAssembler)GridTerminalSystem.GetBlockWithName("Assembleur");
    IMyOxygenGenerator IceOxygen = (IMyOxygenGenerator)GridTerminalSystem.GetBlockWithName("OxygenIce");
    IMyOxygenFarm SunOxygen = (IMyOxygenFarm) GridTerminalSystem.GetBlockWithName("OxygenSun");
    MyCargoContainer Entrepots = (IMyCargoContainer)GridTerminalSystem.GetBlockWithName("Entrepots");
    IMyLargeGatlingTurret Defences = (IMyLargeGatlingTurret)GridTerminalSystem.GetBlockWithName("Defences");
    IMyReactor Generateur = (IMyReactor)GridTerminalSystem.GetBlockWithName("Generateur");
    
    var Croiseur =newList<IMyTerminalBlock>();
    
    GridTerminalSystem.SearchBlocksOfName("Assembleur" , Croiseur);
    GridTerminalSystem.SearchBlocksOfName("OxygenIce", Croiseur);
    GridTerminalSystem.SearchBlocksOfName("OxygenSun", Croiseur);
    GridTerminalSystem.SearchBlocksOfName("Entrepots", Croiseur);
    GridTerminalSystem.SearchBlocksOfName("Defences", Croiseur);
    GridTerminalSystem.SearchBlocksOfName("Generateur", Croiseur);
    
    uint largetube =0 ;
    uint bulletproofglass =0 ;
    uint computer =0 ;
    uint constructioncomponent =0 ;
    uint detector =0 ;
    uint display =0 ;
    uint girder =0 ;
    uint gravitycomponent =0 ;
    uint interiorplate =0 ;
    uint medicalcomponent =0 ;
    uint metalgrid =0 ;
    uint motor =0 ;
    uint powercell =0 ;
    uint radiocomponent =0 ;
    uint reactorcomponent =0 ;
    uint ironrafined =0 ;
    uint goldrafined =0 ;
    uint uraniumrafined =0 ;
    uint magnesiumrafined =0 ;
    uint nickelrafined =0 ;
    uint recyclage =0 ;
    uint siliconrefined =0 ;
    uint cobaltrefined =0 ;
    uint stonerefined =0 ;
    uint silverrefined =0;
    uint platinumrefined =0 ;
    uint ironminerals =0 ;
    uint goldminerals =0 ;
    uint uraniumminerals =0 ;
    uint magnesiumminerals =0 ;
    uint nickelminerals =0 ;
    uint siliconminerals =0 ;
    uint cobaltminerals =0 ;
    uint stoneminerals =0 ;
    uint silverminerals =0 ;
    uint platinumminerals =0 ;
    
    {
    
    }
    
    
    
    
    }
     
  4. Paracelsus Trainee Engineer

    Messages:
    15
    This might work , so I don't need a class if I understand you and I could declare all my variables before tbhe void main ? Will I be able to change there value during the programm ?
     
  5. Xentor Junior Engineer

    Messages:
    869
    Ok, some issues...

    1) I forgot to mention... Classes are intended to be self-contained... It's known as "encapsulation", one of the principles of object-oriented development. Unless you say otherwise, anything inside a class is private, and can't be accessed outside of the class. Change that behavior by adding "public" before the type (e.g. public int myVariable)

    2) Can't use 'var' when defining variables at the class level, and need a space after 'new'.... List<IMyTerminalBlock> Croiseur = new List<IMyTerminalBlock>();

    Before or after it, yes. As long as you don't declare them as "readonly" or "const" (constant), you can modify them. You do need to declare them as "static", so they work without an instance reference.


    Something to keep in mind for later, when you're ready for more advanced topics... Having a ton of variables like that isn't generally the way to go, because you'll need huge blocks of code to decide which variable to put things into or take things out of. You probably want to use a Dictionary, which is basically a lookup table. You could associate numbers with strings, for example...
    Code:
    static Dictionary<string,uint> amounts = new Dictionary<string,uint>();
    
    ...
    
    amounts["something"] = 1;
    amounts["something"] += 5;
    uint newAmount = amounts["something"];
     
  6. Paracelsus Trainee Engineer

    Messages:
    15
    Thank you :) It worked out :) .

    As for the amount of variable , if I use [] wouldn't this reduce the length of the block of code considering It could act on multiple variable contained in the array in one action ?
     
  7. Xentor Junior Engineer

    Messages:
    869
    The idea is that if you're finding inventory items by name anyway, you might as well just use that... Don't remember the exact API, but you can do something like this in your loop:
    Code:
    amounts[item.Content.SubTypename] += item.Amount;
    
    A bit more concise than:
    Code:
    if (item.Content.SubTypeName == "Display") displays += item.Amount;
    else if (item.Content.SubTypeName == "Girder") girders += item.Amount;
    ...
    
     
Thread Status:
This last post in this thread was made more than 31 days old.