Data Realms Fan Forums
http://45.55.195.193/

Array problems
http://45.55.195.193/viewtopic.php?f=73&t=19129
Page 1 of 1

Author:  Awesomeness [ Mon Jun 28, 2010 9:20 pm ]
Post subject:  Array problems

I have a parasite I'm making. When it gets too close to other non-parasites, it infects them. They take damage every turn until they die and create a new parasite. I made a list of currently infected actors so that I wouldn't infect people multiple times. The problem was, if I didn't do that, people, because they got infected once every frame, died ridiculously fast and made a crabsplosion of parasites when they did. It's interesting, because if you infect one of the crabs at the crab level, the infection spreads across the map like a tidal wave and kills everything.

Anyways...

It works fine so far, however, I keep having problems getting rid of people from the array. I want to get rid of dead people because otherwise the array would get larger and larger, and if many people get infected, could use a lot of memory. I keep getting an error in DeleteSelf() (at the bottom) at line 48:
Code:
function Create(self)
   --Find who to hurt by taking our mass, because we stored the actor ID in the mass when the particle was made.
   self.victim = MovableMan:GetMOFromID(self.Mass);

   --Determine the type of actor so we can access its info.
   if self.victim.ClassName == "AHuman" then
      self.victim = ToAHuman(self.victim);
   elseif self.victim.ClassName == "ACrab" then
      self.victim = ToACrab(self.victim);
   else
      --If it is neither quadroped nor biped, erase this as it is not the type of actor we can hurt.
      DeleteSelf();
   end

   self.hurtInterval = 60;

   --Timer to wait out intervals.
   self.hurtTimer = Timer();

end



function Update(self)
   if MovableMan:IsActor(self.victim) then
      --If the actor's dead, gib it and spawn another parasite.
      if self.victim.Health <= 0 then
         local newParasite = CreateACrab("Parasite");
         newParasite.Pos = self.victim.Pos;
         self.victim:GibThis();
         MovableMan:AddParticle(newParasite);
         DeleteSelf();
      else
         --If the actor still exists and the timer is up, hurt it.
         if self.hurtTimer:IsPastSimMS(self.hurtInterval) then
            self.victim.Health = self.victim.Health - 1;
            self.hurtTimer:Reset();
         end
      end
   else
      --If the actor is gone or dead, destroy this.
      DeleteSelf();
   end
end

function DeleteSelf()
   for i, v in ipairs (infected) do
      if v == self.victim then       --LINE 48
         table.remove(infected, i);
         self.ToDelete = true;
      end
   end
end


It says I attempted to index the global "self", a nil value when the infected actor dies. Over and over. I don't understand what that means.

Author:  Petethegoat [ Mon Jun 28, 2010 9:26 pm ]
Post subject:  Re: Array problems

function DestroySelf() can't be helping.
Self shouldn't be capitalised, ever.
DeleteSelf()? You want self.ToDelete = 1, unless DeleteSelf() is something you've defined.

Edit:
Okay, I just saw that you defined DeleteSelf. At the bottom is unlikely to be the ideal place for it, but I'm not very knowledgable about diy functions in Lua.

Author:  Grif [ Mon Jun 28, 2010 11:28 pm ]
Post subject:  Re: Array problems

lua isn't parsed in read order like .ini code is, it doesn't matter where in the file a function is, so long as it's read before the actual script is executed (which it's guaranteed to be)

anyways, your problem is that you're trying to call 'self.ToDelete' inside of a function, which isn't allowed to happen.

The easy workaround: add 'self' as an argument to your defined function, and then call the function as DeleteSelf(self).

It's probably not optimal from a memory usage standpoint, but I do that all the time, and it's guaranteed to work, at least insofar as what you want it to do.

Author:  Awesomeness [ Tue Jun 29, 2010 12:32 am ]
Post subject:  Re: Array problems

Okay, I tried what you said; I get the same stuff except with local instead of global.

Author:  ScifiSpirit [ Tue Jun 29, 2010 12:49 am ]
Post subject:  Re: Array problems

Did you add it to all lines where you call the function, not just function definition?

Example:

function MyFunc(self)
print(self.SomeValue);
end

MyFunc(self);

If you leave it out of the calls, meaning, if you call it like this:
MyFunc();

then error "attempt to index local 'self' (a nil value)" is exactly what you get, because your function definition has defined a local parameter, but you haven't supplied any arguments for the function. Which means that the parameter will be "nil".

You need to do all this because otherwise the function can't see "self" that you want it to see. It can only see global variables, and "self" in this case isn't global.

Author:  Awesomeness [ Tue Jun 29, 2010 12:56 am ]
Post subject:  Re: Array problems

Lol, I feel so stupid...

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