Jump to content

Making a Spawning Prefab


silvertip83
 Share

Recommended Posts

Hi all,

 

I'm new to Leadwerks and have a couple questions about writing up some scripts.

 

In all the past projects I've worked on, there have been spawn prefabs already available like in UE3 and CRYENGINE3 however, I don't see any prefabs in this engine for spawning in... well anything.

 

I would like to write a spawning script and assign it to an invisible cube that would be saved as a prefab for future use. Considering this is in lua I'm sure this can be easily done but I would like some tips from experienced devs who have been using LW3.1

 

Specifically, how would you suggest starting the script to actually make an object spawn in? if possible does anyone have an example they could share?

 

I've combed through the script files within the engine but I could not find a spawn script. Would I use the "Script.draw()" function to dictate something needs to spawn?

Link to comment
Share on other sites

I have a zombie spawner for my little demo.

 

"Invisible cubes" would be pivots in Leadwerks. It's just a point in space (entity). Create one in the editor under the Objects tab. Select Misc then pivot and then click in the view windows.

 

What you would want to do then is to take an enemy model and place it in the scene. Set it's physics so it's a controller/character and give it mass. Now you need a script that controls your enemy. You can probably look in the AI script folder and assign one of them. Then right click and select "Save as Prefab".

 

Then what I did is make a spawning script that I place on the pivot and it looks like:

 

Script.Target = nil --Entity
Script.SpawnRate = 5.5 --float
Script.MaxZombies = 5 --int

function Script:Start()
self.lastSpawnTime = 0
self.counter = 0
end


--[[
function Script:UpdateWorld()

end
]]--


function Script:UpdatePhysics()
if self.counter >= self.MaxZombies then return end

if Time:GetCurrent() > self.lastSpawnTime + (self.SpawnRate * 1000) then
self.counter = self.counter + 1
self.lastSpawnTime = Time:GetCurrent()

-- create a zombie and set location, speed, and target to the player
local zombie = Prefab:Load("Prefabs/Characters/zombie.pfb")
zombie.script:Start()
zombie:SetPosition(self.entity:GetPosition())
zombie.script:Enable()
--zombie.script.Speed = 2
zombie.script.Player = self.Target
end
end


--[[
function Script:Collision(entity, position, normal, speed)

end
]]--

--[[
function Script:Draw()

end
]]--

--[[
function Script:DrawEach(camera)

end
]]--

--[[
--This function will be called after the world is rendered, before the screen is refreshed.
--Use this to perform any 2D drawing you want the entity to display.
function Script:PostRender(context)

end
]]--

--[[
function Script:Release()

end
]]--

--[[
function Script:Cleanup()

end
]]--

 

I have my own zombie ai script though so it'll be slightly different but you can probably work it out. Basically on a timed basis I create a prefab, call it's Start() function, set it's position to the pivots position, call a function I defined (Enable) in the zombie script and set it's target to the player.

 

Something like that should get you going.

Link to comment
Share on other sites

  • 2 years later...

Ok I made the respawn pivot as a child to the player so that whenever the player is moving zombies spawn around him.

 

Thats my spawining script:

 

 

Script.spawncrawler = "" --Entity "Spawn Crawler"

 

local lasttime = 0

local crawler = 3

 

function Script:UpdateWorld()

 

if Time:GetCurrent()-15000>lasttime and crawler >0 then

lasttime=Time:GetCurrent()

local newcrawler = self.spawncrawler:Instance()

newcrawler:SetPosition(self.entity:GetPosition())

crawler = crawler -1

 

end

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...