Author |
Message |
Hoovytaurus
Joined: Mon Dec 21, 2015 9:30 am Posts: 89
|
Changing a gun's frame with lua
I've been slowly getting into lua and lately I've been wondering if frames can be changed with lua. I want a gun to change appearance while charging up depending on how far the charge timer has gone, and the charging is all nice and it works but I can't figure out how to do the changing. I've tried self.frame but that's not an actual thing and I've tried SetNextFrame but that just messed it all up.
Help is appreciated.
|
Tue Jun 14, 2016 10:20 pm |
|
|
4zK
Joined: Mon Oct 11, 2010 1:15 pm Posts: 594 Location: Finlandia
|
Re: Changing a gun's frame with lua
The sprite frame of a HDFirearm is dictated by whether the gun is activated or not, and it can't be overwritten through lua AFAIK.
MOS.Frame is definitely a thing, but it seems to only work on other Movable-Object-Sprites, but not HeldDevice.
You should instead attach an Attachable or AEmitter to the weapon and have it read whatever properties it's supposed to base its' animations on, then use the attachable's frames for the desired animation.
|
Wed Jun 15, 2016 10:59 am |
|
|
Hoovytaurus
Joined: Mon Dec 21, 2015 9:30 am Posts: 89
|
Re: Changing a gun's frame with lua
4zK wrote: You should instead attach an Attachable or AEmitter to the weapon and have it read whatever properties it's supposed to base its' animations on, then use the attachable's frames for the desired animation. Will replacing "self" in the code meant to read those properties with "parent" (like, if parent.yadayada:IsPastSimMS) work?
|
Wed Jun 15, 2016 8:22 pm |
|
|
4zK
Joined: Mon Oct 11, 2010 1:15 pm Posts: 594 Location: Finlandia
|
Re: Changing a gun's frame with lua
Technically if the gun doesn't operate through lua, you don't need to apply any scripts to the gun itself. Use all timers etc. regarding the animation in the Attachable's script, and for stuff involving the weapon itself you can do something like this: Code: if self.parent == nil then mo = MovableMan:GetMOID(self.RootID); if mo and mo:IsHDFirearm then self.parent = ToHDFirearm(mo); end end After this, you can refer to self.parent in the Attachable's code whenever you need the gun's properties (activation, ammo count, etc.).
|
Wed Jun 15, 2016 10:40 pm |
|
|
Hoovytaurus
Joined: Mon Dec 21, 2015 9:30 am Posts: 89
|
Re: Changing a gun's frame with lua
4zK wrote: After this, you can refer to self.parent in the Attachable's code whenever you need the gun's properties (activation, ammo count, etc.). Can you give an example, say to check for when the gun is being activated? I don't think self.parent:IsActivated() would work (would it?)
|
Thu Jun 16, 2016 8:44 am |
|
|
4zK
Joined: Mon Oct 11, 2010 1:15 pm Posts: 594 Location: Finlandia
|
Re: Changing a gun's frame with lua
self.parent:IsActivated() works as a condition, it's whenever the holder of the gun is firing or "holding down the trigger". A better way to see whether the gun has been fired is checking the ammo every frame, so that the script can tell when there's 1 round less in the magazine. Examples of both: Code: if self.parent == nil then -- if self.parent isn't defined
mo = MovableMan:GetMOID(self.RootID); if mo and mo:IsHDFirearm then self.parent = ToHDFirearm(mo); end else -- if self.parent exists
if self.parent:IsActivated() then -- if trigger is held down print("activated"); -- placeholder console spam end
if self.parent.Magazine then -- if a magazine is attached to the gun
if self.parentAmmo then -- the parent gun's ammo count from the perspective of this Attachable -- (this will be nil at the very first frame of this script)
if self.parent.Magazine.RoundCount < self.parentAmmo then -- if there's a change in the ammo count since last frame
print("fired"); -- placeholder console spam end end
self.parentAmmo = self.parent.Magazine.RoundCount; -- set the actual ammo count at the end of each frame end end Then again, you need to know when the charge-up is done, right? In that case, I think the IsActivated() condition tells you right when the trigger is held, even if it's a charge-up. This all, of course, assuming that you're doing the charge-up through .ini code, the one that can be seen in gatling guns. If it gets tricky, there's always an option to make the charge-up lua-based. It would be good to know what kind of charge-up effect you're going for though - whether the sprite animation could be replaced with something else, such as particle effects.
|
Thu Jun 16, 2016 11:10 am |
|
|
Hoovytaurus
Joined: Mon Dec 21, 2015 9:30 am Posts: 89
|
Re: Changing a gun's frame with lua
4zK wrote: It would be good to know what kind of charge-up effect you're going for though - whether the sprite animation could be replaced with something else, such as particle effects. I'm using your very own charge-up code, actually. Particle effects are nice and all (they're what I'm using now since I can't figure out animations 100%) but I was thinking showing a little light filling up as the chargeTimer you used slowly goes up in time. That can't quite be done with particle effects, I don't think. I'm going to use this newfound information that you gave me to try and whip up some code ASAP. Thanks!
|
Thu Jun 16, 2016 8:21 pm |
|
|
4zK
Joined: Mon Oct 11, 2010 1:15 pm Posts: 594 Location: Finlandia
|
Re: Changing a gun's frame with lua
Oh right, you were the one I helped some months ago. Yeah, use that script I made as the base, if it does the job. The introduction of movable object number values in the latest build makes a lot of things easier, since it gives separate scripts the ability to "communicate" with each other (e.g. you can't read the gun's charge timer from the animated Attachable). You could make it so that whenever the charge is active, make little checks for different windows of time to change the animation frame. EDIT: I'm just gonna write the damn script for you while I'm at it. EDIT2: Add this line under the condition "if self.triggerPulled == false then" where it starts the charge in the weapon's charge script I made for you last time. Code: self:SetNumberValue("Anim", self.chargeDelay); This will give the script below the needed info. The following one is the full script you should use for the Attachable that displays the animation. Code: function Create(self) self.animation = false; -- no animation on creation self.animTimer = Timer(); -- it's a timer! self.parent = nil; end
function Update(self)
if self.parent == nil then -- if self.parent isn't defined
mo = MovableMan:GetMOFromID(self.RootID); if mo then if IsHDFirearm(mo) then -- if root ID is the gun self.parent = ToHDFirearm(mo); elseif IsAHuman(mo) then -- if root ID is the actor holding the gun if ToAHuman(mo).EquippedItem and IsHDFirearm(ToAHuman(mo).EquippedItem) then self.parent = ToHDFirearm(ToAHuman(mo).EquippedItem); end end end
else -- if self.parent exists
if self.animation == false then -- animation not started if self.parent:NumberValueExists("Anim") then -- if parent number value "Anim" has changed from 0 (signal to start animation)
self.animTimer:Reset(); -- start timer from 0, naturally self.animation = true; self.Frame = 1; -- first frame of the animation (second sprite)
-- 0 = idle frame (sprite000.bmp) -- 1 = start of the charge (sprite001.bmp) -- ... -- n = last frame of charge end
else -- animation has started
local delay = self.parent:GetNumberValue("Anim") / (self.FrameCount - 1); -- math (frames are spaced out equally in the charge window) -- FrameCount minus 1 because idle frame (0) is not included if self.animTimer:IsPastSimMS(delay) then
self.Frame = self.Frame + 1; -- next frame self.animTimer:Reset();
if self.Frame > self.FrameCount then -- the final frame has been passed (gun should be fired) self.animation = false; -- animation is done self.parent:SetNumberValue("Anim", 0); -- reset parent's number value self.Frame = 0; -- back to idle frame end end end end end I can't be certain whether this will work since I haven't tested it at all, let me know if there are errors. The script should be written in a way that you needn't add any values, since everything's automated.
Last edited by 4zK on Sun Jun 19, 2016 11:42 am, edited 4 times in total.
|
Thu Jun 16, 2016 8:41 pm |
|
|
Hoovytaurus
Joined: Mon Dec 21, 2015 9:30 am Posts: 89
|
Re: Changing a gun's frame with lua
4zK wrote: The following one is the full script you should use for the Attachable that displays the animation.
It's giving me an unexpected symbol near z error in the console. Not sure how it's SUPPOSED to look, so I can't really fix it. It looks beautiful, though, more complex than whatever I was about to come up with. EDIT: Okay, fixed all the small errors with the help of codepad, but now it's saying that GetMOID is a nil value and can't be called. Script still won't work, I'll look into it. EDIT2: Looked into it. MovableMan:GetMOID isn't a thing, apparently. Hm.
|
Fri Jun 17, 2016 8:00 am |
|
|
4zK
Joined: Mon Oct 11, 2010 1:15 pm Posts: 594 Location: Finlandia
|
Re: Changing a gun's frame with lua
There were some pretty dumb mistakes in the code, I must've been tired. GetMOFromID() is the correct function.
I edited it, try now.
|
Fri Jun 17, 2016 11:42 am |
|
|
Hoovytaurus
Joined: Mon Dec 21, 2015 9:30 am Posts: 89
|
Re: Changing a gun's frame with lua
4zK wrote: There were some pretty dumb mistakes in the code, I must've been tired. GetMOFromID() is the correct function.
I edited it, try now. After putting in some parentheses you missed, it gives no errors, but it still doesn't work. I will post the .ini code in a spoiler below.
|
Fri Jun 17, 2016 1:22 pm |
|
|
4zK
Joined: Mon Oct 11, 2010 1:15 pm Posts: 594 Location: Finlandia
|
Re: Changing a gun's frame with lua
Oh yeah, IsHDFirearm() is also a function. Did you add the line to the first script? Try changing GetsHitByMOs to 1 on the Attachable. It could also be the GetMOFromID() function not serving its' purpose. Try dropping the gun ingame and picking it up again. You can also troubleshoot by adding print() lines to see how far the script has gotten since there are no errors. Code: print("message here"); -- appears in the console, but might generate spam if it's something that happens every frame
|
Fri Jun 17, 2016 1:32 pm |
|
|
Hoovytaurus
Joined: Mon Dec 21, 2015 9:30 am Posts: 89
|
Re: Changing a gun's frame with lua
4zK wrote: Oh yeah, IsHDFirearm() is also a function.
Console spams that the code is trying to call it when it's a nil value. IsHDFirearm, that is.
|
Sun Jun 19, 2016 12:21 am |
|
|
4zK
Joined: Mon Oct 11, 2010 1:15 pm Posts: 594 Location: Finlandia
|
Re: Changing a gun's frame with lua
♥♥♥♥ bollocks, I completely forgot how that function actually works. Yeah, "mo" is supposed to be inside the brackets.
How about now.
|
Sun Jun 19, 2016 10:49 am |
|
|
Hoovytaurus
Joined: Mon Dec 21, 2015 9:30 am Posts: 89
|
Re: Changing a gun's frame with lua
4zK wrote: ♥♥♥♥ bollocks, I completely forgot how that function actually works. Yeah, "mo" is supposed to be inside the brackets.
How about now. Just about nothing happens, now. The charge functions as normal, but no errors happen and the 4 prints I put after the first "else" (the one that signifies that there is a parent) never fire off.
|
Sun Jun 19, 2016 11:28 am |
|
|
|