View unanswered posts | View active topics It is currently Fri Dec 27, 2024 8:23 am



Reply to topic  [ 6 posts ] 
 Array problems 
Author Message
User avatar

Joined: Sat Jun 19, 2010 5:02 pm
Posts: 331
Location: Mekkan
Reply with quote
Post 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.


Mon Jun 28, 2010 9:20 pm
Profile
User avatar

Joined: Mon Jun 15, 2009 4:02 pm
Posts: 905
Reply with quote
Post 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.


Mon Jun 28, 2010 9:26 pm
Profile WWW
REAL AMERICAN HERO
User avatar

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
Reply with quote
Post 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.


Mon Jun 28, 2010 11:28 pm
Profile
User avatar

Joined: Sat Jun 19, 2010 5:02 pm
Posts: 331
Location: Mekkan
Reply with quote
Post Re: Array problems
Okay, I tried what you said; I get the same stuff except with local instead of global.


Tue Jun 29, 2010 12:32 am
Profile

Joined: Sat Jun 16, 2007 8:04 pm
Posts: 42
Reply with quote
Post 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.


Tue Jun 29, 2010 12:49 am
Profile
User avatar

Joined: Sat Jun 19, 2010 5:02 pm
Posts: 331
Location: Mekkan
Reply with quote
Post Re: Array problems
Lol, I feel so stupid...


Tue Jun 29, 2010 12:56 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.051s | 13 Queries | GZIP : Off ]