Author Topic: RemoveSeedlings error circunventions  (Read 5713 times)

elideb

  • Seed
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
RemoveSeedlings error circunventions
« on: May 17, 2011, 07:51:00 AM »
Hi, I recently bought Eufloria, just to make some levels and learn a bit of Lua.

In my first complex level I have stumbled onto the RemoveSeedlings bug and it is driving me crazy. Does anyone know of some way to achieve the same behaviour while avoiding the crash?

So far I've tested things like

Code: [Select]
local i = 0
for i = 0, numToRemove do
  asteroid:RemoveSeedlings (faction, 1)
end

but it still crashes in the loop, if numToRemove is big enough.

What I am trying to do is change some seedling's faction, so I remove them from the planet and add them with the new faction and stats. If there is another way to do that, I'll be pleased as peaches =)

Thanks!

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: RemoveSeedlings error circunventions
« Reply #1 on: May 17, 2011, 08:07:54 AM »
RemoveSeedlings() crashes, I gave up at once I figurd it out...

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: RemoveSeedlings error circunventions
« Reply #2 on: May 20, 2011, 09:24:41 PM »
Hi, welcome.  :>


Is it the case that the for loop crashes because it's trying to remove too many seedlings?

Ie it loops 10 times, removing one seedling each time, but there were only 5 seedlings to begin with so it crashes when it tries to remove the 6th?



If that's the case, try something like this:


Code: [Select]
local i = 0
for i = 0, numToRemove do
if asteroid:GetNumSeedlings(1) ~= 0 then
asteroid:RemoveSeedlings(1, 1)
end
end


You're saying "ok Mr Loop, I only want you to remove seedlings IF the number of Player 1 seedlings present at the asteroid is not 0."



So in the example above, after all 5 seedlings have been removed, the number of seedlings would be 0, so the remove command would not be triggered again.  The rest of the loop iterations would finish harmlessly.

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: RemoveSeedlings error circunventions
« Reply #3 on: May 21, 2011, 12:03:52 AM »
yuo don't need to the line local i = 0.  I think.

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: RemoveSeedlings error circunventions
« Reply #4 on: May 21, 2011, 12:07:45 AM »
It's not needed, but making your variables local can speed things up.  It's just a performance thing though.

elideb

  • Seed
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
Re: RemoveSeedlings error circunventions
« Reply #5 on: May 21, 2011, 05:25:05 AM »
RemoveSeedlings() crashes, I gave up at once I figurd it out...

I'm about to, as well. I'm rethinking my level so that it does not depend on rebelling troops...

Hi, welcome.  :>


Is it the case that the for loop crashes because it's trying to remove too many seedlings?

As a matter of fact, removing seedlings from an empty asteroid has never crashed in my tests =)

I'm afraid that's not the problem. In my level I remove a percentage of the asteroid's population belonging to a given faction. And in my tests I verified that I did not remove more seedlings than available in the asteroid.
The closest I've got to pinpointing the crash is that it occurrs when a sufficiently high percentage (30%-50%) of the existing population is removed. So, removing 1 from a population of 20 is alright, but removing one out of 4 is (usually) not.

Thanks for trying!

yuo don't need to the line local i = 0.  I think.

I use the local so that it does not overwrite any global variable named the same declared somewhere else. It's a good practice when using names that typical in languages defaulting variables to global.

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: RemoveSeedlings error circunventions
« Reply #6 on: May 21, 2011, 05:47:34 PM »
Oh, okay.

Would it be okay to remove the same percentage of the seedlings from an increaingly smaller number of them? So like:

N = Number of seedlings to leave remaining
X = Asteroid ID
E = Empire number
math.huge is the Lua equvalent of +- infinite

for i=0, math.huge()
  if GetAsteroid(X):GetNumSeedlings(E) >= N
    GetAsteroid(X):RemoveSeedlings(math.floor(GetAsteroid(X):GetNumSeedlings(E)*SafeProportion))
  else
    break
  end
end


I think that's all usable, but I haven't Lua'd for a while. You could probably do it in a while loop, but I can't be bothered to rewrite it.