Data Realms Fan Forums
http://forums.datarealms.com/

For Loops
http://forums.datarealms.com/viewtopic.php?f=73&t=29715
Page 1 of 1

Author:  Mongoska [ Fri Jan 27, 2012 10:00 pm ]
Post subject:  For Loops

Bonsoir (ou bonjour), mes amis! I'm trying to understand For Loops properly (I understand the basic principles of i = x,x and what that does), and I figure if I have one within a script I'm using explained, I'll gain a lot more understanding of both For Loops and my script.

What is this doing, exactly?

Code:
function Create(self)
   local thisID = ToActor(self).ID
   local MoObj
   for i = 1, MovableMan:GetMOIDCount() do
      if (i ~= thisID) and (MovableMan:GetRootMOID(i) == thisID) then
         MoObj = MovableMan:GetMOFromID(i)
         if MoObj.PresetName == "Weasel Face" then
            self.Head = ToAttachable(MoObj)
            break
         end
      end
   end
end


Thanks!

Author:  Roast Veg [ Fri Jan 27, 2012 10:26 pm ]
Post subject:  Re: For Loops

The code you have posted here cycles through all movable objects in the scene, finds one with an appropriate presetname and root movable object, and registers it as "self.head" presumably for use later.

The code is, however, outdated and unnecessary in the current build, since attachables became a lot easier to script.

Author:  Mongoska [ Fri Jan 27, 2012 11:42 pm ]
Post subject:  Re: For Loops

Ah! I suspected as much! It certainly appeared to be registering self.head, yet I made a reference to self.FGLeg and that worked, so I wondered what else it could be for.

Interesting that you should reply, since the original author of the code (according to Naxete) was you!

As I'm already on the subject and since I'm still having difficulties with it, perhaps someone could help me out with my own For Loop:

Code:
      for i = 1,15 do
         if self.HFlipped then
            damagePar.RotAngle = damagePar.RotAngle + 0.13
         else
            damagePar.RotAngle = damagePar.RotAngle - 0.13
         end
         MovableMan:AddParticle(damagePar)
      end


I replaced "MovableMan:AddParticle(damagePar)" with the above For Loop, causing it to stop working with "ERROR: no overload of 'MovableMan:AddParticle' matched the argument 'MovableManager, MOPixel) candidates are: MovableManager:AddParticle(MovableManager&, MovableObject*)' where "&" is a symbol unknown to me resembling a semicolon - a horizontal line with a circle above it.

I figured since the code worked before I added the For Loop that the error was within the loop itself, so I didn't feel it necessary to post the rest of the code.

Author:  Roast Veg [ Sat Jan 28, 2012 12:23 am ]
Post subject:  Re: For Loops

It was mine? Hahahaa, wow. :)

Anyway, the issue here is that your process for generating the MOPixel is written incorrectly, but I'd need to see all of the code to work out where, that little snippet doesn't tell me enough as it is.

Author:  Mongoska [ Sat Jan 28, 2012 1:11 am ]
Post subject:  Re: For Loops

Aha, okay! Here it is!

Code:
function Create(self)
   self.fired = false;
end

function Update(self)
   if self:GetController():IsState(Controller.WEAPON_FIRE) and self.Head:IsAttached() and self.FGLeg:IsAttached() and self.fired == false then
      local Emitter = CreateAEmitter("Maximilian Talon", "Maximilian.rte")
     local damagePar = CreateMOPixel("Maximilian Talon Slash", "Maximilian.rte")
      Emitter.Vel = self.Vel
     damagePar.Vel = Vector(30,0):RadRotate(self:GetAimAngle(true));
     
     if self.HFlipped then
         Emitter.RotAngle = self.Head.RotAngle + 3.1415
      else
         Emitter.RotAngle = self.Head.RotAngle
      end
    
     damagePar:SetWhichMOToNotHit(self,-1)
     damagePar.Pos = self.FGLeg.Pos + self.FGLeg:RotateOffset(Vector(0,-15))
      Emitter.Pos = self.FGLeg.Pos + self.FGLeg:RotateOffset(Vector(0,-15))
        
      for i = 1,15 do
         if self.HFlipped then
            damagePar.RotAngle = damagePar.RotAngle + 0.13
         else
            damagePar.RotAngle = damagePar.RotAngle - 0.13
         end
         MovableMan:AddParticle(damagePar)
      end
      
      MovableMan:AddParticle(Emitter)
      self.fired = true;
   end
   if self:IsPlayerControlled() == false then
      self:GetController():SetState(Controller.BODY_CROUCH, false);
   end
   local Ctrl = self:GetController();
   if Ctrl:IsState(Controller.BODY_JUMPSTART) then
   else
      self.AngularVel = self.AngularVel * 0.75;
   end

   if not self:GetController():IsState(Controller.WEAPON_FIRE) then
      self.fired = false;
   end
end

Author:  Mongoska [ Sun Jan 29, 2012 1:36 am ]
Post subject:  Re: For Loops

I'm still having trouble with this! If anyone has any idea as to what I'm doing wrong it'd be appreciated!

I just can't fathom what relationship the AddParticle has with the For Loop that makes in incompatible with my CreateMOPixel function!

Unless...

Author:  Mongoska [ Sun Jan 29, 2012 2:53 am ]
Post subject:  Re: For Loops

Revelation!

I'm still not sure why the local variable had to be defined within the For Loop, but that was the problem!

Code:
      for i = 1,15 do
         local damagePar = CreateMOPixel("Maximilian Talon Slash", "Maximilian.rte")
         local damageParRot = 0.13 * i
         if i == 1 then
            if self.HFlipped then
               damagePar.Vel = Vector(30,0):RadRotate(self:GetAimAngle(true) + 0.975);
            else
               damagePar.Vel = Vector(30,0):RadRotate(self:GetAimAngle(true) - 0.975);
            end
            damagePar:SetWhichMOToNotHit(self,-1)
            damagePar.Pos = self.FGLeg.Pos + self.FGLeg:RotateOffset(Vector(0,-15))
            MovableMan:AddParticle(damagePar)
         else
            if self.HFlipped then
               damagePar.Vel = Vector(30,0):RadRotate(self:GetAimAngle(true) + 0.975 - damageParRot);
               damagePar:SetWhichMOToNotHit(self,-1)
               damagePar.Pos = self.FGLeg.Pos + self.FGLeg:RotateOffset(Vector(0,-15))
            else
               damagePar.Vel = Vector(30,0):RadRotate(self:GetAimAngle(true) - 0.975 + damageParRot);
               damagePar:SetWhichMOToNotHit(self,-1)
               damagePar.Pos = self.FGLeg.Pos + self.FGLeg:RotateOffset(Vector(0,-15))
            end
            MovableMan:AddParticle(damagePar)
         end
      end


I made the rotations work now, too.

Hmm... Could it be that the variables for For Loops are defined within them in order to make the "i" variable work? That'd make sense.

Author:  TheLastBanana [ Sun Jan 29, 2012 9:00 am ]
Post subject:  Re: For Loops

CreateMOPixel makes the actual particle. MovableMan:AddParticle just tells the game to include it in the simulation from that point on. The problem is that you were creating one particle, but trying to add it 15 times. By moving that into the for loop, you're creating a new particle to add each time.

Author:  Mongoska [ Sun Jan 29, 2012 10:34 am ]
Post subject:  Re: For Loops

Good gosh and blimey! It's so simple!

Well, shoot me down and sell my corpse to a bishop!

Thanks!

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