Help for a script, using Number Keys to Auto Select weapon

GPC2 script programming for Titan Two. Code examples, questions, requests.

Help for a script, using Number Keys to Auto Select weapon

Postby maw » Fri Apr 23, 2021 3:47 pm

Hey there,
I am a programmer, so I know how to code. But I have no idea why the combo is not being executed. The logs you see in the example are all logged correctly & everything looks fine. But its not doing anything in the game.
The idea was to use the number 1-5 to direclty select the correct weapon/item slot in Fortnite on the Nintendo Switch (I ignored the pickaxe). I added the number 0 key to reset everything.
It would be great if you could help me, to get this running. Maybe I am missing a simple option/setting somewhere...

Code: Select all
 
#pragma METAINFO("Fortnite", 1, 2, "")
#include <keyboard.gph>
#include "key_events.gph"
 
int8 currentSlot, maxSlot, minSlot, slotLoopIndex, slotLoopEnd;
bool isSlotLoop;
 
init {
    maxSlot = 5;
    minSlot = 1;
    currentSlot = 1;
    isSlotLoop = FALSE;
    slotLoopIndex = 0;
}
 
main {
    if (key_event_active(KEY_1)) {
        setSlotVals(1);
    }
    if (key_event_active(KEY_2)) {
        setSlotVals(2);
    }
    if (key_event_active(KEY_3)) {
        setSlotVals(3);
    }
    if (key_event_active(KEY_4)) {
        setSlotVals(4);
    }
    if (key_event_active(KEY_5)) {
        setSlotVals(5);
    }
    if (key_event_active(KEY_0)) {
        combo_stop(loopSlots);
        currentSlot = minSlot;
        isSlotLoop = FALSE;
        slotLoopIndex = 0;
    }
    if (event_active(BUTTON_4)) {
        updateCurrentSlot(1);
    }
    if (event_active(BUTTON_7)) {
        updateCurrentSlot(-1);
    }
    if (isSlotLoop) {
        printf("i: %d, e: %s", slotLoopIndex, slotLoopEnd);
        if (slotLoopIndex == slotLoopEnd) {
            slotLoopIndex = 0;
            isSlotLoop = FALSE;
            printf("combo stop");
            combo_stop(loopSlots);
        } else {
            combo_run(loopSlots);
            slotLoopIndex++;
        }
    }
}
 
combo loopSlots {
    printf("slotLoop: %d", slotLoopIndex);
    set_val(BUTTON_4,100.0)
    wait(100);             
    set_val(BUTTON_4,0.0);
    wait(100);
}
 
void updateCurrentSlot(int8 offset) {
    currentSlot = currentSlot + offset;
    if (currentSlot > maxSlot) {
        currentSlot = minSlot;
    }
    if (currentSlot < 0) {
        currentSlot = maxSlot;
    }
}
 
void setSlotVals(int8 newSlot) {
    printf("change loop: %d", newSlot);
    if (newSlot != currentSlot) {
        slotLoopEnd = newSlot - currentSlot;
        if (slotLoopEnd < 0) {
            slotLoopEnd = slotLoopEnd + maxSlot;
        }
        isSlotLoop = TRUE;
        currentSlot = newSlot;
        slotLoopIndex = 0;
    }
}
 
User avatar
maw
Sergeant
Sergeant
 
Posts: 7
Joined: Fri Apr 23, 2021 3:37 pm

Re: Help for a script, using Number Keys to Auto Select weap

Postby maw » Mon Apr 26, 2021 12:56 pm

I would be very glad, if someone could try that script. It is my first and I am very disappointed, that nothing in it works.
The first version of it was very buggy, because C (GPC2) is not a language that I use. But it did something in the game.
Now, after I removed a the bugs, I have it perfectly working, when I trust the debug printf output. But I have zero feedback in the game.
Has anyone an idea what the problem might be? Maybe its just a settings checkbox, like enabling keyboard keys or something. (Although I doubt that, since it was doing stuff before).

Thanks!
User avatar
maw
Sergeant
Sergeant
 
Posts: 7
Joined: Fri Apr 23, 2021 3:37 pm

Re: Help for a script, using Number Keys to Auto Select weap

Postby maw » Mon Apr 26, 2021 1:09 pm

I will try to use `key_status` later this day instead of using `key_event_active`.
Maybe it doesn't work in the game because "key_events.gph" is not transferred to TitanTwo.
User avatar
maw
Sergeant
Sergeant
 
Posts: 7
Joined: Fri Apr 23, 2021 3:37 pm

Re: Help for a script, using Number Keys to Auto Select weap

Postby Scachi » Mon Apr 26, 2021 7:33 pm

key_event_active works fine.

the main runs each 1ms or faster.
combo_run does not wait for the combo to finish before it returns to the main.
your slotLoopIndex counter will reach its destination value before the combo is sending its first button press.
take a look at the device monitor and you will see that the combo doesn't press any buttons.
moving the line
slotLoopIndex++;
from main to the end of the combo (after the last wait) and you will see some buttons being pressed in the device monitor.
this way the counter gets increased at the end of each combo run.

left side of the device monitor shows the input side, right side shows the output side.
you can drag & drop buttons from left or right to the Channels in the Output Graph and hit the run button for displaying the input or output side in that graph. that may help with spotting very fast button pressed.
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: Help for a script, using Number Keys to Auto Select weap

Postby maw » Mon Apr 26, 2021 8:28 pm

Thanks a lot. I guessed something like this this afternoon, but I couldn't manage to test it.
I noticed already that the device monitor doesn't show any outputs and was wondering why not.
But this explains it.
Good to know that the main loop runs every 1ms. I guess it is a very bad idea to block the main loop, since it would mess up everything. But I should manage to build a little toggle value, which blocks the slotLoopIndex to be incremented too quickly.
I keep you posted.
User avatar
maw
Sergeant
Sergeant
 
Posts: 7
Joined: Fri Apr 23, 2021 3:37 pm

Re: Help for a script, using Number Keys to Auto Select weap

Postby Scachi » Mon Apr 26, 2021 8:34 pm

Just move the increase of the counter into the combo as I said, easiest solution:
Code: Select all
combo loopSlots {
    printf("slotLoop: %d", slotLoopIndex);
    set_val(BUTTON_4,100.0)
    wait(100);             
    set_val(BUTTON_4,0.0);
    wait(100);
    slotLoopIndex++;
}
 


Or use the combo name directly to check if it is already running and only increase the counter if this is false:
if (loopSlots)
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: Help for a script, using Number Keys to Auto Select weap

Postby maw » Tue Apr 27, 2021 5:41 am

Thanks for the hint. I remembered the boolean check on a combo from the docs while I was thinking about the addtional boolean value. The script works fine now.
I guess I will add some more logic, to use the shortest way for looping through the slots. At the moment its only from left to right. But when you are on slot 5 and select 4, it should do just one combo execution to the left instead of four to the right.
Anyways thank you so much for helping me with this. I understand the basic concept of the main loop and the combos.

In case somebody is interested:

Code: Select all
#pragma METAINFO("Fortnite", 1, 13, "")
#include <keyboard.gph>
#include "key_events.gph"
 
int8 currentSlot, maxSlot, minSlot, slotLoopIndex, slotLoopEnd;
bool isSlotLoop;
 
init {
    maxSlot = 5;
    minSlot = 1;
    currentSlot = 1;
    slotLoopIndex = 0;
    isSlotLoop = FALSE;
}
 
main {
    if (key_event_active(KEY_1)) {
        setSlotVals(1);
    }
    if (key_event_active(KEY_2)) {
        setSlotVals(2);
    }
    if (key_event_active(KEY_3)) {
        setSlotVals(3);
    }
    if (key_event_active(KEY_4)) {
        setSlotVals(4);
    }
    if (key_event_active(KEY_5)) {
        setSlotVals(5);
    }
    if (key_event_active(KEY_0)) {
        printf("clear");
        currentSlot = minSlot;
        isSlotLoop = FALSE;
        slotLoopIndex = 0;
        combo_stop(loopSlots);
    }
    if (event_active(BUTTON_4)) {
        printf("E pressed");
        updateCurrentSlot(1);
    }
    if (event_active(BUTTON_7)) {
        printf("Q pressed");
        updateCurrentSlot(-1);
    }
    if (isSlotLoop && !loopSlots) {
        printf("i: %d, e: %s", slotLoopIndex, slotLoopEnd);
        if (slotLoopIndex < slotLoopEnd)  {
            printf("slotLoop: %d", slotLoopIndex);
            combo_run(loopSlots);
            slotLoopIndex++;   
        } else {
            slotLoopIndex = 0;
            isSlotLoop = FALSE;
            printf("combo stop");
        }
    }
}
 
combo loopSlots {
    set_val(BUTTON_4,100.0)
    wait(100);             
    set_val(BUTTON_4,0.0);
    wait(50);
}
 
void updateCurrentSlot(int8 offset) {
    currentSlot = currentSlot + offset;
    if (currentSlot > maxSlot) {
        currentSlot = minSlot;
    }
    if (currentSlot < 1) {
        currentSlot = maxSlot;
    }
    printf("currentSlot update: %d", currentSlot);
}
 
void setSlotVals(int8 newSlot) {
    printf("change loop: %d", newSlot);
    if (newSlot != currentSlot) {
        slotLoopEnd = newSlot - currentSlot;
        if (slotLoopEnd < 0) {
            slotLoopEnd = slotLoopEnd + maxSlot;
        }
        isSlotLoop = TRUE;
        currentSlot = newSlot;
        slotLoopIndex = 0;
    }
}
 
User avatar
maw
Sergeant
Sergeant
 
Posts: 7
Joined: Fri Apr 23, 2021 3:37 pm

Re: Help for a script, using Number Keys to Auto Select weap

Postby maw » Wed Apr 28, 2021 5:48 am

Just a short update. The weapon switch with number key uses the shortest direction now, feel free to use it.
But I haven't optimized it much. I could use better math operations to set min and max values. But I think you can read it better like this.

Code: Select all
#pragma METAINFO("Fortnite", 1, 14, "maw")
#include <keyboard.gph>
#include "key_events.gph"
 
#define LOOP_BUTTON(ltr)  ((ltr) ?  (BUTTON_4) : (BUTTON_7))
 
int8 currentSlot, maxSlot, minSlot, slotLoopIndex, slotLoopEnd;
bool isSlotLoop, isLoopingLtr;
 
init {
    maxSlot = 5;
    minSlot = 1;
    currentSlot = 1;
    slotLoopIndex = 0;
    isSlotLoop = FALSE;
    isLoopingLtr = TRUE;
}
 
main {
    if (key_event_active(KEY_1)) {
        setSlotVals(1);
    }
    if (key_event_active(KEY_2)) {
        setSlotVals(2);
    }
    if (key_event_active(KEY_3)) {
        setSlotVals(3);
    }
    if (key_event_active(KEY_4)) {
        setSlotVals(4);
    }
    if (key_event_active(KEY_5)) {
        setSlotVals(5);
    }
    if (key_event_active(KEY_0)) {
        currentSlot = minSlot;
        isSlotLoop = FALSE;
        slotLoopIndex = 0;
        combo_stop(loopSlots);
    }
    if (event_active(BUTTON_4)) {
        updateCurrentSlot(1);
    }
    if (event_active(BUTTON_7)) {
        updateCurrentSlot(-1);
    }
    if (isSlotLoop && !loopSlots) {
        if (slotLoopIndex < slotLoopEnd)  {
            combo_run(loopSlots);
            slotLoopIndex++;   
        } else {
            slotLoopIndex = 0;
            isSlotLoop = FALSE;
        }
    }
}
 
combo loopSlots {
    set_val(LOOP_BUTTON(isLoopingLtr),100.0)
    wait(100);             
    set_val(LOOP_BUTTON(isLoopingLtr),0.0);
    wait(50);
}
 
void updateCurrentSlot(int8 offset) {
    currentSlot = currentSlot + offset;
    if (currentSlot > maxSlot) {
        currentSlot = minSlot;
    }
    if (currentSlot < 1) {
        currentSlot = maxSlot;
    }
}
 
void setSlotVals(int8 newSlot) {
    if (newSlot != currentSlot) {
        slotLoopEnd = newSlot - currentSlot;
        if (slotLoopEnd < 0) {
            slotLoopEnd = slotLoopEnd + maxSlot;
        }
        if (slotLoopEnd > 2) {
            slotLoopEnd = maxSlot - slotLoopEnd;
            isLoopingLtr = FALSE;
        } else {
            isLoopingLtr = TRUE;
        }
        isSlotLoop = TRUE;
        currentSlot = newSlot;
        slotLoopIndex = 0;
    }
}
 
User avatar
maw
Sergeant
Sergeant
 
Posts: 7
Joined: Fri Apr 23, 2021 3:37 pm


Return to GPC2 Script Programming

Who is online

Users browsing this forum: No registered users and 76 guests