Giter Site home page Giter Site logo

playerprefs's Introduction

PlayerPrefs

Simple tool for Godot Engine to add persistent player preferences. It's just a JSON file with data serialized.

Get Set-up

It's pretty simple. Most projects will only require 3 things:

  • Import/Clone/Copy into your project.
  • Add the PlayerPrefs scene to your Project Settings -> AutoLoad
  • Make sure you include *.json in your export profiles.

Basic Use

Here's how I use PlayerPrefs in Starscan to set window settings:

func LoadWindowSettings():
  OS.window_fullscreen = PlayerPrefs.get_pref("window_fullscreen", true)
  if !OS.window_fullscreen:
    OS.window_size = Vector2(PlayerPrefs.get_pref("window_size", 1280).x,PlayerPrefs.get_pref("window_size", 720).y)
  

And I set it like so:

func SaveWindowSettings():
  PlayerPrefs.set_pref("window_fullscreen", OS.window_fullscreen)
  if !OS.window_fullscreen:
    PlayerPrefs.set_pref("window_size", {"x":OS.window_size.x,"y":OS.window_size.y})
 

And if if I want to do something specific if we do have settings, you can check for the existence of a setting preference:

if PlayerPrefs.has_pref("previous_difficulty_code"):
  default_difficulty = PlayerPrefs.get_pref("previous_difficulty_code", default_difficulty)
  ApplyDifficultyFromCode(default_difficulty)

Gotchas and Considerations

  • This probably doesn't serialize binary data very well.
  • If you want to use this in other "AutoLoaded" scripts, you might need to set it to load earlier (use the up arrow in the list so it is first)
  • You can change the file names, if so, you might make sure those export properly too.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.