I've spent a number of hours this week trying to accomplish this goal. Unfortunately, after making some progress, I think I've hit a wall.
Even though a) the keyboard inputs are detected properly, b) my global-like 'thrust' var is being set properly, and c) the Tick override is applying it the way I thought would work,
there just isn't any movement.
(As a last resort, I even tried moving Super.Tick() at the beginning of the function, but it made no difference.)
At the end of the code you'll see the 2 Printfs. The first one always reports 0 for the cmd.forwardmove, and the second always reports it being set properly with the h.thrust value. (And it's always the right intervals based on my button presses, though of course it's 2 printfs per tick, so the console fills fast. If you want to try it, I suggest pressing F twice in rapid succesion. Mix in presses of left Shift too for the running speed.)
These thrust values are what I found from Github web searches of the GZ repo. (For instance, see
here,
here and
here.)
So my question now:
Is it even possible to induce player movement via Zscript?
If the answer is Yes, and you know how to do it, please respond here. I really would like this AutoMove feature; it would mean less W-keying for backtracking, looting after a fight, etc. Save some strain on my middle finger please
Otherwise, I guess it would mean having to modify the GZ source. That's a route I'd rather not go down.
Anyhow, here's my code. If you want to try it, just make the 2 text files and zip them up. (I'm sharing it this way because I benefitted from searchable code snippets on the forum in the process of getting as far as I did.)
mapinfo.txt
Code: Select all
GameInfo
{
AddEventHandlers = "AutoForwardHandler"
}
zscript.zc
Code: Select all
version 4.2
class AutoForwardHandler : EventHandler
{
int16 thrust;
override void OnRegister()
{
thrust = 0;
}
override void WorldTick()
{
if ( level.time == 0 )
{
thrust = 0;
}
}
override bool InputProcess( InputEvent e )
{
if ( e.Type == InputEvent.Type_KeyDown )
{
if ( e.KeyScan == 0x11 || e.KeyScan == 0x1F )
{
SendNetworkEvent( "AF_stop" );
}
else if ( e.KeyScan == 0x21 )
{
SendNetworkEvent( "AF_toggle" );
}
else if ( e.KeyScan == 0x2A )
{
SendNetworkEvent( "AF_shift_pressed" );
}
}
else if ( e.Type == InputEvent.Type_KeyUp && e.KeyScan == 0x2A )
{
SendNetworkEvent( "AF_shift_released" );
}
return false;
}
override void NetworkProcess( ConsoleEvent e )
{
if ( e.Name == "AF_stop" )
{
thrust = 0;
}
else if ( e.Name == "AF_toggle" ) // values from b_bot.h
{
thrust = thrust ? 0 : 0x1900;
}
else if ( e.Name == "AF_shift_pressed" && thrust == 0x1900 )
{
thrust = 0x3200;
}
else if ( e.Name == "AF_shift_released" && thrust == 0x3200 )
{
thrust = 0x1900;
}
}
override void PlayerEntered( PlayerEvent e )
{
players[ e.PlayerNumber ].mo.A_GiveInventory( "DoAutoForward", 1 );
}
}
class DoAutoForward : Inventory
{
AutoForwardHandler h;
override void Tick()
{
if ( owner && owner.player )
{
// since each level has its own handler
h = AutoForwardHandler( EventHandler.Find("AutoForwardHandler") );
if ( h.thrust )
{
Console.Printf( "cmd.fm before = 0x%X", owner.player.cmd.forwardmove );
owner.player.cmd.forwardmove = h.thrust;
Console.Printf( "h.thrust = 0x%X cmd.fm = 0x%X", h.thrust, owner.player.cmd.forwardmove );
}
}
Super.Tick();
}
}