Gamer4Life Posted February 12, 2016 Share Posted February 12, 2016 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 Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted February 12, 2016 Share Posted February 12, 2016 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 Quote Link to comment Share on other sites More sharing options...
Gamer4Life Posted February 12, 2016 Author Share Posted February 12, 2016 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 Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted February 12, 2016 Share Posted February 12, 2016 Please place your code in code tags. Makes it easier to read. When you debug your game do you get in to the Use, Toggle function? Does the enable value get changed to true? Quote Link to comment Share on other sites More sharing options...
Gamer4Life Posted February 12, 2016 Author Share Posted February 12, 2016 I honestly wouldn't even know how to check during the debug running of the program if that value was getting swapped. I don't see an option to view outputs/inputs being produced in debug. Quote Link to comment Share on other sites More sharing options...
Josh Posted February 12, 2016 Share Posted February 12, 2016 This tutorial explains how to use the debugger: http://www.leadwerks.com/werkspace/page/tutorials/_/script-debugger-r34 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
mdgunn Posted February 12, 2016 Share Posted February 12, 2016 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 Quote Link to comment Share on other sites More sharing options...
Gamer4Life Posted February 14, 2016 Author Share Posted February 14, 2016 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.