Jump to content

Trouble with spawner script


Gamer4Life
 Share

Recommended Posts

I found a tutorial for a decent script to make a monster spawner ,but I do not need it to run upon game start as it was shown in the tutorial. I'm pretty bad at scripting but i tried to have the spawner disabled at start and tried to start it upon enabling but I am jacking something up in the code somewhere and I lack the experience to find out where. If you can help me correct my mistake I would be thankful:)

 

Script.Target = nil --Entity "character target"

Script.SpawnRate = 5.5 --float "spawn rate"

Script.MaxZombies = 5 --int Max zombs

 

function Script:Start()

self.enabled = false

end

 

 

function Script:Enable()--in

if self.enabled == false then

self.enable = true

self.lastSpawnTime = 0

self.counter = 0

end

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("AddOns/Zombie Character Pack/zombie1.pfb")

zombie.script:Start()

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

zombie.script:Enable()

--zombie.script.Speed = 2

zombie.script.Player = self.Target

end

end

Link to comment
Share on other sites

The spawning inside the update physics function is not checking the enabled variable. Perhaps something like this (haven't tested it in leadwerks):

 

 

Script.Target = nil --Entity "character target"
Script.SpawnRate = 5.5 --float "spawn rate"
Script.MaxZombies = 5 --int Max zombs

function Script:Start()
self.enabled = false
end

function Script:Enable()--in
if self.enabled == false then
	self.enable = true
	self.lastSpawnTime = 0
	self.counter = 0
end
end

function Script:UpdatePhysics()
if self.enabled then
	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("AddOns/Zombie Character Pack/zombie1.pfb")
		zombie.script:Start()
		zombie:SetPosition(self.entity:GetPosition())
		zombie.script:Enable()
		--zombie.script.Speed = 2
		zombie.script.Player = self.Target
	end
end
end 

Link to comment
Share on other sites

Oddly enough that is what I had the first time but when click the button to activate the spawner it does nothing. I am trying to incorporate a toggle function into it so the button use toggle output will work with the spawner but so far no effect. There are no run time errors produced so i'm trying to find the fail. Any thoughts?

Script.Target = nil --Entity "character target"

Script.SpawnRate = 5.5 --float "spawn rate"

Script.MaxZombies = 5 --int Max zombs

 

function Script:Start()

self.enabled = false

end

 

function Script:Use()

self:Toggle()

end

 

function Script:Toggle()--in

if self.enabled == true then

self.enabled = true

self.lastSpawnTime = 0

self.counter = 0

else

self.enabled = false

 

end

end

 

 

function Script:UpdatePhysics()

if self.enabled then

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("AddOns/Zombie Character Pack/zombie1.pfb")

zombie.script:Start()

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

zombie.script:Enable()

--zombie.script.Speed = 2

zombie.script.Player = self.Target

end

end

end

Link to comment
Share on other sites

The script almost works (and Aggrors almost worked apart from easy minor typo of enable rather than enabled).

 

The logic is messed up in the toggle block. Try this

 

function Script:Toggle()--in
if self.enabled == false then
  self.enabled = true
  self.lastSpawnTime = 0
  self.counter = 0
else
  self.enabled = false
end
end

 

Make sure the toggle is hooked up to a block with an use script on it and connected to the spawner in the flow graph, but it sounds like you've got that figured.

 

The debugger is super handy for putting in a breakpoint (click to the RIGHT of the numbers) and examining your code at a specific point (e.g. a logic branch).

 

Also System:Print is a simple if archaic friend too!

 

Hope it works out.

 

Michael

Link to comment
Share on other sites

Yup, Thanks a bunch for all the replies but after a while of tinkering with the logic and reviewing some similar toggles I found a way to get it to work. This works like a champ if anyone wants to use a toggle trigger to turn a spawner on.

Script.Target = nil --Entity "character target"
Script.SpawnRate = 5.5 --float "spawn rate"
Script.MaxZombies = 5 --int Max zombs
function Script:Start()
    self.enabled = false
end
function Script:Use()
self:Toggle()
end
function Script:Toggle()--in
if  self.disabled then
 self.enabled = true
 self.lastSpawnTime = 0
    self.counter = 0
 else
 self.disabled = true

end
end

function Script:UpdatePhysics()
    if self.enabled then
		    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("AddOns/Zombie Character Pack/zombie1.pfb")
				    zombie.script:Start()
				    zombie:SetPosition(self.entity:GetPosition())
				    zombie.script:Enable()
				    --zombie.script.Speed = 2
				    zombie.script.Player = self.Target
		    end
    end
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...