Page 1 of 1
how to use clear()?
Posted: Sun Mar 10, 2013 12:01 am
by drs9999
Sorry for spamming the forum, but just another question:
How I can use the clear-method?
I tried: glob.chest.cleared() but I got an "invalid key"- error...
Anyway, maybe there is a better way to achieve what I want.
I want to check if a specific chest contains a specific item, if yes then his amount should be decreased by one.
What I tried so far:
Code: Select all
if glob.chest.getitemcount("coal") > 0 then
local coal =glob.chest.getitemcount("coal")
local wood = glob.wood.getitemcount("raw-wood")
glob.chest.cleared()
coal=coal-1
glob.field[i].insert{name="coal", count= coal}
glob.field[i].insert{name="raw-wood", count=wood}
end
And:
Code: Select all
if glob.chest.getitemcount("coal") > 0 then
glob.field[i].insert{name="coal", count= -1}
end
FIrst version gives me the "invalid key"- error and also if i delete that line and it seem like using a variable behind the "count=" wont work very well (game crashed

)
Second version isnt better. After a huge lag the invetory is completly full

Re: how to use clear()?
Posted: Sun Mar 10, 2013 12:10 am
by kovarex
The function is clear, not cleared.
The addition of -1 shouldn't be possible (it tries to add 2^32 - 1 instead

I will correct that.
Re: how to use clear()?
Posted: Sun Mar 10, 2013 12:28 am
by ficolas
drs9999 wrote:Sorry for spamming the forum, but just another question:
That is not spamming.
Also, there isnt any way of extracting from a container?? (removing an item) without clearing the whole container?
Re: how to use clear()?
Posted: Sun Mar 10, 2013 12:45 am
by kovarex
ficolas wrote:drs9999 wrote:Sorry for spamming the forum, but just another question:
That is not spamming.
Of course not.
ficolas wrote:
Also, there isnt any way of extracting from a container?? (removing an item) without clearing the whole container?
Good point, I just added this function for the next version.
Re: how to use clear()?
Posted: Sun Mar 10, 2013 8:33 am
by drs9999
Ah extracting was the word I was looking for
Anyway I also only misstyped "cleared" in the example code. I tried both clear() and cleared() => same error
Also what is about the possibility to add a variable amount of items to a chest.
For example the player have to kill 50 creepers as fast as possible. If he kills them in under 5 min then he should receive 200 iron-plates, if it takes longer than 5 min then he only should receive 100.
Re: how to use clear()?
Posted: Sun Mar 10, 2013 11:42 am
by ficolas
Code: Select all
if event.name=="onunitdied" then
if count.started==nil or false then
if count==nil then
glob.count={}
glob. count.started={}
glob. count.kills={}
end
glob.count.started=true
glob.count.kills=0
end
glob.count.kills=count.kills+1
end
if glob.count.started==true then
if count.time==nil then
glob.count.time={}
glob.count.time=0
end
glob.count.time=count.time+1
if glob.count.time==120000
if glob.count.kills>=50
game.getplayer().insert(name=iron-plate, count=200)
else
game.getplayer().insert(name=iron-plate, count=100)
end
end
end
Re: how to use clear()?
Posted: Sun Mar 10, 2013 11:43 am
by ficolas
Sorry for the shitty code, I wrote it with my mobile phone.
Re: how to use clear()?
Posted: Sun Mar 10, 2013 2:32 pm
by drs9999
Thanks for the code, but that is not what i am looking for. But i noticed that I choose a bad example maybe.
That example point it out better:
Player have to kill as many creepers as possible in 5 min. after the 5 min he should receive 1 iron-plate for 1 creeper-kill
Re: how to use clear()?
Posted: Sun Mar 10, 2013 2:50 pm
by ficolas
Code: Select all
if event.name=="onunitdied" then
if count.started==nil or false then
if count==nil then
glob.count={}
glob. count.started={}
glob. count.kills={}
end
glob.count.started=true
glob.count.kills=0
end
glob.count.kills=count.kills+1
end
if glob.count.started==true and event.name=ontick then
if glob.count.time==nil then
glob.count.time={}
glob.count.time=0
end
glob.count.time=glob.count.time+1
end
if glob.count.time==6000 then
game.getplayer().insert(name=iron-plate, count=glob.count.kills)
glob.count.time=0
glob.count.kills=0
glob.count.started=false
end