Data Realms Fan Forums
http://45.55.195.193/

More Lua Trouble
http://45.55.195.193/viewtopic.php?f=73&t=17368
Page 1 of 1

Author:  Areku [ Wed Dec 16, 2009 3:17 pm ]
Post subject:  More Lua Trouble

I decided to mess around with some Sparkle Magic, and came up with this script. It is designed to animate a nearby gun until a certain frame, and then create some speeding particles at the said gun's position. Took me a while to iron the bugs out (believe me, there was a lot of them), and yet I can't make it work. It gives out no error, but, if I fire the gun, the game freezes instantly. Can anyone explain why? (Sorry for the lousy tabbing.)

Code:
function Create(self)

   print ("IMMA FIRIN' MAH LAZOR!!!");
   self.timer = Timer();
   self.HasMOGun = 0;
   self.GunTOShoot = nil;
   self.GunHolder = nil;
   

 
   
end

function Update(self)


   if self.HasMOGun == 0 then
   for mo in MovableMan.Particles do
      for i = 1, MovableMan:GetMOIDCount() - 1 do
         mo = MovableMan:GetMOFromID(i)
         if mo.PresetName == "Vocal-0-ID" then
            local avgx = mo.Pos.X - self.Pos.X;
            local avgy = mo.Pos.Y - self.Pos.Y;
            local maindist = math.sqrt(avgx ^ 2 + avgy ^ 2);
            if maindist < 100 then
               if mo.ID ~= mo.RootID then
                  self.GunToShoot = ToAttachable(mo);
                  for modude in MovableMan.Actors do
                     modude = MovableMan:GetMOFromID(self.GunToShoot.RootID);
                     self.GunHolder = ToActor(modude);
                  end
               else
                  self.ToDelete = true;
               end
               
            end
         end
      end
   end
   self.HasMOGun = 1;
   end



   while self.GunToShoot.Frame < 38 do
      if self.timer:IsPastSimMS(370) then
         self.GunToShoot.Frame = self.GunToShoot.Frame + 1;   
         self.timer:Reset()
   else
      self.Boom = CreateAEmitter("Sonic Burst Heavy");
      self.Marker = CreateMOPixel("Sonic Burst Pixel Normal Heavy");
      local theta = ToActor(self.GunHolder):GetAimAngle(true);
      self.Marker.Pos = self.GunHolder.Pos + Vector(math.cos(theta)*100,math.sin(theta)*-100);
      self.Boom.Pos = self.GunHolder.Pos + Vector(math.cos(theta)*50,math.sin(theta)*-50);
      self.Boom.Vel = (self.Boom.Pos - self.Marker.Pos) * 0.1;
      MovableMan:AddParticle(self.Boom);
      self.ToDelete = true;
   end
end
   
      

end

Author:  Darlos9D [ Wed Dec 16, 2009 3:53 pm ]
Post subject:  Re: More Lua Trouble

So, what's running this script?

Author:  Areku [ Wed Dec 16, 2009 5:09 pm ]
Post subject:  Re: More Lua Trouble

A particle, created when the weapon shoots. I may be illiterate in Lua, but not so much as to try to add a script to an attachable.

Author:  Abdul Alhazred [ Wed Dec 16, 2009 5:45 pm ]
Post subject:  Re: More Lua Trouble

Only had a quick look at it but that while-loop perhaps? The timer must pass 370ms 38 times for it exit so it looks to me like it will run indefinitely.

Some other things: Why are you iterating through all particles? If the script is attached to a particle you already know the position. I would recommend using Sunshines(?) method of finding an actor; spawn the bullet inside the gun and use SceneMan:GetMOIDPixel in the Create function.

Author:  Darlos9D [ Wed Dec 16, 2009 7:39 pm ]
Post subject:  Re: More Lua Trouble

My method: just fire a particle with a velocity of 1. Its Create script uses either its velocity (if it's an MOPixel or MOSParticle) or its RotAngle (if it's a MOSRotating or anything based off of it) to cast a ray backwards, which should find the weapon.

Author:  Areku [ Wed Dec 16, 2009 8:00 pm ]
Post subject:  Re: More Lua Trouble

Abdul: Dang. Hadn't realised that. Will try to fix it now.

Darlos: That's actually a very good idea. I'll implement it if my script starts to cause too much lag.

Author:  Areku [ Thu Dec 17, 2009 12:33 am ]
Post subject:  Re: More Lua Trouble

More stuff happened. (DOUBLE POST I KNOW SORRY DON'T KILL ME)

After fixing pretty much everything that needed to be fixed, I ended up with this:
Code:
function Create(self)

   print ("Gun sequence initiated.");
   self.timer = Timer();
   self.HasMOGun = 0;
   self.GunToShoot = nil;
   self.GunHolder = nil;
   self.FrameToGun = 0;

 
   
end

function Update(self)


   if self.HasMOGun == 0 then
   for mo in MovableMan.Particles do
      for i = 1, MovableMan:GetMOIDCount() - 1 do
         mo = MovableMan:GetMOFromID(i)
         if mo.PresetName == "Vocal-0-ID" then
            local avgx = mo.Pos.X - self.Pos.X;
            local avgy = mo.Pos.Y - self.Pos.Y;
            local maindist = math.sqrt(avgx ^ 2 + avgy ^ 2);
            if maindist < 100 then
               if mo.ID ~= mo.RootID then
                  self.GunToShoot = ToAttachable(mo);
                  print ("Weapon found: " .. self.GunToShoot.PresetName .. " Number: " .. self.GunToShoot.ID);
                  for modude in MovableMan.Actors do
                     modude = MovableMan:GetMOFromID(self.GunToShoot.RootID);
                     self.GunHolder = ToActor(modude);
                     print ("Actor found: " .. self.GunHolder.PresetName);
                     self.HasMOGun = 1;
                  end
               else
                  self.ToDelete = true;
               end
               
            end
         end
      end
   end
   end


   
   local GunToFrame = self.GunToShoot.Frame;
   if GunToFrame < 37 then
      if self.timer:IsPastRealMS(37) and self.FrameToGun < 38 then
--         print ("Current frame: " .. self.GunToShoot.Frame);
--         print ("increasing frame by 1.");
         self.GunToShoot.Frame = self.FrameToGun;
         self.FrameToGun = self.FrameToGun + 1;
         self.timer:Reset()
      end
   else
      self.Boom = CreateAEmitter("Sonic Burst Heavy");
      self.Marker = CreateMOPixel("Sonic Burst Pixel Normal Heavy");      
      local theta = ToActor(self.GunHolder):GetAimAngle(true);
      self.Marker.Pos = self.GunHolder.Pos + Vector(math.cos(theta)*100,math.sin(theta)*-100);
      self.Boom.Pos = self.GunHolder.Pos + Vector(math.cos(theta)*50,math.sin(theta)*-50);
      self.Boom.Vel = (self.Boom.Pos - self.Marker.Pos) * 0.1;
      MovableMan:AddParticle(self.Boom);
      print ("Adding particle: " .. self.Boom.PresetName);
      print ("IMMA FIRIN' MAH LAZOR!!!");
      self.ToDelete = true;
   end
end


The actor/gun finding part is ok, all the print lines return the correct PresetNames, but the actual effect on the gun is far from perfect. When the fire key is pressed, the gun begins animating. The frames, although, keep resetting to the first one before advancing, creating an ugly, jagged animation. Surprisingly, if there are too many MOIDS in-game, the simulation will freeze for a couple of seconds, and then animate perfectly right, without the repeated frames. Plus, the frame check in the end always returns a zero, no matter which frame the gun is displaying. The particle it is supposed to create also appears only sporadically, sometimes at the right frame, sometimes right when you press the trigger. I suspect i'm running into some kind of hardcoded barrier, but I've got a whole 38 frames of animation and want to use them all, thank you. So, anyone got ideas on how to solve this?

Author:  Grif [ Thu Dec 17, 2009 1:00 am ]
Post subject:  Re: More Lua Trouble

Particles like hdfirearms and mosparticles constantly try to reset their frames. It's extremely difficult (impossible) to get them to consistently animate with Lua when they're not set to do so by .ini code.

Author:  Areku [ Thu Dec 17, 2009 1:54 am ]
Post subject:  Re: More Lua Trouble

Dang again. So I reckon it would require some fidgeting with the animmode and duration, right? I am unsure on how to proceed, though, as setting the animmode to 4 only lets it animate the first frames.

Author:  Grif [ Thu Dec 17, 2009 4:00 pm ]
Post subject:  Re: More Lua Trouble

Uh.

Short of using guns with spinup, hdfirearms are hardcoded to only animate into their first and second frames "naturally", meaning that you're SOL, at least insofar as using an actual gun to do your animation.

Author:  Areku [ Thu Dec 17, 2009 6:26 pm ]
Post subject:  Re: More Lua Trouble

Grif wrote:
hdfirearms are hardcoded to only animate into their first and second frames "naturally".


Not quite. Using AnimMode = 4, I was able to make another gun show a whole five frames when shooting. And spinup doesn't help anyway, as it always makes the animation play several times in a row, no matter what the duration variables are set to. Oh well, I guess I'll just follow your advice and transform the gun in a pseudo-attachable, then.

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/