Page 1 of 1

Tutorial: Teleporting your party using an item

Posted: Sat Sep 15, 2012 1:16 pm
by Scorcher24
This is a tutorial from Montis (RussischerZar on Steam) and me.
We tried to figure out how to teleport your party when an item is used.

1.) Open items.lua in mod_assets/scripts and paste the following:

Code: Select all


cloneObject{
   name = "rock_teleporter",   
   baseObject = "rock",
   uiName= "Weird looking rock",
   throwingWeapon = false,
   rangedWeapon = false,
   reachWeapon = false,
   stackable = true,
   consumable = true,
   
   onUseItem = function(item, champion)
		-- spawn the teleporter
		rockTeleporter = spawn("teleporter", 1, party.x, party.y, party.facing, "rockTeleporter")
		
		-- Set it invisible
		rockTeleporter:setInvisible(true)
		
		-- Let it only be triggered by the party
		rockTeleporter:setTriggeredByParty(true)
		
		-- Set the target
		rockTeleporter:setTeleportTarget(15, 11, party.facing, 1)
		
		-- activate it
		rockTeleporter:activate()
		
		-- Print a message
		hudPrint("Weeeeee")
		
		-- Consume item
		return true
   end,
}

2.) Reload your project and place the rock somewhere
3.) Place a hidden pressure plate where the party gets teleported at and a script_entity next to it.
4.) Select the script and enter the following:

Code: Select all

function stopTeleporter()
	findEntity("rockTeleporter"):deactivate()
end
5.) Add the script and the function to the connector property of the hidden pressure plate

Here is a showcase to download:
http://www27.zippyshare.com/v/85374530/file.html
Open it in the editor, pick up the rock and rightclick it in the inventory.

This is pretty straight forward and easy to do.
I personally would still like a party:teleport function because it always leaves a hidden teleporter behind, since there is afaik no destroy() function.
And it only works one time with this specific setup, since the id of the spawned teleporter must be unique.

Re: Tutorial: Teleporting your party using an item

Posted: Sat Sep 15, 2012 1:22 pm
by Montis
Now I only have to figure out how to make a wand with charges out of this :D

Re: Tutorial: Teleporting your party using an item

Posted: Sat Sep 15, 2012 1:25 pm
by Komag
wow, cool stuff! this is the sort of thing that can really expand the gameplay in new ways

Re: Tutorial: Teleporting your party using an item

Posted: Sat Sep 15, 2012 1:34 pm
by Scorcher24
I have enhanced the code a bit. New download, but it works the same.

Re: Tutorial: Teleporting your party using an item

Posted: Mon Sep 17, 2012 10:36 pm
by Montis
Ok guys, brace yourselves. After 4 days of fiddling around, I finally figured it out, so here it is:

Teleporting your party to a certain location, using an item with charges.

1) Define the item in the items.lua
SpoilerShow

Code: Select all

cloneObject{
	name = "teleport_wand",
	baseObject = "lightning_rod",
	uiName = "Staff of Teleportation",
	description = "This staff crackles with energy and can teleport\nyou to the only safe location you know:\nout of this f---ing dungeon!",
	coolDownTime = 10, -- 10 seconds cooldown
	weight = 2.5, -- make it a bit heavier, why not
	charges = 10, -- 10 charges should be okay. you can alter this if you want
	skill = "spellcraft", -- makes it so only mages can use this
	requiredLevel = 1, -- makes it so only mages can use this
	wandPower = 0, -- it will cast a lightning bolt, so make it weak
	emptyItem = "teleport_wand_empty", -- when it's empty, it gets replaced by the item below
}

cloneObject{
	name = "teleport_wand_empty",
	baseObject = "lightning_rod_empty",
	uiName = "Staff of Teleportation (Empty)",
	description = "Could be used to teleport you to safety,\nnow only serves as an awkward club.",
	weight = 2.5, -- make it a bit heavier
	skill = "maces", -- why not let people with maces hit a bit harder with it? it's a staff anyway :P
}
2) Now it gets tricky: define the teleport function in init.lua
SpoilerShow

Code: Select all

cloneObject{
	name = "party",
	baseObject = "party",
	onUseItem = function(champ,item,slot)
		-- first I'll check if the used item is my custom teleport item
		if item.name == "teleport_wand" then
			-- then I'll check if the item is equipped in hand (slot 7 or 8) since this
			-- hook also triggers when you're right clicking on an item in your inventory
			if (slot ~= 7 and slot ~= 8) then	
				hudPrint("You have to equip the staff to use it.") -- you can just delete this line if you want
				return false
			end
			
			-- I'm assuming intelligent adventurers so I'll check here if it makes sense to use the item.
			-- this is also needed because we later will have a hidden pressure plate that deactivates
			-- any teleporters we created with this script, so at least confine this to your teleport target.
			-- "safe zone" here is defined as row (y coordinate) 3 and 4 on level 1
			if (party.y == 3 or party.y == 4) and (party.level == 1) then	
				hudPrint("Why would you use this here? You're already safe!")
				return false
			else
				-- ok all checks done, we're in for a teleport when the code reaches this point!
				
				-- spawn the teleporter.
				-- note that I use a teleportCounter, we have to place that in the dungeon later
				spawn("teleporter", party.level, party.x, party.y, party.facing, "wandTeleporter_"..teleportCounter:getValue())
					-- it's nice you can define spawned things this way, saves a bit of space and a lot of hassle
					:setTeleportTarget(7, 3, 2, 1) -- edit this to your teleport target in format x,y,facing,level!
					:setInvisible(true)
					:setTriggeredByParty(true)
					:setTriggeredByItem(false)
					:setScreenFlash(true)
					:setSilent(false)
					:setChangeFacing(true)
					:activate()
				
				-- confirmation always makes the player feel good (not that they wouldn't notice teleporting...)
				hudPrint("You teleport yourself to safety!")
			end
		end
	end,
}
3) We're teleported, now we have to clean up a bit...
This part was a big mess until I figured it out. you can't instantly deactivate the teleporter with the code above, because it will just not teleport you at all.

3.1) First we need the counter mentioned in the code above. Place one counter, at best near your teleport target and name it teleportCounter. You can give it an initial value of 1. No connectors needed.

3.2) Then we need a script_entity that contains a function to deactivate the teleporter we spawned just before
SpoilerShow

Code: Select all

function teleportDeactivate()
	--checks if the teleporter with the number inside the counter exists
	if findEntity("wandTeleporter_"..teleportCounter:getValue()) then
		-- if found, the following will deactivate the teleporter
		findEntity("wandTeleporter_"..teleportCounter:getValue()):deactivate()
		-- incrementing the counter number is vitel so we don't get any duplicates (would cause crash)
		teleportCounter:increment() 
	end
end
3.3) Finally we need a pressure_plate_hiddon on your teleport target location that will only be Triggered by Party (use the checkboxes). Add a connector to the script_entity we defined above on the activate event and use the action teleportDeactivate.


Done! If you find anything not working, give me a shout. This code is still a bit confusing, even for me and I probably messed something up or forgot something. :D
Also: even if you don't use this code, I learned a lot while figuring this out, so you might derive some knowledge here as well :)

Re: Tutorial: Teleporting your party using an item

Posted: Wed Jul 15, 2015 11:45 pm
by trancelistic
I always wanted to try this usable item to teleport a party somewhere. But your script refuse to work on my dungeon:( It will crash.
( latest version ofcourse of LoG1)

I don't get it:(