Forum

> > CS2D > Scripts > inventory script,cant find how to fix bug
Forums overviewCS2D overview Scripts overviewLog in to reply

English inventory script,cant find how to fix bug

3 replies
To the start Previous 1 Next To the start

old inventory script,cant find how to fix bug

loldlold123
User Off Offline

Quote
This is whole code;

problem is sometimes it doesnt save item numbers (like its becomes like megaphone x0)there is something wrong with save&load please help me

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
--//declarations of other functions
function Array(m,v)
     local t={}
     for i=1,m do
          t[i]=v
     end
     return t
end

if not script then Script={} end;
Script[#Script+1]='Inventory';
--//declarations of other functions
function Array(m,v)
local t={}
for i=1,m do
t[i]=v
end
return t
end

--//var
Bag={}
Bag.MaxItems=100
Bag.Inv=Array(32,Array(Bag.MaxItems,1))
Bag.Sta=Array(32,Array(Bag.MaxItems,0))
Bag.PlayerMenu=Array(32,"")
Bag.Items= {
[1]={
name="-",
func=function(id) menu(id,Bag.PlayerMenu[id]) end,
cons=false
},
[2]={
name="Megaphone",
func=function(id) menu(id,Bag.PlayerMenu[id]) end,
cons=false
}
}

--//declarations of game functions
function Bag.OpenInventory(id,page)
local page=page or 1 
local pages=math.ceil(#Bag.Inv[id]/6)
for i=1,pages*6 do
if i<=Bag.MaxItems and Bag.Inv[id][i]==nil then
Bag.Inv[id][i]=1
Bag.Sta[id][i]=0
end
end
if page<1 then page=pages end
if page>pages then page=1 end
local m="Inventory Page "..page
for i=6*page-5,6*page do
local ID=Bag.Inv[id][i]
if ID~=nil then m = m ..', '..Bag.Items[ID].name..'|x'..Bag.Sta[id][i] else m=m..',' end
end
if page==pages then m=m ..',,<<- First page' else m=m..',,Next page -->' end
if page==1 then m=m ..',Last page ->>' else m=m ..',<-- Previous page' end
Bag.PlayerMenu[id]=m
menu(id,m)
end

addhook("menu","Bag.Menu")
function Bag.Menu(id,title,button)
if title:sub(1, 15)=='Inventory Page ' then
local page=tonumber(title:sub(16,#title))
if button==8 then Bag.OpenInventory(id,page+1) end
if button==9 then Bag.OpenInventory(id,page-1) end
if button<=6 then
local x=(page-1)*6+button
local ID=Bag.Inv[id][x]
local _remove=Bag.Items[ID].func(id)
if Bag.Items[ID].cons==true and _remove==nil then Bag.RemoveItem(id,ID,1) end
end
end
end
function Bag.RemoveItem(id,itemid,count)
local ID=0
for n,v in pairs(Bag.Inv[id]) do
if v==itemid then ID=n break end
end
if ID==0 then return; end
Bag.Sta[id][ID]=Bag.Sta[id][ID]-count
if Bag.Sta[id][ID]<=0 then Bag.Inv[id][ID]=1 Bag.Sta[id][ID]=0 end
for i=1,#Bag.Inv[id] do
if Bag.Inv[id][i]==1 then Bag.Inv[id][i]=Bag.Inv[id][i+1] Bag.Inv[id][i+1]=1 Bag.Sta[id][i]=Bag.Sta[id][i+1] Bag.Sta[id][i+1]=0 end
end
end

function Bag.AddItem(id,itemid,count)
if not count or count<=0 then count=1 end
if Bag.Items[itemid]==nil or id<0 or id>32 then return; end
--//check free slot
local slot=0
for i=1,#Bag.Inv[id] do
if Bag.Inv[id][i]==itemid then slot=i; break;end
end
if slot==0 then
for i=1,#Bag.Inv[id] do
if Bag.Inv[id][i]==1 then slot=i; break; end
end
end
if slot==0 then return; end
--//adding
if Bag.Inv[id][slot]==1 then
Bag.Inv[id][slot]=itemid
Bag.Sta[id][slot]=count
elseif Bag.Inv[id][slot]==itemid then
Bag.Sta[id][slot]=Bag.Sta[id][slot]+count
end
for i=1,#Bag.Inv[id] do
if Bag.Inv[id][i]==1 then Bag.Inv[id][i]=Bag.Inv[id][i+1] Bag.Inv[id][i+1]=1 Bag.Sta[id][i]=Bag.Sta[id][i+1] Bag.Sta[id][i+1]=0 end
end
end

addhook("join", "Bag.OnJoin")
function Bag.OnJoin(id)
local t,t2={},{}
if player(id,"usgn")>0 then
---------------------------------------------------------------------------------------------
local file=io.open("sys/lua/saves/"..player(id,"usgn").."_inv.txt","r")
if file~=nil then
for line in file:lines() do
table.insert(t,tonumber(line))
end
file:close()
else
t=Array(Bag.MaxItems,1)
print("Save File not found!")
end
---------------------------------------------------------------------------------------------
local file2=io.open("sys/lua/saves/"..player(id,"usgn").."_sta.txt","r")
if file2~=nil then
for line in file2:lines() do
table.insert(t2,tonumber(line))
end
file2:close()
else
t2=Array(Bag.MaxItems,0)
print("Save File not found!")
end
---------------------------------------------------------------------------------------------
else
          t=Array(Bag.MaxItems,1)
          t2=Array(Bag.MaxItems,1)
      end
Bag.Inv[id]=t
Bag.Sta[id]=t2
Bag.AddItem(id,1,5)
end

function Bag.OnLeave(id)
if player(id,"usgn")>0 then
local file=io.open("sys/lua/saves/"..player(id,"usgn").."_inv.txt","w+")
for i=1,#Bag.Inv[id] do
file:write(Bag.Inv[id][i].."\n")
end
file:close()
local file2=io.open("sys/lua/saves/"..player(id,"usgn").."_sta.txt","w+")
for i=1,#Bag.Sta[id] do
file2:write(Bag.Sta[id][i].."\n")
end
file2:close()
end
end

addhook("mapchange","Bag.OnMapchange")
function Bag.OnMapchange()
     for i=1,32 do
          Bag.OnLeave(i)
     end
end

function Bag.Check(id,itemid)
local q=0
for i=1,#Bag.Inv[id] do
if Bag.Inv[id][i]==itemid then
q=q+Bag.Sta[id][i]
end
end
return q
end
edited 2×, last 07.08.12 01:10:54 am
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview