gearbox中文件操作

由于在开发过程中,会增加游戏中的暂停功能和下次继续等操作,这就需要保存当前状态下游戏中的各种参数

以便在下次开始游戏时直接提取保存的参数接着上次的玩

这就需要我们将各种参数保存在文件中再在下次加载游戏时提取出来

于是就设计到了文件的操作,下面就是封装在file.lua中的save方法和load方法


function save(fileName,dataName)	
	local gameFile=gearboxfile.open(fileName,"write");
	local data=serialize(dataName);
	gearboxfile.append(gameFile,data);
	gearboxfile.close(gameFile);
end

function load(fileName)
	local gameFile=gearboxfile.open(fileName,"read");
	local data;
	if gameFile~=-1 then
		data=unserialize(gearboxfile.getItem(gameFile,0));
	else
		data=nil;
	end
	gearboxfile.close(gameFile);
	return data;
end


在实际开发中的用法如下:


aLabel={
	   a=1;
	   b=1;
	   }
	bLable={
	   a={};
	   b={};
	   };
	cNumber=1;
	--------上以为要保存的数据,开始放表保存-----
	saveData={
		aData=aLabel;
		bData=bLabel;
		cData=cNumber;
	};
	save(gameSave,saveData);
	--------保存完毕,下面是读取-------------
	saveData=load(gameSave);
	aLabel=saveData.aData;
	bLabel=saveData.bData;
	cNumber=saveData.cData;
	--------完毕-------------

最后,在一些其他功能,如排行榜、上次游戏等等操作中,都会涉及这些文件操作,所以需要熟练掌握。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值