May 23, 2012, 02:26:30 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Please report any bugs or issues that you might be encountering with the Beta in the Support System so that we can better keep track of any oustanding issues that may come up.

GameCore Support System
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Please solve this code problem  (Read 960 times)
pixel_legolas
Hero Member
*****
Posts: 786


View Profile
« on: October 13, 2009, 05:37:02 AM »

There is a code problem that would solve ALOT of the issues I am having.

The problem is like this, I want to make something happen just once when pressing a key. This would solve things as:

- When jumping with space and holding it down will make character jump once
- When using an attack animation character attacks only once even if attack button is pressed
- Playing an attack/jump sound once when entering respective key.

There are an infinite list of what this "make it happen once" code could do but I have not seen it yet being used. Have I just missed it and can it be applied to everything easy using just a small while or if statement?
Logged
Ron
Sr. Member
****
Posts: 326


View Profile
« Reply #1 on: October 13, 2009, 12:20:42 PM »

This might help, its what I use for parachute deployment. Its would all go in your character gsl file.

Code:
bool ChuteOpen = false;

void Initialize( Object@ object)
{
.....
}

bool HandleEvent( Object@ object, const String& in event, GameEventParams@ params)
{
        if (event == "Input")
        {
if (params.stringValue == "Chute")
{
chutePressed = params.floatValue;
return true;
}
        }
..........
}

void DynamicsAdvance( Object@ object, float seconds)
{
Object@ Chute = GetGameManager().GetWorld().GetObject( "parachute");

if (!ChuteOpen && chutePressed > 0.5f && speed > 20)
{
Chute.PlayAnim("Deploy");
ChuteOpen = true;
}
}
Logged
Ron
Sr. Member
****
Posts: 326


View Profile
« Reply #2 on: October 13, 2009, 12:49:40 PM »

Here is a little more complex version playing different animations and adding a force when its open. It also resets itself after a time interval
Code:
bool ChuteOpen = false;
float ChuteTime = 0;

void Initialize( Object@ object)
{
.....
}

bool HandleEvent( Object@ object, const String& in event, GameEventParams@ params)
{
        if (event == "Input")
        {
if (params.stringValue == "Chute")
{
chutePressed = params.floatValue;
return true;
}
        }
..........
}

void DynamicsAdvance( Object@ object, float seconds)
{
Object@ Chute = GetGameManager().GetWorld().GetObject( "parachute");
ObjectCollide@ body = object.GetCollideObject( "Body");

if (!ChuteOpen && chutePressed > 0.5f && speed > 20)
{
Chute.PlayAnim("Deploy");
ChuteOpen = true;
}
else if (ChuteOpen && ChuteTime > 14.0)
{
Chute.PlayAnim("Idle");
ChuteOpen = false;
}
else if (ChuteOpen && ChuteTime > 12.0)
{
Chute.PlayAnim("Repack");
}
else if (ChuteOpen && ChuteTime > 10.0)
{
Chute.PlayAnim("Deflate");
}
else if (ChuteOpen && ChuteTime > 2.0)
{
Chute.PlayAnim("Deployed");
}

if (ChuteOpen)
{
ChuteTime += seconds;
float chuteForce = speed * windDrag * 250.0f;
if (chuteForce >= 0)
{
body.AddForce( object.GetCurMatrix().Project( 0, 0, chuteForce));
}
}
else
ChuteTime = 0;

}


Logged
gekido
Guest
« Reply #3 on: October 13, 2009, 02:26:23 PM »

When handling keypresses, there are a couple of ways to interpret the event.

Previously, in Beyond Virtual, we only had the 'params.floatValue;' method of seeing whether a key has been pressed. 

A few releases ago, we also added bool & int values for keypress events, which allow you to get a bit more specific info about whether a key has been pressed or not.

So you can do something like:

Code:
bool action= false;

bool HandleEvent( Object@ object, const String& in event, GameEventParams@ params)
{
        if (event == "Input")
        {
if (params.stringValue == "BoolAction")
{
action= params.boolValue;  // true if the key has been pressed and not handled previously
return true;
}
        }
}

Or, alternately, you can use the intValue to check and see whether a key has been pressed, released or hasn't changed state, like so:

Code:
bool action= false;

bool HandleEvent( Object@ object, const String& in event, GameEventParams@ params)
{
        if (event == "Input")
        {
if (params.stringValue == "IntAction")
{
                        if (params.intValue == 1)
                        {
                            // intValue value of 1 means that the key has been pressed (ie a KeyDown event)
    action1= true;
}
                        if (params.intValue == -1)
                        {
                            // intValue of -1 means that the key has been released (ie a KeyUp event)
    action= false;
                            return true;
                        }
}
        }
}

So depending upon what you are looking to do you can use any of the floatValue, intValue or boolValue input events to handle specific keys.
Logged
pixel_legolas
Hero Member
*****
Posts: 786


View Profile
« Reply #4 on: October 13, 2009, 02:39:16 PM »

Thanks both of you. One handles this with a timer and the other with bool, I think having both solutions are good for different things.

I will try this the coming days, expect a printed copy in store soon Smiley
Logged
pixel_legolas
Hero Member
*****
Posts: 786


View Profile
« Reply #5 on: October 13, 2009, 04:09:30 PM »

Ok, this is what I have so far but is not working the way I want. The character is now soing the attack animation forever without me even pressing a button:

Adding after jump and move etc

Quote
if (params.stringValue == "Attack")
      {
         AttackPressedis= params.boolValue;  // true if the key has been pressed and not handled previously
         return true;
      }
      
      return false;
   }


In dynamics advance:

Quote
   

if ( AttackPressedis = true)
    {
    //base.SetLinearVelocity( Vector( 0, 0, 0));
    object.PlayAnim("Attack");
   GetGameManager().GetWorld().AddAreaForce(object.GetPosition(), 20, 100);
   
    }

Something happened at least after all errors I got but now it doesn't do what I want   
Logged
Ron
Sr. Member
****
Posts: 326


View Profile
« Reply #6 on: October 13, 2009, 04:21:53 PM »

Looks like you need a way to set it back to false. I have never used the boolValue for inputs before so I not very familiar with that method yet.

You could also set it back to false after a certain amount of time in the DynamicsAdvance section of code or set it to false with the keyup
Logged
pixel_legolas
Hero Member
*****
Posts: 786


View Profile
« Reply #7 on: October 14, 2009, 04:42:28 AM »

Well I am basically stuck here again. I am randomly setting return false; and and other things in script but most often hit a script error, mostly about "this is not a bool" or "bool" is not stated.

Gekido, could you provide a very small demo code with the lego-man or something. Just a very easy attack thingy or just play a sound once when hitting a certain key...say E
Logged
pixel_legolas
Hero Member
*****
Posts: 786


View Profile
« Reply #8 on: October 14, 2009, 06:02:29 PM »

hm, figured it out but probably ugly. This code plays a battle animation for 1.49 seconds and then stops. It resets the timer to zero and when you hit the key again it plays it for another 1.49 seconds

PS. die is actually a battle anim but to many places to change to Smiley


Quote
float diePressed = 0;
bool diePressedis = false;


Quote
  if (params.stringValue == "die")
      {
         diePressed = params.floatValue;
         return true;
      }

Quote
   if ( diePressed > 0.1 && AttackTime < 1.5)
    {
   
   diePressedis = true;
   base.SetLinearVelocity( Vector( 0, 0, 0));
    object.PlayAnim("Die");
   
   }
      

   
   if (diePressedis)
   {
   AttackTime += seconds;
   }
   
   
   
   if (AttackTime == 1.49)
   {
   diePressedis = false;
      
      }
      
if (diePressed < 0.1)
{
diePressedis = false;
AttackTime = 0;
}
« Last Edit: October 14, 2009, 06:19:35 PM by pixel_legolas » Logged
Ron
Sr. Member
****
Posts: 326


View Profile
« Reply #9 on: October 14, 2009, 06:31:35 PM »

Glad you got it working. My typical coding is find 1001 way that it will not work, finally get something semi-working the way I want it to, figure out that I accidentally erased something I needed at trial 500 or so, try to remember what I did, finally get it working somehow and do not know how it happened  Cheesy. The final result is code that absolutely nobody can understand, even myself lol
Logged
BigDaz
Sr. Member
****
Posts: 345


HardCore


View Profile
« Reply #10 on: October 15, 2009, 04:22:51 AM »

This is what I had trouble with when doing my helicopter in the Tips n Tricks section of the forum. The inputs work but you cant input more than 1 thing at a time and it's not very good.
Logged
pixel_legolas
Hero Member
*****
Posts: 786


View Profile
« Reply #11 on: October 15, 2009, 04:32:37 AM »

Ron, I really like your way of doing different animations after a certain time.

I am thinking of a knight where you press the attack key once and hold it. This will make the knight do attack1 and after certain time do attack2. This is good to blend different attacks in 1 motion.

How would I do to make the knight do different attack motions when pressing the attack key? So, when pressing the key he does not start with same animation?

I am thinking of:

Quote
//Super psuedo code deluxe

random (1-3)

Case 1

do attack animation 1

Case 2

do attack animation 2

etc

I still wish to have a good example including the bool version because this timer is only good when you know the exact time it should stop. I prefer it to loop the whole animation and then just return to idle
Logged
gekido
Guest
« Reply #12 on: November 14, 2009, 12:52:58 PM »

If you look in the 'path_character.gsl' script in the default script library (in the AI category) you can find an example of how to do the 'random anim' thing:

Quote
// standing idle, randomize
            int anim = RandomInt(1, 10);
            switch( anim)
            {
               case 1:
                  object.PlayAnim( "Listen");
                  break;
               case 2:
                  object.PlayAnim( "LookAround");
                  break;
               case 3:
                  object.PlayAnim( "Talk1");
                  break;
               case 4:
                  object.PlayAnim( "Talk2");
                  break;
               case 5:
                  object.PlayAnim( "ClapHands");
                  break;
               case 6:
                  object.PlayAnim( "Cheer");
                  break;
               default:
                  object.PlayAnim( "Idle");
                  break;                  
            }
Logged
Pages: [1]
  Print  
 
Jump to:  

 
Powered by MySQL Powered by PHP bluBlur Skin © 2006, hbSkins
Powered by SMF 1.1.14 | SMF © 2006-2011, Simple Machines LLC
Valid XHTML 1.0! Valid CSS!