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
|
  |
| 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 |
|
|
|
| 18 Jan 2015 03:45 PM |
| what is the point of that even lol |
|
|
| Report Abuse |
|
|
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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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 |
|
|
|
| 18 Jan 2015 04:33 PM |
| Have no clue what you guys are talking about |
|
|
| Report Abuse |
|
|
Goulstem
|
  |
| Joined: 04 Jul 2012 |
| Total Posts: 7177 |
|
| |
|
|
| 18 Jan 2015 04:37 PM |
| I was gonna join your group to learn how to script. Do you teach? |
|
|
| Report Abuse |
|
|
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 |
|
|
|
| 18 Jan 2015 04:41 PM |
| I want to learn to script. |
|
|
| Report Abuse |
|
|