View unanswered posts | View active topics It is currently Sat Dec 28, 2024 1:32 am



Reply to topic  [ 9 posts ] 
 How should I make a script only run once? 
Author Message
User avatar

Joined: Sun Dec 25, 2011 7:23 am
Posts: 269
Reply with quote
Post How should I make a script only run once?
Most, or pretty much all Lua coding I've seen so far follows the basic format of

Code:
function Create(self)
--Defining of some values
end

function Update(self)
--Defining some parameters and what to do if those parameters are met (if... then...) and checking them over and over till the end
end

function Update(self) checks the game for every single interval of time until whatever the script is attached to disappears.
But I was wondering about how the script should be written so that it only acts once before self terminating, so as to save up processing power and make stuff more streamlined.


Sun Jan 08, 2012 2:50 pm
Profile

Joined: Sat Oct 22, 2011 8:07 pm
Posts: 149
Reply with quote
Post Re: How should I make a script only run once?
If you want everything to be done only once, you could put all of it into the Create function.


Sun Jan 08, 2012 2:54 pm
Profile
User avatar

Joined: Fri Feb 16, 2007 8:43 pm
Posts: 1695
Location: AH SHIT FUCK AUGH
Reply with quote
Post Re: How should I make a script only run once?
Code:
if self.darpdarp == 0 then
   do stuff
   self.darpdarp = 1
end

Should work. Just make sure the variable of your choosing is set to 0 to begin with. If you want it to be exactly timed, you could go for something like:
Code:
if self.darpdarp == 0 and self.Age > 2000 then   --Age in milliseconds obviously
   do stuff
   self.darpdarp = 1
end

Mind you, "darpdarp" is a completely arbitrary variable name. :U


Sun Jan 08, 2012 2:58 pm
Profile WWW
User avatar

Joined: Sun Dec 25, 2011 7:23 am
Posts: 269
Reply with quote
Post Re: How should I make a script only run once?
Quote:
self.darpdarp

I assume I should put this in the create function area?


Mon Jan 09, 2012 1:04 pm
Profile
User avatar

Joined: Fri Feb 16, 2007 8:43 pm
Posts: 1695
Location: AH SHIT FUCK AUGH
Reply with quote
Post Re: How should I make a script only run once?
Yes. I should start speaking more clearly, silly me. :U
So yeah, to clarify myself a little more:
Code:
function Create(self)
   --Your other variables go here
   self.darpdarp = 0
end

function Update(self)
   if self.darpdarp == 0 and self.Age > 2000 then --Doesn't have to be 2000
      --Do stuff here
      self.darpdarp = 1
   end
end


Mon Jan 09, 2012 1:29 pm
Profile WWW
User avatar

Joined: Tue Jun 12, 2007 11:52 pm
Posts: 13144
Location: Here
Reply with quote
Post Re: How should I make a script only run once?
Well, Shook's example script will still loop. It just won't do anything while it loops (except checking if self.darpdarp == 0). Since the Create() function only runs once, you should put your single-run stuff into there instead of putting it into Update() (the Update() function loops while the object is in the simulation).


Mon Jan 09, 2012 11:03 pm
Profile
User avatar

Joined: Sun Dec 25, 2011 7:23 am
Posts: 269
Reply with quote
Post Re: How should I make a script only run once?
Figured it out and I found that the resulting code was surprisingly simple
Code:
function Create(self)
self.dundun = do stuff
end


Tue Jan 10, 2012 4:12 am
Profile

Joined: Sat Oct 22, 2011 8:07 pm
Posts: 149
Reply with quote
Post Re: How should I make a script only run once?
ryry1237 wrote:
Figured it out and I found that the resulting code was surprisingly simple
Code:
function Create(self)
self.dundun = do stuff
end

The Chairman wrote:
If you want everything to be done only once, you could put all of it into the Create function.


Thu Jan 12, 2012 5:20 pm
Profile
User avatar

Joined: Sun Dec 25, 2011 7:23 am
Posts: 269
Reply with quote
Post Re: How should I make a script only run once?
Indeed


Fri Jan 13, 2012 7:31 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.038s | 13 Queries | GZIP : Off ]