Modding Discussion Persistent variable in item's script

Discussion in 'Starbound Modding' started by Stardestroyer001, Aug 19, 2019.

Tags:
  1. Stardestroyer001

    Stardestroyer001 Void-Bound Voyager

    Hey all! I'm new to modding and I'm trying to create a mod that adds the ability to beam up even if underground by crafting and using an item...
    I wanted to set some limits like you have to wait some times while the items load while standing still and not taking damage and a cooldown period for successive teleportation.
    I tried using storage.timer in order to save the inner timer state when saving/loading game or changing worlds but when you throw the item and pick it up the storage variable reset and I can't find another solution to achieve the same thing.
    Is it possible to store my variable to somewhere "safe"?
     
  2. Errors4l

    Errors4l Spaceman Spiff

    Have a look at activeItem.setInstanceValue. you could also use status.setStatusProperty for player-sided data.
     
  3. Stardestroyer001

    Stardestroyer001 Void-Bound Voyager

    Thanks a lot!
    I tried both solutions and the InstanceValue one seems to work without issues. I didn't have a config file but it still worked.
    I found some reference for activeItem.setInstanceValue in the vehiclecontroller.lua script but when trying to read the parameter it uses config.GetParameter. Do I have to create a .config file?
    How to use status.setStatusProperty? What are the differences between the two?

    If I put the setInstanceValue in the die function it is guaranteed that it always stores the data or there are some exception when die() isn't called and the self variables may reset?
     
  4. Errors4l

    Errors4l Spaceman Spiff

    I haven't really worked with active items myself, so you'd have to go by what other items do. All items have a base descriptor (the item asset) with a bunch of parameters. Spawned items have parameters that overrule this base config. So you can override something like the "price" parameter with your own value. The value is unique to the item and can be stored/traded/passed around.

    As for status properties, they're serialized directly to the player file and only apply to that player. The value persist no matter what you do (warp/die/reboot game), until you overwrite it with something else. Seems like the best option for your use case.
     
  5. Stardestroyer001

    Stardestroyer001 Void-Bound Voyager

    Thank you very much! The item works but, as you said, when you have another similar item in your inventory the cooldown applies only to the first. I will try to use status properties if I manage to save and read the value that I set because my test didn't work.

    I used all the help I could find here in the forum or in working scripts and I think it went well for the first time. It's wonderful to have the possibility to ask for help.

    I was missing some full documentation on active items and their hooks because it is missing from the wiki and the Starbound docs folder. For example the list of arguments of update function or the die function of items (I tried and it seems to not be called by the engine) which is pretty useful in some cases.

    Probably the same thing that I wanted to do could be done in some other way in order to make my life easier.
    Are active items so bad to work with?
     

Share This Page