Giter Site home page Giter Site logo

sharecart.lua's Introduction

Instructions

You should call sharecart.valid with the current working directory to determine if you're in a valid sharecart location. You'll need your game to be running in a directory that's a sibling to a /dat directory that contains a proper o_o.ini file.

/dat
   o_o.ini
/GAMENAME
   GAMEEXECUTABLE

sharecart.valid will return a boolean describing if you're in a sharecart compatible directory. If it isn't, it's up to your game to decide what to do. Maybe you create a sibling directory? Maybe you bail out and let the player know they should move the game? I don't know. Your call.

If you're using this from Love2D like I would be, there's an alternate initializer that you should use.

-- you should add the following code to your existing love.load declaration
function love.load(args)
	local sharecart_data = sharecart.love_load(love, args)
	if sharecart_data == nil then
	    -- do something to handle the fact that there is no
	    -- save file where it should be
	end
end

This will run the .valid check for you, and do a couple other things to make sure that it will correctly find the sharecart save file. Otherwise if you distribute Mac .app games it won't work correctly, without some additional effort.

The Sharecart save file permits only specifc keys and restricted values for them

  • MapX (0-1023)
  • MapY (0-1023)
  • Misc0 (0-65535)
  • Misc1 (0-65535)
  • Misc2 (0-65535)
  • Misc3 (0-65535)
  • PlayerName (1023 characters max)
  • Switch0 (TRUE/FALSE)
  • Switch1 (TRUE/FALSE)
  • Switch2 (TRUE/FALSE)
  • Switch3 (TRUE/FALSE)
  • Switch4 (TRUE/FALSE)
  • Switch5 (TRUE/FALSE)
  • Switch6 (TRUE/FALSE)
  • Switch7 (TRUE/FALSE)

Writing to the sharecart save file is easy

local cart_data = sharecart.load(cwd)
cart_data.PlayerName = 'Stephen'

Doing this will immediately write that value to dat/o_o.ini. If you want to defer writing the value, for now you'll have to wrap the cart_data setters on your own.

If you set a non-sharecart key on the table, it'll blow up

cart_data.Name = 'Not a real key'
-->> lua: sharecart.lua:118: Key "Name" isn't a sharecart save key

Similarly, if you don't follow the restrictions for the key's data type, it'll also blow up.

cart_data.MapX = -5
-->> lua: sharecart.lua:122: Key "MapX" can't store the given value (-5)

Ideally you wouldn't write bad stuff to the shared file. Sometimes bugs happen. To protect yourself from writing bad data without crashing your game, you can do something like the following

function save_position(position)
	return function()
		cart_data.x = position.x
		cart_data.y = position.y
	end
end

if not pcall(save_position(position)) then
	-- handle failure case
end

I'm not psyched about that pattern, but I'd rather not let me wreck the save data.

Let me know if this is terrible. I'll make an example game shortly in its own repo.

sharecart.lua's People

Contributors

alts avatar leafo avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

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.