MODULE: Controlz 0.4.0

Started by eri0o, Wed 28/08/2019 01:30:16

Previous topic - Next topic

eri0o

Controlz  0.4.0

controlz.scm | controlz_demo_windows.zip | GitHub repo

Move your character with keyboard or joystick controlz for Adventure Game Studio.


This code was originally made by Dualnames for Strangeland and I eri0o got his permission to open source and wrapped in this function to be easier for consumption.

Usage example

Call it on repeatedly execute, in your Global Script or in the script that you deal with player input, passing a character and some way to check for directional keys, once for each direction (down, left, right, up).

In the example below, WASD will be used to control the player character, and arrow keys will be used to control a second character named cEgo2.

Code: ags
// called on every game cycle, except when the game is blocked
function repeatedly_execute() 
{
  Controlz(player, 
    IsKeyPressed(eKeyDownArrow),
    IsKeyPressed(eKeyLeftArrow), 
    IsKeyPressed(eKeyRightArrow),
    IsKeyPressed(eKeyUpArrow));

  Controlz(cEgo2, 
    IsKeyPressed(eKeyS),  IsKeyPressed(eKeyA), 
    IsKeyPressed(eKeyD),  IsKeyPressed(eKeyW));
}

  • Since 0.3.0, solid characters collide with solid characters.
  • Since 0.4.0, a character will keep walking in place if a button is pressed.

script API

Controlz only has a single function

Controlz(Character* c, bool down, bool left, bool right, bool up)

Call it on your repeatedly execute or repeatedly execute always, passing a character and which keys are pressed at that time. If you need to control more characters, just call Controlz again, passing the new character and the buttons that are mapped to move it.

You can check for multiple keys or inputs too.

Code: ags
function repeatedly_execute() 
{
  Controlz(player, 
    IsKeyPressed(eKeyDownArrow) || IsKeyPressed(eKeyS),
    IsKeyPressed(eKeyLeftArrow) || IsKeyPressed(eKeyA), 
    IsKeyPressed(eKeyRightArrow) || IsKeyPressed(eKeyD),
    IsKeyPressed(eKeyUpArrow) || IsKeyPressed(eKeyW));
}

License

This code is licensed with MIT LICENSE.

Olleh19

#1
Edit: Got it working. I was dumb  (laugh)



arj0n

Quote from: Olleh19 on Tue 20/10/2020 21:38:43
DLL's are in compiled folder and the mainfolder.

Quote from: Dualnames on Tue 30/04/2019 03:20:35
The only thing u have to do, is make sure u put SDL2.dll inside ur debug and compiled folders, otherwise you'll get error messages.

Olleh19

Quote from: arj0n on Tue 20/10/2020 22:38:16
Quote from: Olleh19 on Tue 20/10/2020 21:38:43
DLL's are in compiled folder and the mainfolder.

Quote from: Dualnames on Tue 30/04/2019 03:20:35
The only thing u have to do, is make sure u put SDL2.dll inside ur debug and compiled folders, otherwise you'll get error messages.

Actually i forgot to type that in, ofc i've followed his instructions! But thanks for reminding me, i should edit the post!  (laugh)

fratello Manu

Hi!

I'm using your module and enjoying it!

But I have a problem: it seems not to play sound related to walking frames. I assigned a step sound to some frames and usually this way I can hear sounds.
If I move the character with arrow keys, I can't hear any sound. But if I move him from script (or via mouse click as well, but my game won't be mouse-controlled) using the .Walk() method, sound is correctly played

thanks!
Bye!
Manu

eri0o

#5
Probably lockviewframe doesn't play sound, so I need to handle it manually. I actually never noticed sounds weren't playing. I will fix this later tonight and update it here!

Edit: I think I fixed, please verify!

fratello Manu

It works!  :wink:
Awesome!
thanks!

eri0o

Hi, I added support for solid characters. If you control a solid character it should collide with other solid character.

fratello Manu

Hi!
Good job, it works!
I also had to set the character's .BlockingHeight to obtain the proper blocking

Thanks!

shaun9991

This is a fantastic module, Eri0o - thanks!

Quick questions - how do I map this to joystick controls? Do I need to use the Joystick module alongside it?

Also, when the player reaches the end of a walkable area, is it possible to have the player continue to animate in their walkcycle (while standing in place) rather than coming to a stop mid animation?

Many thanks,
Shaun
Support Cloak and Dagger Games on Patreon: https://www.patreon.com/user?u=460039

eri0o

#10
QuoteQuick questions - how do I map this to joystick controls? Do I need to use the Joystick module alongside it?

The module API is just that single function, so it's only a matter of passing a true for the direction that is pressed and false on the others (the module supports diagonals if your character has 8 loops).

I usually don't do this directly though, because I want to be able to remap buttons - I have a script for button remapping but I haven't open sourced it basically because it's a bit ingrained in my game menus and a bit hard to pull out...

Unfortunately for now joystick in AGS requires a plugin. So you use either the hat check (for arrows) or the analog stick stuff (for the left analog) and then convert them to a binary like check to pass the resulting boolean. I have been thinking a lot of adding a GamePad API (all joysticks as Xbox360 gamepads) in AGS directly (no plugins) since this would make much easier to support joysticks in Android and WebGL builds. With the new SDL Port I can't think of a reason why not.

My joystick handling is wrapped in a separate module that handles joystick button mapping, so it's not transparent and ends up looking like this:

Code: ags
bool downkeyLA = adventureJoy.isWalkDownPressed() || IsKeyPressed(eKeyS);
bool leftkeyLA = adventureJoy.isWalkLeftPressed() || IsKeyPressed(eKeyA);
bool rightkeyLA = adventureJoy.isWalkRightPressed() || IsKeyPressed(eKeyD);
bool upkeyLA = adventureJoy.isWalkUpPressed() || IsKeyPressed(eKeyW);
        
movement_state = Controlz(player, downkeyLA, leftkeyLA, rightkeyLA, upkeyLA);


QuoteAlso, when the player reaches the end of a walkable area, is it possible to have the player continue to animate in their walkcycle (while standing in place) rather than coming to a stop mid animation?

I haven't tried but have you tried adding a iddle animation for this? Alternatively, the function is not void, it returns a state bit that may be useful for this. Other than this I am not sure how to handle this for people that want the full stop - aka, me.  :-\

I may not have understood exactly if the full stop you mean is the one I think you are, so a gif/video to show what you mean would be nice.

eri0o

I now understood the feature request, I am trying to think how to add this to the API in a way it doesn't get complicated - I like that the module has a single function... I should come up with a solution soon.

eri0o

New Controlz 0.4.0 release. This version introduces a breaking change, now if you keep a button pressed and a character is facing a wall they will keep animating their walk animation (they won't stop animating).

When calling Controlz it actually returns an enum state corresponding to it's movement, each state represents a different moment

Code: ags
enum MovingState {
  eControlzJustInvalid, // should not return this never
  eControlzStopped, // returns this when there's no input and no animation, continuously
  eControlzMoving, // returns this when the character is moving (position changing and animating)
  eControlzJustReleased // when the input is ceased and controlz stopped animating the character, it returns this for only one frame, you are free to animate the character after this
  eControlzWalkingInPlace, // the character is walking in place, facing a wall, you can use this information to decide to change it's normal view to something else
};

Quote from: shaun9991 on Wed 30/06/2021 10:30:05Also, when the player reaches the end of a walkable area, is it possible to have the player continue to animate in their walkcycle (while standing in place) rather than coming to a stop mid animation?

So this is the change this new version introduces.

SMF spam blocked by CleanTalk