Author |
Message |
Areku
Joined: Fri Oct 17, 2008 9:46 pm Posts: 5212 Location: The Grills Locker.
|
Lua tricks Topic
So, basically, this is a topic for all those useful snippets of code I occasionally find while playing with Sparkle Magic TM. Just thought I'd post 'em here for easier access, and 'cause sometimes you're having a lot of trouble with a complex script when a small bit of code would solve the problem. Feel free to post your other Lua tricks here, along with a brief description of their purpose. tl;dr: Post your cool Lua stuff (not console commands, we already have a thread for that) here. To get the topic started, a tiny bit of code that I found today, pretty useful stuff: Code: function Update(self)
if self:GetAltitude(5,2) < 3 then self.ToDelete = true; end end
This code , designed to be used on MOPixels, makes the bullet harmless on terrain, while leaving its interactions with other particles unchanged. Good for when you wanna make a powerful sniper, but don't want it to rape too much terrain.
|
Tue Aug 18, 2009 1:06 am |
|
|
mail2345
Joined: Tue Nov 06, 2007 6:58 am Posts: 2054
|
Re: Lua tricks Topic
Eh, I need a place to centralize my insane hacksuseful tricks. viewtopic.php?f=73&t=16213viewtopic.php?f=73&t=15692viewtopic.php?f=73&t=15359Gonna post some neat armour piercing code later.
|
Tue Aug 18, 2009 5:38 am |
|
|
Grif
REAL AMERICAN HERO
Joined: Sat Jan 27, 2007 10:25 pm Posts: 5655
|
Re: Lua tricks Topic
Code: function KeyPressLength() if self.init == 0 then self.init = 1; self.presses = 0; self.pressed = 0; self.presstimer = Timer(); end --replace BODY_JUMP with whichever if self:GetController():IsState(Controller.BODY_JUMP) == true then self.pressed = 1; else if self.pressed == 1 then self.pressed = 0; self.presstime = self.presstimer.ElapsedSimTimeMS; --put any code to execute when you stop holding the button here elseif self.pressed == 0 then self.presstimer:Reset(); end end end Get the duration of a keypress and execute code only when it's released.
|
Sat Aug 22, 2009 3:29 pm |
|
|
xerxys
Joined: Sun Feb 17, 2008 11:21 am Posts: 54
|
Re: Lua tricks Topic
TLB's conveyor area check:Code: if (item.Pos.X >= self.Pos.X - 15) and (item.Pos.X <= self.Pos.X + 15) and (item.Pos.Y >= self.Pos.Y - 16) and (item.Pos.Y <= self.Pos.Y + 16) then Another big proximity check:Code: local curdist = 600; for actor in MovableMan.Actors do local avgx = actor.Pos.X - self.Pos.X; local avgy = actor.Pos.Y - self.Pos.Y; local dist = math.sqrt(avgx ^ 2 + avgy ^ 2); if dist < curdist then curdist = dist; Kyred's proximity check:Code: if math.abs(actor.Pos.X - self.Pos.X) < 40 and math.abs(actor.Pos.Y - self.Pos.Y) < 40 then The Magnitude() thing:Code: if (thing1.Pos - thing2.Pos).Magnitude < desiredproximity then
|
Mon Aug 24, 2009 7:51 pm |
|
|
Grif
REAL AMERICAN HERO
Joined: Sat Jan 27, 2007 10:25 pm Posts: 5655
|
Re: Lua tricks Topic
First check (the one for conveyors) can be done more efficiently with boxes. SceneMan:ShortestDistance is the most efficient for circular checks, but it isn't quite at the same scale (Pythagorean theorem gets pixel-accurate dimensions, but vector magnitudes aren't the same)
|
Mon Aug 24, 2009 11:59 pm |
|
|
piipu
Joined: Mon Jun 30, 2008 9:13 pm Posts: 499 Location: Finland
|
Re: Lua tricks Topic
Vector magnitude is exactly the same as the dist calculated with that pythagoras stuff. Vector.Magnitude is just shorter and likely a lot faster. And SceneMan:ShortestDistance() just returns a vector to the target, not actual distance, so you have to use Vector.Magnitude with that too.
|
Tue Aug 25, 2009 5:34 am |
|
|
Grif
REAL AMERICAN HERO
Joined: Sat Jan 27, 2007 10:25 pm Posts: 5655
|
Re: Lua tricks Topic
Yeah, I meant SceneMan:ShortestDistance.Magnitude. But uh, in my experience it's slightly skewed.
|
Tue Aug 25, 2009 5:53 am |
|
|
Kyred
Joined: Sun May 31, 2009 1:04 am Posts: 308
|
Re: Lua tricks Topic
Grif wrote: (Pythagorean theorem gets pixel-accurate dimensions, but vector magnitudes aren't the same) Distance in CC is measured in pixels from the target. Finding the magnitude of a vector stretched between two objects should return the number of pixels between the two. The Pythagorean theorem is often used to find the magnitude of a vector. To use the Pythagorean theorem to find the distance between the dummy and the soldier in the picture below, you would do: Code: math.sqrt( (588-190)^2 + (495-250)^2 ) = math.sqrt(398^2 + 245^2) The magnitude of vector V3 can be found by using the Pythagorean theorem on the V3's components: Code: math.sqrt(398^2 + 245^2) These both come out to the same value. So, unless if Vector.Magnitude uses a different, less accurate, calculation method, Vector.Magnitude and the Pythagorean theorem methods should come out to the same value. @Grif: By skewed do you mean shorter or longer? Anything in the picture with a P (like P1 or P2) stands for a point. V's stand for vectors.
|
Wed Aug 26, 2009 9:38 pm |
|
|
Grif
REAL AMERICAN HERO
Joined: Sat Jan 27, 2007 10:25 pm Posts: 5655
|
Re: Lua tricks Topic
I know that they should be the same but I've had experiences where it doesn't line up with the pixel-width of a glow. This could have to do with Data's method of drawing glows or something, but it's distinctly not the actual diameter of the glow in question.
|
Wed Aug 26, 2009 11:45 pm |
|
|
DrLuke
Joined: Thu Sep 03, 2009 6:01 pm Posts: 56
|
Re: Lua tricks Topic
Build your own timer: Code: function Create(self) self.timer = 0; end
function Update(self) self.timer = self.timer + 1; end
|
Sat Oct 03, 2009 10:56 am |
|
|
Roon3
Joined: Sun May 11, 2008 12:50 pm Posts: 899
|
Re: Lua tricks Topic
|
Sat Oct 03, 2009 12:16 pm |
|
|
CrazyMLC
Joined: Fri Dec 22, 2006 4:20 am Posts: 4772 Location: Good news everyone!
|
Re: Lua tricks Topic
Areku wrote: Code: function Update(self)
if self:GetAltitude(5,2) < 3 then self.ToDelete = true; end end
This code , designed to be used on MOPixels, makes the bullet harmless on terrain, while leaving its interactions with other particles unchanged. Good for when you wanna make a powerful sniper, but don't want it to rape too much terrain. I think you just made my day. Also, you might, maybe, want to enforce a rule where you cannot post if you don't have a code snippet.
|
Sat Oct 03, 2009 2:27 pm |
|
|
findude
Joined: Tue Dec 12, 2006 3:10 pm Posts: 495 Location: Uncertain quantum state
|
Re: Lua tricks Topic
Just so happens that the default gravity acceleration is nullified by substracting ~0.3 from the objects Y velocity each update call. Zerogravity!
|
Mon Oct 12, 2009 10:14 am |
|
|
Grif
REAL AMERICAN HERO
Joined: Sat Jan 27, 2007 10:25 pm Posts: 5655
|
Re: Lua tricks Topic
findude wrote: Just so happens that the default gravity acceleration is nullified by substracting ~0.3 from the objects Y velocity each update call. Zerogravity! self.Vel.Y = self.Vel.Y - SceneMan.GlobalAccSalar * TimerMan.DeltaTimeSecs Salar is mispelled in the actual function and idk exactly what deltatime is referenced as, but that's your ideal zero gravity.
|
Mon Oct 12, 2009 3:08 pm |
|
|
Kyred
Joined: Sun May 31, 2009 1:04 am Posts: 308
|
Re: Lua tricks Topic
Grif wrote: findude wrote: Just so happens that the default gravity acceleration is nullified by substracting ~0.3 from the objects Y velocity each update call. Zerogravity! self.Vel.Y = self.Vel.Y - SceneMan.GlobalAccSalar * TimerMan.DeltaTimeSecs Salar is mispelled in the actual function and idk exactly what deltatime is referenced as, but that's your ideal zero gravity. For anyone who is wonder how Grif got this equation: Velocity = Initial Velocity + Acceleration * Change In Time (ie. V = V0 + A*t)We want to apply a velocity upward. In CC, -Y means up, so the equation changes to: V = V0 - A*tTimerMan.DeltaTimeSecs gives the ratio of Real Time vs. Simulation Time. Or to put it simply, the time in seconds between each Sim Update (usually 0.003 something. Check your settings.ini to see what it is by default). Also remember, that different objects have a different GlobalAcc value set by their .ini information. You'll need to account for that as well.
|
Mon Oct 12, 2009 6:13 pm |
|
|
|