Data Realms Fan Forums
http://45.55.195.193/

How should I make a script only run once?
http://45.55.195.193/viewtopic.php?f=1&t=29142
Page 1 of 1

Author:  ryry1237 [ Sun Jan 08, 2012 2:50 pm ]
Post subject:  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.

Author:  The Chairman [ Sun Jan 08, 2012 2:54 pm ]
Post subject:  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.

Author:  Shook [ Sun Jan 08, 2012 2:58 pm ]
Post subject:  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

Author:  ryry1237 [ Mon Jan 09, 2012 1:04 pm ]
Post subject:  Re: How should I make a script only run once?

Quote:
self.darpdarp

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

Author:  Shook [ Mon Jan 09, 2012 1:29 pm ]
Post subject:  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

Author:  CaveCricket48 [ Mon Jan 09, 2012 11:03 pm ]
Post subject:  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).

Author:  ryry1237 [ Tue Jan 10, 2012 4:12 am ]
Post subject:  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

Author:  The Chairman [ Thu Jan 12, 2012 5:20 pm ]
Post subject:  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.

Author:  ryry1237 [ Fri Jan 13, 2012 7:31 am ]
Post subject:  Re: How should I make a script only run once?

Indeed

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/