generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Preventing Stack Overflow?

Previous Thread :: Next Thread 
Goulstem is not online. Goulstem
Joined: 04 Jul 2012
Total Posts: 7177
18 Jan 2015 03:42 PM
So i'm making a script where 'repicking' occurs, meaning I have to call the function inside itself. Everything works fine but the problem is that if the function has to repick too many times then it causes a stack overflow. So how do I prevent this, because I know eventually it will come up with a result.

The code is along these lines;

function choose()
local num = math.random(0,100)
if num > 10 then
choose()
else
return num;
end

choose();
--if it's above ten too many times it causes a stack overflow, but there is a definite possibility that it could eventually be 10.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
18 Jan 2015 03:44 PM
Why not just return math.random(0, 10)?
Anyways, to prevent this use a loop instead but then you'll get a possibility of a hang which should eventually return a number less than 10.

function choose()
local num = math.random(0, 100);
while num > 10 do num = math.random(0, 100); end
return num
end
Report Abuse
QuintinityZero is not online. QuintinityZero
Joined: 01 Oct 2008
Total Posts: 10618
18 Jan 2015 03:45 PM
what is the point of that even lol
Report Abuse
Goulstem is not online. Goulstem
Joined: 04 Jul 2012
Total Posts: 7177
18 Jan 2015 03:47 PM
The sample code isn't anything like what i'm using this for, it was just a sample. I'm making a object spawner(like apocalypse rising) so that objects will spawn inside specified areas and not be too close to eachother and there won't be like a bajillion objects in workspace.

Thanks cnt i'll try that.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
18 Jan 2015 03:50 PM
Goul for that spawn thing, you don't need to use a lot of recursion.
Just insert the possible points (doing some math to "randomly" select the points from a given position and some radius) into a table and remove them each time?
Report Abuse
Goulstem is not online. Goulstem
Joined: 04 Jul 2012
Total Posts: 7177
18 Jan 2015 03:53 PM
That moment when you forget to put a wait() in your while loop.

Ugh.
Report Abuse
Goulstem is not online. Goulstem
Joined: 04 Jul 2012
Total Posts: 7177
18 Jan 2015 03:57 PM
No need, it works now.

To see how i'm doing this, http://www.roblox.com/Object-Spawning-Help-place?id=196617017

Any pointers on efficiency would be nice too.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
18 Jan 2015 04:01 PM
It seems like you are relying on random too much which is very inefficient.
See if you can rely on something else, maybe put all the available spawns in a table or whatever.
Report Abuse
Goulstem is not online. Goulstem
Joined: 04 Jul 2012
Total Posts: 7177
18 Jan 2015 04:04 PM
But then wouldn't I just need to use randomized methods to get a spawn out of the table again? Lol.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
18 Jan 2015 04:06 PM
Well yes but you are not relying on it since if you remove it from the table, you know it will never be used again.
Report Abuse
Goulstem is not online. Goulstem
Joined: 04 Jul 2012
Total Posts: 7177
18 Jan 2015 04:12 PM
But I want it to be used again :c
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
18 Jan 2015 04:13 PM
Then clone the table that you plan on removing the positions from :)
Either way it'll be far more reliable then relying on math.random
Report Abuse
ATLASGideon is not online. ATLASGideon
Joined: 07 Aug 2013
Total Posts: 3885
18 Jan 2015 04:33 PM
Have no clue what you guys are talking about
Report Abuse
Goulstem is not online. Goulstem
Joined: 04 Jul 2012
Total Posts: 7177
18 Jan 2015 04:34 PM
^Then don't post
Report Abuse
ATLASGideon is not online. ATLASGideon
Joined: 07 Aug 2013
Total Posts: 3885
18 Jan 2015 04:37 PM
I was gonna join your group to learn how to script. Do you teach?
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
18 Jan 2015 04:39 PM
old post
-------------------------------------------
bumping to see where this goes...
-------------------------------------------
new post
Report Abuse
ATLASGideon is not online. ATLASGideon
Joined: 07 Aug 2013
Total Posts: 3885
18 Jan 2015 04:41 PM
I want to learn to script.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image