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



Reply to topic  [ 5 posts ] 
 Script does something twice it should do once 
Author Message
User avatar

Joined: Sat Jun 19, 2010 5:02 pm
Posts: 331
Location: Mekkan
Reply with quote
Post Script does something twice it should do once
My parasite script infects people twice when it should only once. Once the first frame, then the frame after that. I don't see why. I've been looking and looking... I know this is going to be stupidly obvious again, but oh well.
Code:
function Create(self)
   self.killdist = 50;
   if infected == nil then
      infected = {};
   end
end

function Update(self)

   --Find everyone close enough to infect that isn't a parasite.
   for actor in MovableMan.Actors do
   
      local curdist;
      curdist = math.sqrt(math.pow(self.Pos.X - actor.Pos.X,2) + math.pow(self.Pos.Y - actor.Pos.Y,2));
      
      if curdist <= self.killdist and actor:IsInGroup("Parasites") == false and (actor.ClassName == "AHuman" or actor.ClassName == "ACrab") then
      
         --Make sure the actor isn't already infected first.
         local actorInfected = false;
         for i, v in ipairs(infected) do
            if v == actor then
               actorInfected = true;
            end
         end
         if actorInfected == false then
            local wound = CreateMOSRotating("Infection");
               --The mass will be unused for the other object, so store the actor's ID as that.
               wound.Mass = actor.ID;
               MovableMan:AddParticle(wound);
            table.insert(infected, 0, actor);
         end
      end
   end
end
While I'm at it, does anyone know why every frame when I'm within infecting range of another actor, it says "ERROR- No such operator defined"? I Googled it to no avail.

Also, is there any way I can pass down the parent parasite's team to the wound that eventually creates a new parasite? I managed to do it for the actor to infect's ID using the mass, but I can't find any other variables to pass down more data with. I could make it always set the team to my team, but what if I wanted to make the CPU team or the neutral team have parasites? I would want it to work. I suppose I could add 0, 1, or 2 onto the mass number, with 0 and 1 being their respective teams and 2 being the neutral team, negative one, but all the string parsing would take some work, and I'm looking for alternatives.


Tue Jun 29, 2010 7:58 pm
Profile
User avatar

Joined: Tue Jun 29, 2010 8:17 pm
Posts: 1
Reply with quote
Post Re: Script does something twice it should do once
Quote:
local curdist;
curdist = math.sqrt(math.pow(self.Pos.X - actor.Pos.X,2) + math.pow(self.Pos.Y - actor.Pos.Y,2));


why do you do that

why not local curdist = maths

that's the first thing that is most obviously wrong to me

also what do you mean "infects people twice", are there two actors in the infection table?

and why are you inserting at position 0(or a specific position at all)


Tue Jun 29, 2010 8:58 pm
Profile
User avatar

Joined: Sat Jun 19, 2010 5:02 pm
Posts: 331
Location: Mekkan
Reply with quote
Post Re: Script does something twice it should do once
Konata wrote:
Quote:
local curdist;
curdist = math.sqrt(math.pow(self.Pos.X - actor.Pos.X,2) + math.pow(self.Pos.Y - actor.Pos.Y,2));


why do you do that

why not local curdist = maths

that's the first thing that is most obviously wrong to me

What's the difference?

Konata wrote:
also what do you mean "infects people twice", are there two actors in the infection table?

Periodically they get hurt in two quick consecutive frames. Two new parasites also spawn upon death rather than one. I know my infection code is correct.

Konata wrote:
and why are you inserting at position 0(or a specific position at all)

Because I don't want to have to find of the list and insert it at the end.


Wed Jun 30, 2010 12:21 am
Profile

Joined: Sat Jun 16, 2007 8:04 pm
Posts: 42
Reply with quote
Post Re: Script does something twice it should do once
I don't see anything obviously wrong with the script. Maybe whatever the script is attached to, is created twice and something goes wrong while testing if the actor has already been infected.
"No such operator defined" error probably refers to problem with userdata objects (no operator defined for the userdata to do some action, could be printing or equality testing or something else), and I guess no line numbers either in the error? In that case it's a bit harder to figure out and requires testing.
I'm not sure if it's ok to test it with objects directly like "if actor == actor". I've run into problems before unless I've used the member variables of the object. Using actor ID's could work better. I'm just guessing, but that might be the problem.

Use the basic debugging method by removing code until the error is gone and then add it back until you've located the problem or upload the whole mod here for somebody else to figure it out ;)

I can only think of 2 ways to pass information between scripts other than the game property variables: (btw, did you try wound.Team?)

1. If setting script2.MyVariable = x, and then inside script2 reading it by self.MyVariable doesn't work...
2. Use global tables like you used the infected table. Script 1 creates table if it doesn't exist, and writes it's data to it, script 2 reads the data from the table and removes the entry, and if the table then is empty, sets the reference to the table to nil to mark the table's memory to be released. Make sure you read the right data from right script when there are multiple scripts sending data to other scripts at the same time. You also have to avoid collisions with global variables of other mods.

This is a bit complicated though and might result in a lot of unnecessary memory allocation and deallocation, let alone numerous checks in the numerous scripts running. And if it's not done, the memory won't be released until CC is closed down.
It might be better to use some global manager particle script that manages the table for all scripts and perhaps even the scripts themselves could run in the manager script, all combined, so you wouldn't need to worry about passing information between scripts then or use global variables at all. Best option in my opinion in that case, when you really don't have much choice.

Awesomeness wrote:
What's the difference?

There isn't any difference in program logic in this case.


Thu Jul 01, 2010 6:48 am
Profile
User avatar

Joined: Tue Nov 06, 2007 6:58 am
Posts: 2054
Reply with quote
Post Re: Script does something twice it should do once
ScifiSpirit wrote:
"No such operator defined" error probably refers to problem with userdata objects (no operator defined for the userdata to do some action, could be printing or equality testing or something else), and I guess no line numbers either in the error? In that case it's a bit harder to figure out and requires testing.
I'm not sure if it's ok to test it with objects directly like "if actor == actor". I've run into problems before unless I've used the member variables of the object. Using actor ID's could work better. I'm just guessing, but that might be the problem.

Correct.


Fri Jul 16, 2010 5:11 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.043s | 13 Queries | GZIP : Off ]