View unanswered posts | View active topics It is currently Thu Dec 26, 2024 5:34 am



Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
 Sharplength adjustment through lua? 
Author Message
User avatar

Joined: Thu Jan 21, 2010 10:59 pm
Posts: 151
Reply with quote
Post Sharplength adjustment through lua?
Could someone make a script that lowers a gun's sharplength(by 50) when an attachable is destroyed?

I wanted to have a scope that can be shot off, and once it is, the sharplength will be lowered making so that the scope has a function.

Also, there was one topic that did show up in the search where the author also wanted to know the same thing but there were no answers or rather there was but none that answered the question.

EDIT: Or, you know, someone could at least say what the function name for sharplength in lua is (unless it is exactly that).


Last edited by ShadowStorm on Wed Nov 21, 2012 12:30 am, edited 2 times in total.



Tue Nov 06, 2012 12:03 am
Profile
User avatar

Joined: Tue Jun 12, 2007 11:52 pm
Posts: 13144
Location: Here
Reply with quote
Post Re: Sharplength adjustment through lua?
I'm pretty sure it's HDFirearm.Sharplength


Fri Nov 09, 2012 3:50 am
Profile
User avatar

Joined: Thu Jan 21, 2010 10:59 pm
Posts: 151
Reply with quote
Post Re: Sharplength adjustment through lua?
Thanks CaveCricket, wish they added that to the wiki.


Fri Nov 09, 2012 5:57 am
Profile
User avatar

Joined: Thu Jan 21, 2010 10:59 pm
Posts: 151
Reply with quote
Post Re: Sharplength adjustment through lua?
Err, could someone help me out with this?
Code:
function Update(self)
   local Parent = ToHDFirearm(HDFirearm) -- Set Parent variable as the parent of the attachable, which is the gun(?)
   local Attachable = Attachable("Scope") -- Set "Scope" attachable as the attachable variable
   if Attachable:IsAttached then -- Checks if the attachable is attached to the HDFirearm, if it is, then do nothing.
   end
   else -- If it's not then reduce sharplength by 50
      Parent.SharpLength = Parent.SharpLength -50 -- Reduce sharplength
   end
end

Edit: fix't


Last edited by ShadowStorm on Tue Nov 20, 2012 3:56 am, edited 2 times in total.



Mon Nov 19, 2012 11:06 pm
Profile
Forum Moderator
User avatar

Joined: Fri Feb 02, 2007 3:53 pm
Posts: 1896
Location: in my little gay bunker
Reply with quote
Post Re: Sharplength adjustment through lua?
If it isn't read only, you need to make sure it is EXACTLY like it is in the ini files. The variables are case sensitive, etc. Also that script would very quickly reduce the sharplength to very high negatives.


Tue Nov 20, 2012 12:18 am
Profile
User avatar

Joined: Sun Jan 28, 2007 10:32 pm
Posts: 1609
Location: UK
Reply with quote
Post Re: Sharplength adjustment through lua?
I know it's not read-only, 'cause Coops made an auto-adjust sniper scope zoom a while back.

Also p3l is right. It's SharpLength not Sharplength, and you need to put some kind of safety catch in so that it only reduces sharplength once, or else you'll never be able to aim once the scope is taken off. :lol:


Tue Nov 20, 2012 1:01 am
Profile YIM
User avatar

Joined: Thu Jan 21, 2010 10:59 pm
Posts: 151
Reply with quote
Post Re: Sharplength adjustment through lua?
So.. The script is mostly fine? Because I kinda just mashed it together...

Also thanks for the tips.

EDIT: Hmm, doesn't seem to work, need to work on it :/ I'd be happy if I can even get it to go to the negative sharplength stage, then I would know I'm going in the right direction.


Last edited by ShadowStorm on Tue Nov 20, 2012 4:08 am, edited 1 time in total.



Tue Nov 20, 2012 2:07 am
Profile

Joined: Fri Sep 10, 2010 1:48 am
Posts: 666
Location: Halifax, Canada
Reply with quote
Post Re: Sharplength adjustment through lua?
Not gonna guarantee this'll work but I think it should. You need to stick this on the gun, not the scope, there's some notes below about that.
Code:
function Create(self)
   self.SharpRedAmt = 50; --The amount sharpaim is reduced by
   --This part finds the scope attachable. Relies on it being named "scope". It then prints a line to the console to let you know it worked okay
   if not self.Scope then
      local MoObj
      for i = 1, MovableMan:GetMOIDCount()-1 do
         if MovableMan:GetRootMOID(i) == self.ID then
             MoObj = MovableMan:GetMOFromID(i)
            if MoObj.PresetName == "Scope" then
               self.Scope = ToAttachable(MoObj);
               print ("scope found");
            end
         end
      end
   end
end
function Update(self)
   if self.Scope ~= nil then
      if not self.Scope:IsAttached() then
         self.SharpLength = self.SharpLength - self.SharpRedAmt; -- Reduce sharplength
         self.Scope = nil;
      end
   end
end

The only thing I'm not sure of is what this script will do if the scope gets destroyed instead of knocked off. It may crash the game, MovableMan:IsObject(x) doesn't seem to exist so I'm not 100% sure how to make sure it actually exists.

So your biggest problem, aside form Sharplength instead of SharpLenght were the lines
local Parent = ToHDFirearm(HDFirearm) -- Set Parent variable as the parent of the attachable, which is the gun(?)
local Attachable = Attachable("Scope") -- Set "Scope" attachable as the attachable variable
You didn't define HDFirearm before calling ToHDFirearm and that wouldn't have done much anyway. To find the parent you have to search this thing's RootID, that can lead to problems because I can't remember if RootID here will find the gun or the actor holding it. Either way you'd eventually get the gun. As for the second line, you're setting the variable Attachable to be a nonexistent function with the argument "Scope", unless I'm mistaken that does nothing and is probably filling your console with errors.
Aside from that, if you did get the parent properly I'm not 100% sure attachable scripts actually run when they're not attached, I've had some problems with that in the past so I think it's safer to put the script right on the gun.

Hope all this helps, enjoy :)


Tue Nov 20, 2012 4:07 am
Profile
User avatar

Joined: Thu Jan 21, 2010 10:59 pm
Posts: 151
Reply with quote
Post Re: Sharplength adjustment through lua?
First of all, thanks for pointing out the mistakes I made and for making the script. It certainly helps me understand (to a degree) what does what.

Unfortunately though, it doesn't seem to print 'scope found' in the lua console..


Tue Nov 20, 2012 4:50 am
Profile

Joined: Fri Sep 10, 2010 1:48 am
Posts: 666
Location: Halifax, Canada
Reply with quote
Post Re: Sharplength adjustment through lua?
What's the preset name of your scope? If it's not Scope then that might be the problem.
But more likely than that is that I ran into the problem I mentioned earlier about finding the parent; namely that it's checking to see if the object's RootID == self.ID. I used this on an actor instead of a weapon, hence the problem. So you need to change
Code:
if MovableMan:GetRootMOID(i) == self.ID then

to
Code:
if MovableMan:GetRootMOID(i) == self.RootID then


Hopefully that'll do it.


Tue Nov 20, 2012 5:22 am
Profile
User avatar

Joined: Thu Jan 21, 2010 10:59 pm
Posts: 151
Reply with quote
Post Re: Sharplength adjustment through lua?
I was about to post my moment of glory to a depressing adventure until I messed around with it again and it worked.

So Bad Boy, I salute you for your help. Twice now.

Edit: errhg actually, something tells me the script works randomly..
Edit: upon further testing I found that it didn't print for the first actor, but it printed for the next 8 actors I called in.

Edit: ok, so heres the problem. If I take control of the craft and open the doors, causing me to auto switch to the actor, the script doesn't work. However, if I let the craft ai drop the actor off, it works.


Tue Nov 20, 2012 6:32 am
Profile

Joined: Fri Sep 10, 2010 1:48 am
Posts: 666
Location: Halifax, Canada
Reply with quote
Post Re: Sharplength adjustment through lua?
Yeah, it's this weird thing that CC does, similarly you'll probably have problems if you place it in the base building phase.
Luckily yours doesn't have to delete anything if it's not attached so your fix is pretty simple. Move or copy the scope finding part to the update function, you'll also want to put a line saying self.Scope = nil in the Create to make sure you don't get any errors.
Code:
if self.Scope == nil then...findscope
elseif self.Scope ~= nil then...proceed as normal


Tue Nov 20, 2012 1:10 pm
Profile
User avatar

Joined: Thu Jan 21, 2010 10:59 pm
Posts: 151
Reply with quote
Post Re: Sharplength adjustment through lua?
Bad Boy wrote:
Move or copy the scope finding part to the update function, you'll also want to put a line saying self.Scope = nil in the Create to make sure you don't get any errors.
Code:
if self.Scope == nil then...findscope
elseif self.Scope ~= nil then...proceed as normal

(after several attempts to follow the instructions)
Sorry, where in the update function do I move the scope finding part to and where do I put the self.Scope == nil in the create function.
(Hmm I'm hitting the point where I'm feeling like I'm bothering people, not that I didn't feel like that before..)


Tue Nov 20, 2012 7:35 pm
Profile

Joined: Fri Sep 10, 2010 1:48 am
Posts: 666
Location: Halifax, Canada
Reply with quote
Post Re: Sharplength adjustment through lua?
No worries, I'm happy to help, particularly since it's not highly time consuming :)
This here ought to do it.
Code:
function Create(self)
   self.SharpRedAmt = 50; --The amount sharpaim is reduced by
   self.ScopeFound = false;
   --This part finds the scope attachable. Relies on it being named "scope". It then prints a line to the console to let you know it worked okay
   if not self.Scope then
      local MoObj
      for i = 1, MovableMan:GetMOIDCount()-1 do
         if MovableMan:GetRootMOID(i) == self.ID then
             MoObj = MovableMan:GetMOFromID(i)
            if MoObj.PresetName == "Scope" then
               self.Scope = ToAttachable(MoObj);
               self.ScopeFound = true;
               print ("scope found");
            end
         end
      end
   end
end
function Update(self)
   if self.Scope == nil and self.ScopeFound == false then
      local MoObj
      for i = 1, MovableMan:GetMOIDCount()-1 do
         if MovableMan:GetRootMOID(i) == self.ID then
             MoObj = MovableMan:GetMOFromID(i)
            if MoObj.PresetName == "Scope" then
               self.Scope = ToAttachable(MoObj);
               self.ScopeFound = true;
               print ("scope found in update");
            end
         end
      end
   elseif self.Scope ~= nil and self.ScopeFound == true then
      if not self.Scope:IsAttached() then
         self.SharpLength = self.SharpLength - self.SharpRedAmt; -- Reduce sharplength
         self.Scope = nil;
      end
   end
end


Tue Nov 20, 2012 10:25 pm
Profile
User avatar

Joined: Thu Jan 21, 2010 10:59 pm
Posts: 151
Reply with quote
Post Re: Sharplength adjustment through lua?
Many thanks, it works. Just had to change the few things you mentioned before.


Wed Nov 21, 2012 12:29 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 16 posts ]  Go to page 1, 2  Next

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.096s | 16 Queries | GZIP : Off ]