View unanswered posts | View active topics It is currently Tue Apr 16, 2024 11:39 am



Reply to topic  [ 9 posts ] 
 For Loops 
Author Message
User avatar

Joined: Tue Jan 24, 2012 12:40 am
Posts: 37
Location: Weymouth, England
Reply with quote
Post 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!


Fri Jan 27, 2012 10:00 pm
Profile
Data Realms Elite
Data Realms Elite
User avatar

Joined: Tue May 25, 2010 8:27 pm
Posts: 4521
Location: Constant motion
Reply with quote
Post 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.


Fri Jan 27, 2012 10:26 pm
Profile
User avatar

Joined: Tue Jan 24, 2012 12:40 am
Posts: 37
Location: Weymouth, England
Reply with quote
Post 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.


Fri Jan 27, 2012 11:42 pm
Profile
Data Realms Elite
Data Realms Elite
User avatar

Joined: Tue May 25, 2010 8:27 pm
Posts: 4521
Location: Constant motion
Reply with quote
Post 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.


Sat Jan 28, 2012 12:23 am
Profile
User avatar

Joined: Tue Jan 24, 2012 12:40 am
Posts: 37
Location: Weymouth, England
Reply with quote
Post 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


Sat Jan 28, 2012 1:11 am
Profile
User avatar

Joined: Tue Jan 24, 2012 12:40 am
Posts: 37
Location: Weymouth, England
Reply with quote
Post 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...


Sun Jan 29, 2012 1:36 am
Profile
User avatar

Joined: Tue Jan 24, 2012 12:40 am
Posts: 37
Location: Weymouth, England
Reply with quote
Post 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.


Sun Jan 29, 2012 2:53 am
Profile
DRL Developer
DRL Developer
User avatar

Joined: Wed Dec 13, 2006 5:27 am
Posts: 3138
Location: A little south and a lot west of Moscow
Reply with quote
Post 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.


Sun Jan 29, 2012 9:00 am
Profile WWW
User avatar

Joined: Tue Jan 24, 2012 12:40 am
Posts: 37
Location: Weymouth, England
Reply with quote
Post Re: For Loops
Good gosh and blimey! It's so simple!

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

Thanks!


Sun Jan 29, 2012 10:34 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.043s | 15 Queries | GZIP : Off ]