1. This forum is obsolete and read-only. Feel free to contact us at support.keenswh.com

Tutorial: Constructor And Save

Discussion in 'Programming Guides and Tools' started by Malware, Apr 7, 2016.

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

    Malware Master Engineer

    Messages:
    9,867
    UPDATE!!!
    Go here for much more in-detail tutorials



    The Constructor


    The constructor is an optional, special method that is called only once, either after you build your program or after you world has loaded. It's purpose is to initialize your script, most commonly load information from Storage.

    Since the name of your grid program class is Program, so is the name of the constructor:
    Code:
    int _value1;
    int _value2;
    
    public Program() {
    	if (Storage.Length > 0) {
    		var parts = Storage.Split(';');
    		_value1 = int.Parse(parts[0]);
    		_value2 = int.Parse(parts[1]);
    	}
    	Echo("Constructed");
    }
    
    The Save

    The second part of the combo is the Save method. If you implement it, it will be called by the game when required, to allow your script to save its state. It will be called when your world is saving, or when you recompile your script.

    Code:
    public void Save() {
    	Storage = _value1 + ";" + _value2;
    	Echo("Saved");
    }
    
    For completeness, let's add a Main method:
    Code:
    public void Main() {
    	_value1++;
    	_value2 = _value1 * 2;
    	Echo(_value1 + " " + _value2);
    }
    
    Experiment! That's the best way to get a handle on how this works. Or, simply ask a question and I will answer if I can.
     
    Last edited: Jul 14, 2018
    • Like Like x 5
    • Agree Agree x 1
    • Informative Informative x 1
  2. The Linking Tinker

    The Linking Tinker Trainee Engineer

    Messages:
    42
    O wow, This is super useful for the script I have been working on. thank you! guess I've got some rehashing to do this weekend :)
     
  3. draslin

    draslin Trainee Engineer

    Messages:
    16
    So what's the difference between this and simply writing your own function to accomplish the same thing? Is there some voodoo that benefits from this?
     
  4. Malware

    Malware Master Engineer

    Messages:
    9,867
    The save is called by the game on demand so you don't have to waste precious cycles serializing each tick, and the constructor is merely a convenience thing so you don't have to do "if (!_initialized)" or whatever all the time.
     
  5. Innoble

    Innoble Apprentice Engineer

    Messages:
    238
    Can something like this be used to save stuff with a mod script? I am writing mod scripts these days and I am wondering how to save/load values for that.
     
  6. Phoera

    Phoera Senior Engineer

    Messages:
    1,713
    this topic about ingame scripts.

    btw, how the hell you have initialized field in constructor?
     
  7. Malware

    Malware Master Engineer

    Messages:
    9,867
    I'm using System.Runtime.Serialization.FormatterServices.GetUninitializedObject(type), which creates an instance of an object without calling the constructor. Then I initialize the fields and call the constructor manually.

    Obviously something one needs to be careful about using, but the scripts are prime candidates.

    @Innoble no, this is designed specifically for ingame scripts - it's a part of the MyGridProgram base class.
     
  8. Phoera

    Phoera Senior Engineer

    Messages:
    1,713
    hm, a bit of cheating =D, ok
    btw, do you remember which limit have Storage? by fast looking into source i don't saw it.
     
  9. Krienas

    Krienas Trainee Engineer

    Messages:
    37
    As I didn't find artificial limits also, then "The maximum size of a String object in memory is 2GB, or about 1 billion characters." should be true.
     
  10. Malware

    Malware Master Engineer

    Messages:
    9,867
    This will change, it was supposed to be limited. It was originally intended to be 8K characters, if I remember correctly. Which I may not...
     
  11. mexmer

    mexmer Senior Engineer

    Messages:
    1,977
    i think it was 10k, doesn't matter tho'
    there was limit imposed on serializer for PB, when storing storage. so even if you gone past limit on runtime, it was never saved (i think it did truncate storage string) have not checked current storage tho', might be different.

    still having "unlimited" storage might lead to quite bad things.
    not to mention, storage was never meant to store large object, but rather small chunks of data, that will allow you to restore state of your script after reload (or server restart)
     
  12. hellokeith

    hellokeith Apprentice Engineer

    Messages:
    335
    @Malware What is the prerequisite for this to work on world load? The script has been loaded into a PB.. compiled.. run once already.. ?
     
  13. Malware

    Malware Master Engineer

    Messages:
    9,867
    Loaded and compiled once, then it will work every time after that without any further maintenance.
     
    • Like Like x 1
  14. Ronin1973

    Ronin1973 Master Engineer

    Messages:
    4,964
    @Malware

    Will constructors work for Pirate Drones and Cargo ships when they are spawned?
     
  15. Malware

    Malware Master Engineer

    Messages:
    9,867
    I cannot imagine why not. They're grids like all others, no difference.
     
  16. Ronin1973

    Ronin1973 Master Engineer

    Messages:
    4,964
    I noticed your comment was "when the game loads." I know that the game will access the prefabs during load. But didn't know if that applied to spawned ships since the game is already running when they are called into existence in the game world.

    I guess my question is if constructors are called when the game is loading OR when the grid is being placed by SE in the game world from a saved location.
     
  17. Malware

    Malware Master Engineer

    Messages:
    9,867
    Like all other constructors they're called when the program is instantiated, irrespective of how and why it is instantiated.
     
  18. DoubleCouponDay

    DoubleCouponDay Apprentice Engineer

    Messages:
    123
    Was looking for a tutorial just like this. Thanks again malware!
     
  19. Pez

    Pez Apprentice Engineer

    Messages:
    102
    Ok so does the script constructor run when the world is loaded or does it run when the programmable block is executed?

    For the save function you wrote how do you retrieve the saved information? When specifically is it called?

    public void Save() {
    Storage = _value1 + ";" + _value2;
    Echo("Saved");
    }
     
  20. Malware

    Malware Master Engineer

    Messages:
    9,867
    @Pez All your questions have already been answered in the OP :)
     
  21. Pez

    Pez Apprentice Engineer

    Messages:
    102
    I'm a silly goose about the constructor. About the save state, if I make Storage = _value1 does that mean the next time I recompile the script it'll make _value1 equal to what was saved the withe Save() method? I notice the semicolon, is it saving it in some kind of .csv or xml format? Or better question is there documentation for these, I'm a snerd for tech manuals.
     
  22. Malware

    Malware Master Engineer

    Messages:
    9,867
    This is the documentation. There are no tech manuals because the game isn't finished and things change too much. Storage is just a general string property, there's nothing magic going on except for the fact that Storage is saved. You have to do all the work yourself just as demonstrated in the original post. The parsing happens in the demonstrated constructor (by splitting the string) .
    --- Automerge ---
    @Pez Let me suggest you go to Keen's discord to ask questions. You'll get the answers you need there in the #programming-in-game channel - I'm often available there myself. Although given the times you post here, probably not when you are - but someone is likely to be able to answer any question you might have :)
     
    • Like Like x 1
  23. Pez

    Pez Apprentice Engineer

    Messages:
    102
    Thank you that helps a lot malware!
     
Thread Status:
This last post in this thread was made more than 31 days old.