Project X Forums
http://px.worms2d.info/forum/

Looping through all worms each frame - I'm doing it wrong?
http://px.worms2d.info/forum/viewtopic.php?f=15&t=363
Page 1 of 1

Author:  Pac-Man [ Sun Oct 09, 2011 3:34 pm ]
Post subject:  Looping through all worms each frame - I'm doing it wrong?

Hi there,

I wanna loop through all worms each frame, checking if they are too far up in the sky and changing their speed to point downwards if that is the case.

However, when checking this script, the compiler can't find the PosY field of foundObj.
Do I have to cast a CWorm out of the found object? I thought this is not needed because there are virtually no "types" in EAX?
Code:
require utils;
CAntiPatience : CObject;

void CAntiPatience::Message(CObject* sender, EMType type, int size, CMessageData* data)
{
    // Calculations each frame
    if (type == M_FRAME)
    {
        // Iterate through all objects
        for (local i = 1; i < Env->Objs.Count; i++)
        {
            // Check if the found object is a worm
            local foundObj = Env->Objs.GetObject(i);
            if (foundObj == CWorm)
            {
                // Check the coordinates
                if (foundObj->PosY > 300) // Problem here: Doesn't find PosY-field
                {
                    foundObj->SpY = -200;
                }
            }
        }
    }
}


Would be nice if you could fix my head :P

Greetings,
Pac-Man

Author:  Entuser [ Mon Oct 10, 2011 3:18 pm ]
Post subject:  Re: Looping through all worms each frame - I'm doing it wron

There are types, you don't have to declare them, but you still have to do typecasting.

Code:
require utils;
CAntiPatience : CObject;

void CAntiPatience::Message(CObject* sender, EMType type, int size, CMessageData* data)
{
    // Calculations each frame
    if (type == M_FRAME)
    {
        // Iterate through all objects
        for (local i = 1; i < Env->Objs.Count; i++)
        {
            // Check if the found object is a worm
            local foundObj = CGObject(Env->Objs.Objs[i]));
            if (foundObj is CWorm)
            {
                // Check the coordinates
                if (foundObj->PosY > 300) // Problem here: Doesn't find PosY-field
                {
                    foundObj->SpY = -200;
                }
            }
        }
    }
}


I recommend you to learn from existing scripts. Also, that Message would be never get executed, because CAntiPatience is never instanced. You can override CTurnGame::Message instead, or make an instance of CAntiPatience at first frame.

Author:  Pac-Man [ Sat Oct 15, 2011 11:21 pm ]
Post subject:  Re: Looping through all worms each frame - I'm doing it wron

Thanks for the answer. I now override the Message function, but the game now totally locks up at the loading screen (and the chat becomes visible). I could even put a return as the first command inside the override function but it always locks up.
Code:
require utils;

int nextCheck;

// Event each frame
override void CTurnGame::Message(CObject* sender, EMType type, int size, CMessageData* data)
{
    nextCheck--;
    if (nextCheck > 0)
    {
        nextCheck = 30;
        // Calculations each frame
        if (type == M_FRAME)
        {
            // Iterate through all objects
            for (local i = 1; i < Env->Objs.Count; i++)
            {
                // Check if the found object is a worm
                local foundObj = CGObject(Env->Objs.Objs[i]);
                if (foundObj is CWorm)
                {
                    // Check the coordinates
                    if (foundObj->PosY > 1000)
                    {
                        foundObj->SpY = 0;
                    }
                }
            }
        }
    }
}


I already looked at the other scripts but they weren't really helpful to me...

Author:  Entuser [ Sun Nov 06, 2011 6:31 am ]
Post subject:  Re: Looping through all worms each frame - I'm doing it wron

You've forgot to put super; .. sry for late answer, i has been inactive for awhile.

Author:  Pac-Man [ Wed Nov 16, 2011 1:37 pm ]
Post subject:  Re: Looping through all worms each frame - I'm doing it wron

Just because Steps pulls me to get it to work:
Code:
require utils;

// Event each frame
override void CTurnGame::Message(CObject* sender, EMType type, int size, CMessageData* data)
{
    // Calculations each frame
    if (type == M_FRAME)
    {
        // Iterate through all objects
        for (local i = 1; i < Env->Objs.Count; i++)
        {
            // Check if the found object is a worm
            local foundObj = CGObject(Env->Objs.Objs[i]);
            if (foundObj is CWorm)
            {
                // Check the coordinates
                if (foundObj->PosY > 500)
                {
                    foundObj->SpY = 0;
                }
            }
        }
    }
    super;
}


doesnt work at all
looks like foundObj->SpY = 0; does nothing (printing a message in there works)

Author:  StepS [ Thu Nov 17, 2011 1:41 pm ]
Post subject:  Re: Looping through all worms each frame - I'm doing it wron

problem solved by using the PosY override rather than SpY. :)

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