Jump to content
  • 0
havenphillip

Best way to count kills?

Question

I have a camera pick I'm using the pickInfo.entity mode to signal the counter to add a kill but it doesn't stop at 1. I tried a table but it just reiterates the table until the camera moves away from the entity. What's the best way to count just 1 kill per enemy? This is my current:

function Script:UpdateWorld()
    
    local pickInfo = PickInfo()
    self.proj = window:GetMousePosition()
    
    if self.camera:Pick(self.proj.x, self.proj.y, pickInfo, self.pickradius, true,2) then
        if pickInfo.entity:GetKeyValue("type") == "enemy" then
            if pickInfo.entity.script.mode ~= "dying" and pickInfo.entity.script.mode ~= "dead" then
                self.camPick = true
            elseif pickInfo.entity.script.mode == "dying" then --this is the problem. But what do I reference that will count just once?
                for i = 1,1,1 do
                    self.kills = self.kills + i
                    System:Print(i)
                end
            end
        end
    else
        self.camPick = false
    end
end

Share this post


Link to post

58 answers to this question

Recommended Posts

  • 0

OK figured it out. My loop in RaiseEvent() was wrong. Use this RaiseEvent() function and it works:

 

function RaiseEvent(eventName, data)
	-- if someone tried to raise an event that doesn't have an entry in our events table do nothing
	if events[eventName] == null then return end

	-- loop through all the subscriptions for this event (there may be many game entities who want to know about this event)
	for k, v in pairs(events[eventName]) do
		System:Print("Raising event "..eventName)
		local scriptFunc = v.scriptFunction
		local script = v.scriptObject
		
		-- insert the functions into the eventCoroutines table. this will be iterated over in the main game loop below and resumed into
		table.insert(eventCoroutines, {
			co = coroutine.create(scriptFunc),
			args = data,
			script = script
		})
	end
end

The difference is the table of callbacks within a function doesn't go from 1 to x like how I was iterating over it. This loop will loop over all sub tables of the event no matter what eventId is there. My bad.

Share this post


Link to post
  • 0

Ok. Yeah, that works. It does the System:Print(). But it still doesn't appear to be alerting the other crawlers next to it. It seems to have also solved that issue with getting an error when the crawler is beyond the pick range for health. I appreciate your help with this, man. I wish I could be more useful but this is all new to me.

It's probably something on my end at this point but I don't even know where to look. I noticed in the onDead event I had to write "self.kills = self.kills +1" in the enemyDied(data) function, but also you put " subId = subId + 1" in the SubscribeEvent(eventName, script, func). Does it need something like that again?

My best guess is its in the function itself? This is the function currently. Maybe it's one of these true/false things? or I changed the distance to 500 just for testing but don't know if that would do anything.

function Script:onAttacked(data)
    System:Print("Yeah it's calling this function")
   -- I don't recall if this is the correct syntax for distance checks but it gets the idea across. you'll have to research distance checks.

   if data.hurtMonsterPosition:GetDistance(self.entity:GetPosition(true),false) < 500 then

      -- if this monster is in a given range of the monster that was attacked then assign this monster the player target as well and it'll come running towards it!

    self.target = data.player

   end
end

Share this post


Link to post
  • 0

So when you're testing this stuff you can put a bunch of System:Print() commands in places to see if it goes into if statements and such. SO place a System:Print("A") inside the if data.hurtMonsterPosition blah blah to see if it goes inside that. If it does then you need to figure out how to set the player as this monsters target. I'm not 100% sure yet how to do it. I assumed it was just setting self.target but maybe not.

Share this post


Link to post
  • 0

Ok so if I do it like this I can get the others to attack, so it appears that the player is somewhere connected to this script. I just removed the "if" statement and told it to set mode to attack:

function Script:onAttacked(data)
    System:Print("Yeah it's calling this function")

    self.target = data.player
    self:SetMode("attack")
end

Here on the raise event we're calling the self.entity:GetPosition as the crawler who is being shot, right?

    RaiseEvent("onAttacked", { hurtMonsterPosition = self.entity:GetPosition(), player = distributorOfPain})


...and later in the "if" statement "data.hurtMonsterPosition" is itself from above. So we're telling it to get the distance from itself to itself?

   if data.hurtMonsterPosition:GetDistance(self.entity:GetPosition(),false) < 1000 then

 

 

 

Share this post


Link to post
  • 0

Remember this is being called on ALL the monsters. So hurtMonsterPosition is the monster that got hit, but when it fires for the other monsters self.entity is that other monster checking it's position against the one monster that got hit.

 

A good thing you can do is set these values to a variable and System:Print() the value out so you can see what it is and if it's something that you aren't expecting. 

  • Like 1

Share this post


Link to post
  • 0

Thanks for the help, man. I'll probably not do the onAttacked thing. I can't get it to work and that was just like an extra thing anyway. I'm definitely going to study this though and see if I can make it do more stuff. I just need to keep improving my understanding of the thinking behind it. Peace.

  • Like 1

Share this post


Link to post
  • 0

Anytime man. Feel free to ask anything anytime and I'll do my best to answer :)

  • Like 1

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   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.

×
×
  • Create New...