Lua and attachables work around.
Author |
Message |
CrazyMLC
Joined: Fri Dec 22, 2006 4:20 am Posts: 4772 Location: Good news everyone!
|
Re: Lua and attachables work around.
"Ifs make a 'scope' too." ?
|
Sun Jun 21, 2009 3:14 am |
|
|
mail2345
Joined: Tue Nov 06, 2007 6:58 am Posts: 2054
|
Re: Lua and attachables work around.
Oh, I forgot about scoping.
Still, for speed reasons, it's faster to make one variable then to make it over and over again.
|
Sun Jun 21, 2009 3:16 am |
|
|
TheLastBanana
DRL Developer
Joined: Wed Dec 13, 2006 5:27 am Posts: 3138 Location: A little south and a lot west of Moscow
|
Re: Lua and attachables work around.
Marginally faster, yes. And as much as it is possible to declare the variable again, it can lead to some serious problems in more complicated scripts. I would strongly advise against it. Another problem with that code is that the for loop goes from 1 to MovableMan:GetMOIDCount. You would want to subtract one from the MOID count, or it will always return the last one as nil.
|
Sun Jun 21, 2009 7:26 am |
|
|
CrazyMLC
Joined: Fri Dec 22, 2006 4:20 am Posts: 4772 Location: Good news everyone!
|
Re: Lua and attachables work around.
Sorry to bump, but how do you use this to find a specific attachable on an actor that the Lua is applied to? I don't understand the script.
|
Wed Oct 21, 2009 7:31 am |
|
|
Geti
Joined: Sun Jul 13, 2008 9:57 am Posts: 4886 Location: some compy
|
Re: Lua and attachables work around.
if i understood correctly, you want to run something on an attachable? right, heres how, from the actor the thing is attached to. Code: --in create(), have this block to grab all child objects. self.attachables = {} -- create a table to put the children into for i = 1, MovableMan:GetMOIDCount() - 1 do --we have to use getmoidcount -1 to prevent errors self.mo = MovableMan:GetMOFromID(i) --get the mo, use self.mo for simplicity if self.mo.RootID == self.ID and i ~= self.ID then --if it is a child of us but not us table.insert(self.attachables, ToAttachable(self.mo)) --then put it into the table. end end --then, in update(), have this block, with whatever you want to do in it. for k,v in ipairs(self.attachables) do --iterates through the table if string.find(v.PresetName, "INSERT THE PRESETNAME OF THE ONE YOU WANT TO DO STUFF TO HERE") then --find the presetname to identify the object. v:function() --do stuff on it here --more stuff here if you want end end there you go. you'll need to add some "IS THE ATTACHABLE STILL ALIVE?" checks, but thats handling attachables in a nutshell.
|
Wed Oct 21, 2009 8:22 am |
|
|
CrazyMLC
Joined: Fri Dec 22, 2006 4:20 am Posts: 4772 Location: Good news everyone!
|
Re: Lua and attachables work around.
Wonderful! Can you explain to me what the v:function() is so I understand it in it's entirety so I can mess with it?
|
Wed Oct 21, 2009 8:38 am |
|
|
Geti
Joined: Sun Jul 13, 2008 9:57 am Posts: 4886 Location: some compy
|
Re: Lua and attachables work around.
v is the attachable at this point, you can pass functions and change all the variables that are exposed through it. ipairs iterates the table of attachables and returns k and v as index and value respectively
|
Wed Oct 21, 2009 9:02 am |
|
|
411570N3
Joined: Wed Jan 07, 2009 10:26 am Posts: 4074 Location: That quaint little British colony down south
|
Re: Lua and attachables work around.
If you have multiple identically named attachables then I presume you'd need to find another distinguishing factor and switch that into the PresetName checking bit to get it to work, correct?
|
Wed Oct 21, 2009 9:18 am |
|
|
CrazyMLC
Joined: Fri Dec 22, 2006 4:20 am Posts: 4772 Location: Good news everyone!
|
Re: Lua and attachables work around.
You'd just need to make a copy of the object in ini code and give it an A or B or something at the end of it's preset name. At the beginning when you say put it in a function Create(), can I put it in function Create(self)? Geti wrote: you'll need to add some "IS THE ATTACHABLE STILL ALIVE?" checks, but thats handling attachables in a nutshell. Whats the best way to do that? :/ If v:IsSetToDelete?
|
Wed Oct 21, 2009 9:31 am |
|
|
Geti
Joined: Sun Jul 13, 2008 9:57 am Posts: 4886 Location: some compy
|
Re: Lua and attachables work around.
CrazyMLC wrote: You'd just need to make a copy of the object in ini code and give it an A or B or something at the end of it's preset name. Correct, or you could use the parentoffset values, but really, you dont usually get multiples of an attachable. maybe a limb, but we cant do very much with them at the moment anyway <_< CrazyMLC wrote: At the beginning when you say put it in a function Create(), can I put it in function Create(self) That was the intention, yes. Sorry for not being more clear :/ CrazyMLC wrote: Whats the best way to do that? :/ If v:IsSetToDelete? Well, I'd suggest a RootID check. Code: for k,v in ipairs(self.attachables) do if string.find(v.PresetName, "INSERT THE PRESETNAME OF THE ONE YOU WANT TO DO STUFF TO HERE") and v.RootID == self.ID then --do stuff on it here else --elseif v.RootID ~= self.ID then --use this rather than plain else if you want to only remove attachables that arent attached anymore. table.remove(self.attachables, k) --clean up useless pointers so we dont have to keep filtering them out later on end end It should work, im suggesting this because MovableMan:ValidMO(mo) hasnt worked well for me in the past, especially with attachables. note, this might cause errors later cause of the removal of all non-working pointers, im not sure how for handles an empty table..
|
Wed Oct 21, 2009 10:55 am |
|
|
|
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
|
|