Anyone got some code for opening a door with a key?
Printable View
Anyone got some code for opening a door with a key?
xxx is map tiles of the door itself
keyid is id number of the key
step (direction) is which ever way your going through, i.e if ur going north through the door 2 spets north, when ur coming back, 2 spets south, ect...
elseif (labelName == "door-check-in") then
if Map.IsTileWalkable(XXXXX, XXXXX, X) then
Self.Step(Direction)
wait(1500)
Self.Step(Direction)
wait(1500)
else
Self.UseItemWithGround(keyid, XXXXX, XXXXX, X)
wait(1500)
Self.Step(Direction)
wait(1500)
Self.Step(Direction)
wait(1500)
end
elseif (labelName == "door-check-out") then
if Map.IsTileWalkable(XXXXX, XXXXX, X) then
Self.Step(Direction)
wait(1500)
Self.Step(Direction)
wait(1500)
else
Self.UseItemWithGround(keyid, XXXXX, XXXXX, X)
wait(1500)
Self.Step(Direction)
wait(1500)
Self.Step(Direction)
wait(1500)
end
@Jayson and if u open the door for enter, when u use the key for exit u will close it.
lua code:
elseif (labelName == "DoorEnter") then
Self.OpenDoor(X,Y,Z) -- X,Y,Z Door Position.
wait(500,800)
Self.Step("Direction")
wait(500,800)
Self.Step("Direction")
wait(500,800)
if Self.Position().x == X then -- 2 steps from closed door
-- BTW if u go south/north or East/west change X for Y)
return
else Self.UseItemWithGround(ITEMID,X,Y,Z) -- X,Y,Z Door Position.
wait(500,800)
Self.Step("Direction")
wait(500,800)
Self.Step("Direction")
wait(500,800)
if Self.Position().x == X then -- 2 steps from closed door
-- BTW if u go south/north or East/west change X for Y)
return
else gotoLabel("BeforeDoorEnter")
end
There are better ways to do it.
Something like this might work.
lua code:
LockedID = ID of the locked door.
KeyID = ID of the key.
Walker.Stop()
wait(800,1600)
if Map.GetTopUseItem(X, Y, Z).id == LockedID then --- checks the id of the locked door
useKeyOnDoor(KeyID, X, Y, Z) --- opens if locked.
wait(400,600)
Walker.Start()
else
if not Map.IsTileWalkable(X, Y, Z) then Self.UseDoor(X, Y, Z) --- opens door if unlocked
Walker.Start()
end
Walker.Start() ---- If door is neither locked or closed it'll just go to the next node and walk through it
end
This is the function for "useKeyOnDoor" (paste it in the same script.)
lua code:
function useKeyOnDoor(keyID, x, y, z) --- from forgee's library
local Cont = Container.New(0) -- check in main backpack (index is 0)
if Cont:CountItemsOfID(keyID) > 0 then
for s = 0, Cont:ItemCount() do --find specific itemid spot
local item = Cont:GetItemData(s)
if item.id == keyID then
Cont:UseItemWithGround(s, x, y, z)
end
end
end
end
Actually, Self.UseItemWithGround doesn't work with keys. You have to use it via the container.
sorry i dont get it, how do you use the key via the container? with a locked door?
Also this seem rather overly complex, surly it would just be easier just to add the key's to useitemwithground? function along with everything else.
i cant find the old function list anyway, there used to be one on this forum with all the self. /container. /walker. /move. and more...
where has this gone as this would be very helpfull.
Hi!
I kinda new to do own scripts with xenobot and just wonder how i use this lines, Do i paste both of your scripts in the same Lua file namned useKeyOnDoor?
And then change the locked ID for the door i will open, and the ID of the key?
Is it just to run the Lua after that and use a Lable called useKeyOnDoor?
I can not get it to work so if anyone can explain it to me i would be very happy :D
You also need to put in the coordinates for the door (x, y, z) in the appropriate places but don't touch anything in the function "useKeyOnDoor"
Also, you don't need to have this in a separate script just put it in the same script you're using to hunt.
Paste the second section at the end of the file and the first under the lablename that you've chosen to open the door with.
Okey thanks for the explanation! :)
its doesnt work
Hello there! im having some trouble getting this script to work, what am I doing wrong? didnt really understand what to do just paste it into my "hunting script"?
Walker.Stop()
wait(800,1600)
if Map.GetTopUseItem(32190, 32432, 8).id == 1632 then --- checks the id of the locked door
useKeyOnDoor(2968, 32190, 32432, 8) --- opens if locked.
wait(400,600)
Walker.Start()
else
if not Map.IsTileWalkable(32190, 32432, 8) then Self.UseDoor(32190, 32432, 8) --- opens door if unlocked
Walker.Start()
end
Walker.Start() ---- If door is neither locked or closed it'll just go to the next node and walk through it
end
function useKeyOnDoor(keyID, x, y, z) --- from forgee's library
local Cont = Container.New(0) -- check in main backpack (index is 0)
if Cont:CountItemsOfID(keyID) > 0 then
for s = 0, Cont:ItemCount() do --find specific itemid spot
local item = Cont:GetItemData(s)
if item.id == keyID then
Cont:UseItemWithGround(s, x, y, z)
end
end
end
end
Impossible for me to say what you're doing wrong with so little information. You'll have to describe what's not working.
The function should be pasted in the same script but not in the same label. Just put it at the end of the script. Only the first part should trigger at a specific label.
I just get a error message when im trying to make a label in the script, (Error: '<eof>' expected near 'elseif') dont know what im doing wrong, could you help me out? haha
KeyId=2968
x=32190
y=32432
z=8
elseif (labelName == "OpenDoor") then
Walker.Stop()
wait(800,1600)
if Map.GetTopUseItem(32190, 32432, 8).id == LockedID then --- checks the id of the locked door
useKeyOnDoor(2968, 32190, 32432, 8) --- opens if locked.
wait(400,600)
Walker.Start()
else
if not Map.IsTileWalkable(32190, 32432, 8) then Self.UseDoor(32190, 32432, 8) --- opens door if unlocked
Walker.Start()
end
Walker.Start() ---- If door is neither locked or closed it'll just go to the next node and walk through it
end
function useKeyOnDoor(keyID, x, y, z) --- from forgee's library
local Cont = Container.New(0) -- check in main backpack (index is 0)
if Cont:CountItemsOfID(keyID) > 0 then
for s = 0, Cont:ItemCount() do --find specific itemid spot
local item = Cont:GetItemData(s)
if item.id == keyID then
Cont:UseItemWithGround(s, x, y, z)
end
end
end
end
this is what I pasted in my hunting script..
I'm still having trouble understanding what you're pasting where. You shouldn't even be getting that bug if you followed the instructions correctly. Can't really help you with it either since it might be in some other part of the script (eof = end of function).
I'd suggest you read some basic guides on scripting first. None of this is particularly hard if you have the basics under control.
Good guide to start with: http://forums.xenobot.net/showthread...-Script-Making
I think you are copying scripts into wrong place, I think your putting function inside the function and I think that won't work, unsure tho, but what you really are missing is an "end" somewhere...
Post your WHOLE lua here and I can try help you, or if you wish, in private...