Actor gibbing from grenade
I have spent about 4 or 5 hours trying to figure this out now so some help would be appreciated. (My lua is terrible).
I'm trying to make a grenade that gibs into an actor, and I want the team of the actor to be the same as the team of the person who threw the grenade. Is this actually possible, and if so then could someone please give me a script for it? I have managed to get this to work for actors and craft, by making the team of the gib the same as the team of the thing that gibbed, but grenades don't normally have teams so that doesn't work.
Thanks in advance
EDIT: The closest I have got is this:
Code:
function Create(self)
for actor in MovableMan.Actors do
local avgx = actor.Pos.X - self.Pos.X;
local avgy = actor.Pos.Y - self.Pos.Y;
local dist = math.sqrt(avgx ^ 2 + avgy ^ 2);
if dist < 20 then
teamgrenade = actor.Team
end
end
end
function Destroy(self)
self.gib = CreateAHuman("<Name of the actor I want to use>")
self.gib.Pos = self.Pos
self.gib.Team = teamgrenade
MovableMan:AddActor(self.gib)
end
This is supposed to find the team of the actor initially carrying the grenade (or more accurately their dropship) and use that number for the team of the actor who appears when the grenade gibs, but it doesn't work, but I can't work out why. I know the detecting the actor bit in the first part is working, since I can get it to do other things, but it doesn't want to set a value to "teamgrenade". I think I haev made some kind of very simple mistake so could someone have a look at it please?
EDIT2: Derp!
It's missing brackes around the second instance of "teamgrenade"! That's why it wasn't working...
EDIT3: I seem to have solved this myself despite putting it up here. I'll post the complete code, since I learned a lot of what I did about modding by looking at threads like this. Hopefully someone will be able to learn something by looking at this. It is the first time I have actually written my own Lua script.
Code:
function Create(self)
--Scan all actors
for actor in MovableMan.Actors do
--Do some trigonometry to find the closest actor within 20 pixels
local avgx = actor.Pos.X - self.Pos.X;
local avgy = actor.Pos.Y - self.Pos.Y;
local dist = math.sqrt(avgx ^ 2 + avgy ^ 2);
if dist < 20 then
--Store the team of the selected actor for later use
teamgrenade = actor.Team
end
end
end
function Destroy(self)
--Tell the grenade to spawn an actor when it gibs.
self.gib = CreateAHuman("<Insert name of actor here>")
self.gib.Pos = self.Pos
--Tell the actor to be on the team that we stored earler
self.gib.Team = (teamgrenade)
--Create the actor
MovableMan:AddActor(self.gib)
--Profit!
end