Jump to content

Best way to count kills?


havenphillip
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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