Using LevelDesigner in your code
Here I will describe how to use several functions of LevelDesigner in your code in order to spawn entities, and remove them. First of all, let's explain what you need. You need to create your "Spawner" entity, which can be part of your plugin, like this:
PrisonBreak={
host="pr.is.on.brk";
ip="0.0.0.0",
profile="0", -- MUST BE STRING
channelId=-2, -- set it to something under 0
port=-1 -- just like channel Id
};
LevelDesigner requires several these field to identify "spawner", so you can later do functions like Undo(). So basically, just create simulation of player by setting these fields.
Next, create your first function to spawn entity!
LevelDesigner uses function :Spawn() for this, which takes several arguments: Name, Position, Rotation, IsLoaded, TellDebug, ForceZ, HeightOffset, Spawner, IgnoreSave.
Now let's explain what all these arguments mean, first one is I hope clear, it is clearly name of entity, just like when you do !ld 3 house:a, in this case it would be "house:a", but it can also be full path, if you read description of LD, you know, you can put there: "Objects/default.cgf".
Second parameter is position and third is rotation, if you don't know what rotation to use, simply use {1,0,0},
Parameter IsLoaded tells if it was loaded at map loading (:PrepareAll() event), please, always fill this as false, LevelDesigner uses this internally to avoid overflows!
TellDebug is clear enough, it can be set to true or false, I recommend using false, unless you want to lag your server very hard.
ForceZ tells LevelDesigner, whether to set value of Z to be what was passed in Position or whether to assign it to terrain level, just like !ld vs !ldf.
HeightOffset is offset added to .z from Position argument, you can just let it nil by default,
Spawner refers to entity who is spawning the entity, given entity MUST HAVE PROFILE ID!!!
And finally IgnoreSave can be set to true, which means entity will be ignored at saving map by !ldsave.
So let's set up our spawning function!
function PrisonBreak:Spawn(name,pos,rot,save)
LevelDesigner:Spawn(name,pos,rot or {1,0,0},false,false,true,nil,self,not save); --lets not save our entities at default when !ldsave is done
end
Now if we wanted to undo this entity, we could do this: LevelDesigner:Undo(self), or LevelDesigner:Undo(PrisonBreak) when calling externally.
Please, remember that Undo() removes only last entity!
Now if you wanted to get info about last entity spawned, you can use :Last(spawner) function which returns 4 arguments: entity, modelName, pos, direction.
What is this good for? Let's say you want to rotate your entity all of sudden on Z axis:
function PrisonBreak:RotateZ(q)
local ent,mdl,pos,dir=LevelDesigner:GetLast(self); --get the last spawned entity
q=q or 0;
local x,y,z=dir.x,dir.y,dir.z;
dir.x=x*cos(q)-y*sin(q)
dir.y=x*sin(q)+y*cos(q) --sin, cos without math. take DEGREEs, not RADIANs!!!
LevelDesigner:Undo(self); -- undo the entity
self:Spawn(mdl,pos,dir); -- spawn the entity once again with new angles
end
You can now use :RotateZ(90), yay!
And last useful function if you wanted to remotely call Saving map, use :Save() like LevelDesigner:Save().
That should be everything important for you to use LevelDesigner functions in your code I hope, if you wanted to know something more, just ask.
Let's see some real use?
Maybe you remember Race scripts I posted here long time ago, if no, check it out: http://crymp.net/forum/thread.php?id=32&lu=1403365616
Well, there is something missing! Quick way of spawning checkpoint also within virtual checkpoint flag.
So let's make it:
MargeTables(Race,{
host="r.a.c.e",
ip="0.0.0.0",
profile="-1",
port=-3,
channelId=-4
}); -- assign values to Race script first!
function Race:SpawnEnt(name,pos,rot,save)
LevelDesigner:Spawn(name,pos,rot or {1,0,0},false,false,true,nil,self,not save);
end
AddChatCommand("qcp",function(self,player,msg)
Chat:SendToTarget(nil,player,"Checkpoint %d successfuly added!",#Race.Checkpoints+1);
Race:AddCP(player:GetPos(),5); -- area of 5m
local pos,dir=Spawn:CalculatePosition(player,0); --Spawn API function, see ReadMe.html, all descriptions are there!
pos.z=player:GetPos().z;
local q=90;
local x,y,z=dir.x,dir.y,dir.z;
dir.x=x*cos(q)-y*sin(q)
dir.y=x*sin(q)+y*cos(q) -- we must rotate check point entity by 90 degrees first
Race:SpawnEnt("checkpoint",pos,dir); --and finally, spawn it!
end,nil,{AdminOnly=true});
Now you can ingame do !qcp and quickly add checkpoint also with it's entity to race, cool, nah?❤️ 0