Author |
Message |
Miles_T3hR4t
Joined: Mon Jun 04, 2007 5:55 am Posts: 1627 Location: Ohio
|
Lua Request- HALP
Hey. i'm not that great with lua, and probably never will be, i just use it for basic scripts like timers and stuff, and I'm horrible at making anything do anything other than gib, or stay at a fixed height. I need to basically get some lua copypasta. not a complex script either. actually I need 2 but the other is retardedly easy i'd think I need it to do the following. - Set gravity of the object to a fixed point, that point is whatever the closest actor is, human/crab/brain/rocket/dropship doesn't matter, teams dont matter, just whatever is closest. If that actor dies, then it needs to find the next closest actor and set the gravity towards that. I don't know how to check which actor is closest, I dont know how to set gravity to a coordinate, and I dont know how to check if that actor still exists. the other one is a 'force upright' script. I just need an object to always face upright, no matter what. The other thing is, I can tell an object to use multiple scripts, right? like fileA.lua and fileB.lua? (because trying to mix 2 scripts into 1 file doesn't sound like it would work) yes, this IS for the modding contest, yes this DOES make it deadly obvious which submission is mine when its released, but only to people who check this wonderful sparklemagic section. Anyway, I'd also like to make sure something is correct Code: function Create(self) self.somevariable = math.random(15000) self.sometimer = Timer() end
function Update(self) if self.sometimer:IsPastSimMS(35000+self.somevariable) then self:GibThis() end end
the goal of this code is to gib after 35001-50000 MS Im asking rather than testing because If I cant get the gravity thing to work this entire mod will not function properly. either i'd have to figure out how to make an AEmitter thrust towards an actor like a homing missile, without rotating its sprite, or make it an actor with a vacuum gun which would lag the MOID's to hell with what this needs to be. Thanks in advance.
|
Sun Oct 11, 2009 6:53 pm |
|
|
CrazyMLC
Joined: Fri Dec 22, 2006 4:20 am Posts: 4772 Location: Good news everyone!
|
Re: Lua Request- HALP
math.random needs a maximum and a minimum. IsPastSimMS is an argument. You want semi-colons. Code: function Create(self) self.somevariable = math.random(1,15000); self.sometimer = Timer(); end
function Update(self) if self.sometimer:IsPastSimMS(35000+self.somevariable) == true then self:GibThis(); end end
|
Sun Oct 11, 2009 7:44 pm |
|
|
Duh102
happy carebear mom
Joined: Tue Mar 04, 2008 1:40 am Posts: 7096 Location: b8bbd5
|
Re: Lua Request- HALP
I actually meant making a request for "need Lua person, plz respond if you want to help", but whatever. Live and learn.
|
Sun Oct 11, 2009 8:45 pm |
|
|
Miles_T3hR4t
Joined: Mon Jun 04, 2007 5:55 am Posts: 1627 Location: Ohio
|
Re: Lua Request- HALP
CrazyMLC wrote: math.random needs a maximum and a minimum. IsPastSimMS is an argument. You want semi-colons. Code: function Create(self) self.somevariable = math.random(1,15000); self.sometimer = Timer(); end
function Update(self) if self.sometimer:IsPastSimMS(35000+self.somevariable) == true then self:GibThis(); end end
actually, you don't need the semicolons, they are just optional linebreaks. i've used this without the math.random variable. from what i've heard math.random if the low-end is un-defined it is automatically 1, and on top of tha tI know that I don't need ==true after the if self.sometimer:ispastsimMS(), after all been there. the thing I don't know is if I can do number+variable inside of a statement. like self.sometimer:ispastsimms(35000+self.somevariable) or if it has to be self.somevariable = math.random(number) and self.othervariable = somevariable + number, THEN do self.sometimer:ispastsimms(othervariable) thats where I am not sure. Its like I said I haven't been working on this, and in- FFFF I just heard a gunshot outside and screams, i got to go. --edit a second one went off, while i was out to get drinks and ask the corner store what happened. it wasn't a gunshot it was an F-ing bomb. i walked over the second one. if i was 30 seconds later i'd be dead right now. so while i'm releaved that i'm not dead, i'm gonna take this more relaxed moment before I try to call anyone about it to finish what I was doing. -- I haven't loaded CC in nearly a month with the exception of killing time for a half hour here or there. i'm not gonna be testing it myself for the number+variable inside a check, unless I know i'll be able to do the OTHER part as well. Also @ duh. I'll take it as a handycap. the few people that check lua will recognize which mod is mine. the few people that check lua already hate me. i'll count it as a negative handycap. I don't care, I at least feel like i'm doing something. Now I *might* have to go file a police report about that bomb that almost killed me. i'm in cleveland, and i'm not even down town... its not even a busy street. what the hell. re-edit, by hate i mean grif mainly, as he's one of the few that i think would vote against it purely for being me. he's the only one that i know of off hand that both checks lua AND would do that. there might be 2 or 3 more, but thats 2 or 3 out of how many votes?
|
Sun Oct 11, 2009 10:02 pm |
|
|
CrazyMLC
Joined: Fri Dec 22, 2006 4:20 am Posts: 4772 Location: Good news everyone!
|
Re: Lua Request- HALP
It would be stupid for it to be automatically 1, the random can be any integer, meaning it could be zero, or -2. Just put a one there. IsPastSimMS is a n argument function, meaning it can, or in some cases, has to have a true or false. DISCLAIMER: from my knowledge obtained from the Wiki.Wiki wrote: function create(self) self.LTimer = Timer(); --this is needed for "Timer:IsPastSimMS" to work. end
function update(self) if Timer:IsPastSimMs(5000) == true then --checks if 5 seconds (5000 milliseconds) have passed self:GibThis(); --if 5 seconds have passed, gib(kill) the object! end end
I said you want semi-colons, its part of the syntax. Also that is perfectly fine, but you need spaces between the + and the other numbers or variables. Code: function Create(self) self.somevariable = math.random(1,15000); self.sometimer = Timer(); end
function Update(self) if self.sometimer:IsPastSimMS(35000 + self.somevariable) == true then self:GibThis(); end end
And, just curious, why would you have died? I don't think I understand the situation.
Last edited by CrazyMLC on Mon Oct 12, 2009 9:58 pm, edited 1 time in total.
|
Mon Oct 12, 2009 12:39 am |
|
|
Duh102
happy carebear mom
Joined: Tue Mar 04, 2008 1:40 am Posts: 7096 Location: b8bbd5
|
Re: Lua Request- HALP
He was walking down the street and walked over the second bomb. EDIT:@MLC CrazyMLC wrote: It would be stupid for it to be automatically 1, the random can be any integer, meaning it could be zero, or -2. Pretty sure it would return a decimal value between 0 and 1. Pretty standard random range. Can give it arguments to change that. CrazyMLC wrote: IsPastSimMS is an argument. It's a function, you need to give it arguments. It can be an argument itself, but it is not by definition an argument. Semi colons do make it nice and readable, and make sure you don't have stray statements. Lastly, you don't really need spaces between math operators. Makes it look nicer though.
Last edited by Duh102 on Mon Oct 12, 2009 1:00 am, edited 1 time in total.
|
Mon Oct 12, 2009 12:53 am |
|
|
CrazyMLC
Joined: Fri Dec 22, 2006 4:20 am Posts: 4772 Location: Good news everyone!
|
Re: Lua Request- HALP
Oh, didn't see the second bomb part. I wonder who the... arsonist?... was.
EDIT: Fine, fine. function. As for math.random, that's good to know!
Last edited by CrazyMLC on Mon Oct 12, 2009 2:35 am, edited 1 time in total.
|
Mon Oct 12, 2009 12:58 am |
|
|
Grif
REAL AMERICAN HERO
Joined: Sat Jan 27, 2007 10:25 pm Posts: 5655
|
Re: Lua Request- HALP
Oh god this thread is a ♥♥♥♥. math.random without arguments gives you a number between 0 and 1, including both with one argument it gets you a number from 1 to that number with two, it's a number between two numbers you can completely have two scripts running on the same object, but there's NO NEED TO because nothing you are going to do requires two scripts. learn to organize code. that said, running two files is as simple as dofile("shittymod.rte/badcode.lua"); Your first script, gravitating to the closest MO, is a comically easy concatenation of two of TLB's scripts: the distance check of the homing missile launcher (which iterates through all actors and returns the closest one) and the gravity functions from Orbit Lands. Second script: Code: function Update(self) self.RotAngle = 0; self.AngVel = 0; end arguments take whatever the hell they take if they want a number, you can give them a number. a variable that represents a number + a number = a number, so it's a valid call a string + a number does not equal a number so for math.random it would not work. but that is not an issue here. oh and by the way Quote: re-edit, by hate i mean grif mainly, as he's one of the few that i think would vote against it purely for being me. he's the only one that i know of off hand that both checks lua AND would do that. there might be 2 or 3 more, but thats 2 or 3 out of how many votes? in order 1. I would not vote against your mod because you made it 1a. I would vote against it because you make shitty mods 2. I do not check lua, pretty much ever, because 90% of the time I can tell how it's done anyways. God knows not your lua, especially. 3. I really don't care which mod you put in because I'm voting for the one that's best. Since that's not going to be yours, by default, I'm not going to vote for you. Your inability to make a single thread without dragging my name into it (seriously, though, chill the ♥♥♥♥ out, I just wrote like sixteen paragraphs helping you out) is unparalleled, but you also forgot that the world doesn't revolve around you and you're really not worth the effort
|
Mon Oct 12, 2009 1:59 am |
|
|
Miles_T3hR4t
Joined: Mon Jun 04, 2007 5:55 am Posts: 1627 Location: Ohio
|
Re: Lua Request- HALP
wow grif, that was amazingly helpful! I'm just a little confused about a few specific bits. from missile.lua, the only things I need are, Code: function Create(self) self.target = nil
function Update(self) if MovableMan:IsActor(self.target) == false then local curdist = 500; 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 and actor.Team ~= self.Team then curdist = dist; self.target = actor; end end if MovableMan:IsActor(self.target) then local targetdir = math.atan2(-(self.target.Pos.Y-self.Pos.Y),(self.target.Pos.X-self.Pos.X)); end
As far as I know this only tells it that if there is an actor that is not on the objects team within X distance then, set a variable (self.target) to that specific actor, and I think TargetDir is a variable, that when called later returns an angle. So... how do I use GravitateMovableObject() with local TargetDir? everything else can just get lumped on after the end statement with another end statement, that i'm find with... this is my main problem now.
|
Mon Oct 12, 2009 3:43 am |
|
|
Grif
REAL AMERICAN HERO
Joined: Sat Jan 27, 2007 10:25 pm Posts: 5655
|
Re: Lua Request- HALP
Post the code for GravitateMovableObject, not sure which arguments it uses.
The key thing with the homing missile code is an understanding of for loops. The for loop will iterate through a table (basically a long list) of every actor on the scene, that is, craft, actors, crabs, etc.
if MovableMan:IsActor(self.target) == false then if the pointer (a variable that references something) self.target does not exist, or more specifically is not an actor, then
local curdist = 500; for actor in MovableMan.Actors do
create a local variable, curdist (current distance). local means it only applies within the last logical statement, in this case the if movableman:isactor statement.
local avgx = actor.Pos.X - self.Pos.X; local avgy = actor.Pos.Y - self.Pos.Y; local dist = math.sqrt(avgx ^ 2 + avgy ^ 2);
Pythagorean theorem to determine distance, in pixels.
if dist < curdist and actor.Team ~= self.Team then curdist = dist; self.target = actor; end
This is the critical part. dist (distance) is the distance between our two MOs (the missile or whatever and the target). If that distance is less than "curdist", then we reset curdist, to the last distance, and change our target. but this is all in a for loop, which iterates through all actors in the scene which means that it checks every single actor for being nearer to the missile than the last one. If one's closer than whatever curdist originally was, that one becomes the target. But if there's another actor closer than THAT distance, that one becomes the target.
It's a pretty brilliant little snippet and I've gotta hand it to TLB.
As for targetdir, that's a number variable.
Math.atan2 (arc tangent, aka tan^-1), when given two vector coordinates, returns the angle between the two.
That is really astoundingly useful.
From what I remember of gravitatemovableobject, it's probably going to take dist and angle. Dist is whatever gravity coefficient is going to be, probably a factor of distance to target. Angle is that targetdir.
|
Mon Oct 12, 2009 5:06 am |
|
|
Geti
Joined: Sun Jul 13, 2008 9:57 am Posts: 4886 Location: some compy
|
Re: Lua Request- HALP
Grif wrote: Oh god this thread is a ♥♥♥♥. could not be truer. that said: Grif wrote: Pythagorean theorem to determine distance, in pixels. it may be prudent to point out that using SceneMan:ShortestDistance(point,point) or whatever its called is faster and neater, but it doesnt really matter. CrazyMLC wrote: Fine, fine. function. you're still wrong with the rest of what you said.. you do not need an "== true" to check for trueness, if there is no equality expressed the interpreter checks for trueness, meaning you can use things like "if MovableMan:IsActor(thing) then" and it will work fine. code to the gravity function would be helpful, due to laziness.
|
Mon Oct 12, 2009 10:04 am |
|
|
Grif
REAL AMERICAN HERO
Joined: Sat Jan 27, 2007 10:25 pm Posts: 5655
|
Re: Lua Request- HALP
Geti wrote: you're still wrong with the rest of what you said.. you do not need an "== true" to check for trueness, if there is no equality expressed the interpreter checks for trueness, meaning you can use things like "if MovableMan:IsActor(thing) then" and it will work fine. It's not necessary but it's actually better syntax. Functions don't always have clear returns; in the case of IsActor or IsPastSimMS it makes sense, but making sure you know what you're checking for is good form overall. Also, I've had times where I swear to god timers weren't working without an == true on the end.
|
Mon Oct 12, 2009 3:06 pm |
|
|
Miles_T3hR4t
Joined: Mon Jun 04, 2007 5:55 am Posts: 1627 Location: Ohio
|
Re: Lua Request- HALP
okay, so here's the Orbit.lua, with all comments. Note, Huge code, would be nice to have spoiler tags for code Code: ----------------------------------------------------------------------------------------- -- Start Activity -----------------------------------------------------------------------------------------
function Orbit:StartActivity() print("START! -- Orbit:StartActivity()!"); --These 4 seem to be in every mission. Not sure what they do, but I don't want to mess with them just in case they're important. self.FightStage = { NOFIGHT = 0, DEFENDING = 1, ATTACK = 2, FIGHTSTAGECOUNT = 3 }; self.AreaTimer = Timer(); self.StepTimer = Timer(); self.SpawnTimer = Timer(); --If the game is over yet. self.gameover = 0; --Give the player more money. ActivityMan:GetActivity():SetTeamFunds(1000000,0); --Make the player. Make it the brain. playerActor = CreateActor("Brain Case"); playerActor.Pos = Vector(1500,1500); playerActor.Team = 0; MovableMan:AddActor(playerActor); self:SetPlayerBrain(playerActor, 0); self:SetLandingZone(playerActor.Pos, 0); self:SwitchToActor(playerActor, 0, self:GetTeamOfPlayer(0)); --The gravity field. self.gravfield = SceneMan.Scene:GetArea("Gravity Field"); --The max gravity acceleration in pixels per frame. gravity = 0.25; --The max gravity acceleration toward the moon in pixels per frame. moongrav = 0.2; --The max gravity acceleration within the gravity field in pixels per frame. normgrav = 0.35; --Make the moon, Roflamaos. moon = CreateMOSRotating("Roflmaos"); moon.Pos = Vector(1500,200); moon.Vel = Vector(25,0); MovableMan:AddParticle(moon); end
----------------------------------------------------------------------------------------- -- Trigonometry -----------------------------------------------------------------------------------------
--Return the direction (in degrees) to a point. function point_direction(x1,y1,x2,y2) return math.deg(math.atan2((y1-y2),(x1-x2))); end
--Return the distance to a point. function point_distance(x1,y1,x2,y2) return (math.sqrt(math.pow(x2-x1,2)+math.pow(y2-y1,2))); end
--Return the x coordinate of a point dist pixels away in direction ang. function lengthdir_x(dist,ang) return dist * math.cos(ang*math.pi/180); end
--Return the y coordinate of a point dist pixels away in direction ang. function lengthdir_y(dist,ang) return dist * math.sin(ang*math.pi/180); end
----------------------------------------------------------------------------------------- -- Gravity stuff -----------------------------------------------------------------------------------------
function GravitateMovableObject(object,pointx,pointy,grav,rotate,maxrange) local dir = point_direction(object.Pos.X,object.Pos.Y,pointx,pointy);
if rotate == 1 then object.RotAngle = -dir; end
local mod = (-point_distance(object.Pos.X,object.Pos.Y,pointx,pointy)/maxrange)+1; if mod < 0 then mod = 0; end
object.Vel = Vector(object.Vel.X - lengthdir_x(grav*mod,dir),object.Vel.Y - lengthdir_y(grav*mod,dir)); end
----------------------------------------------------------------------------------------- -- Pause Activity -----------------------------------------------------------------------------------------
function Orbit:PauseActivity(pause) print("PAUSE! -- Orbit:PauseActivity()!"); end
----------------------------------------------------------------------------------------- -- End Activity -----------------------------------------------------------------------------------------
function Orbit:EndActivity() print("END! -- Orbit:EndActivity()!"); end
---------------------------------------------------------------------------------------------------------- -- Update Activity ----------------------------------------------------------------------------------------------------------
function Orbit:UpdateActivity() for actor in MovableMan.Actors do if actor.PinStrength <= 0 then if not self.gravfield:IsInside(actor.Pos) then GravitateMovableObject(actor,1500,1500,gravity,0,3000); if MovableMan:IsParticle(moon) == true then GravitateMovableObject(actor,moon.Pos.X,moon.Pos.Y,moongrav,0,375); end else actor.Vel = Vector(actor.Vel.X,actor.Vel.Y + normgrav); end end end for item in MovableMan.Items do if item.PinStrength <= 0 then if not self.gravfield:IsInside(item.Pos) then GravitateMovableObject(item,1500,1500,gravity,0,3000); if MovableMan:IsParticle(moon) == true then GravitateMovableObject(item,moon.Pos.X,moon.Pos.Y,moongrav,0,375); end else item.Vel = Vector(item.Vel.X,item.Vel.Y + normgrav); end end end for particle in MovableMan.Particles do if particle.PinStrength <= 0 then if not self.gravfield:IsInside(particle.Pos) then GravitateMovableObject(particle,1500,1500,gravity,0,3000); if MovableMan:IsParticle(moon) == true then if not particle.ID == moon.ID then GravitateMovableObject(particle,moon.Pos.X,moon.Pos.Y,moongrav,0,375); end end else particle.Vel = Vector(particle.Vel.X,particle.Vel.Y + normgrav); end end end end Unfortunately this is useless to me, because I can't tell what to take out and what is needed and what isn't. For instance, self.gravfield = SceneMan.Scene:GetArea("Gravity Field"); that is scene specific, and requires an area, not a point. Is that the area of what is affected by the gravity, is that where the gravity comes from, what is it? another part is this, function GravitateMovableObject(object,pointx,pointy,grav,rotate,maxrange) . its a function but its not function:create or function:update. theres 5 function calls, and I only know how to use 2 of them, and to top it all off, they all have different names and I currently don't understand how I can and can't change them. is that scene specific, mission specific. Its scene lua, not object lua, so I'm guessing the function calls work differently. Point I'm making is, I don't know what I need and what I don't out of this. Also, Geti wrote: Grif wrote: Pythagorean theorem to determine distance, in pixels. it may be prudent to point out that using SceneMan:ShortestDistance(point,point) or whatever its called is faster and neater, but it doesnt really matter. I'm probably gonna have +50-100 objects running this script. optimization for the fastest possible speed is important. so how would I use Sceneman:shortestDistance(x,y)? I'd need shortestdistance(x,y) to be returned as 2 variables for use with getting its coordinates, from what I know that only returns what the actor is, Not its location.
|
Mon Oct 12, 2009 5:16 pm |
|
|
piipu
Joined: Mon Jun 30, 2008 9:13 pm Posts: 499 Location: Finland
|
Re: Lua Request- HALP
You should not go guessing things like that. Anyways, for the script: Code: function Update(self) if not MovableMan:IsActor(self.target) then local dist = 600 for actor in MovableMan.Actors do local distvec = SceneMan:ShortestDistance(self.Pos , actor.Pos , true) if distvec.Magnitude < dist then dist = distvec.Magnitude self.target = actor end end else local distvec(self.Pos , self.target.Pos , true) --if the objects seem to be gravitating away from each other, swap self.Pos and self.target.Pos self.Vel = self.Vel + distvec / distvec.Magnitude ^ 2 * 100 --you can increase gravity by increasing the 100 here. end end
|
Mon Oct 12, 2009 7:24 pm |
|
|
CrazyMLC
Joined: Fri Dec 22, 2006 4:20 am Posts: 4772 Location: Good news everyone!
|
Re: Lua Request- HALP
Geti wrote: CrazyMLC wrote: Fine, fine. function. you're still wrong with the rest of what you said.. you do not need an "== true" to check for trueness, if there is no equality expressed the interpreter checks for trueness, meaning you can use things like "if MovableMan:IsActor(thing) then" and it will work fine. I was referring to that particular function needing an argument, according to the Wiki. It seems you didn't read my post saying why I think that. You also didn't see the disclaimer. Or the link. Or the quote from the WikiIf you think you're correct then change the Wiki, because then it is wrong.
|
Mon Oct 12, 2009 9:48 pm |
|
|
|
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
|
|