Delay button press check & shields gfx issues, help?

Status
Not open for further replies.

⛧Labyrinthia⛧

Vampire scene rotgirl
So, I won't bother with the specific details but I made this silly script as a concept for something else I'm working on.

Code:
local CANCHANGE = false
local CANREVERT = true

addHook("ThinkFrame", function()
	for player in players.iterate do
		if player.mo.skin == "sonic"
		and player.mo.health >= 6
		and player.powers[pw_shield] == SH_NONE
			CANCHANGE = true
		else
			CANCHANGE = false
		end

		if player.mo.skin == "sonic"
		and player.powers[pw_shield] != SH_NONE
			CANREVERT = true
		else
			CANREVERT = false
		end

		if CANCHANGE == true
		and (player.cmd.buttons & BT_CUSTOM1)
			player.powers[pw_shield] = SH_BOMB
			player.mo.health = player.mo.health - 5
			player.health = $1 - 5
			S_StartSound(nil, sfx_shield)
		end

		if CANREVERT == true
		and (player.cmd.buttons & BT_CUSTOM1)
			player.powers[pw_shield] = SH_NONE
			S_StartSound(nil, sfx_shldls)
		else
			return
		end
	end
end)

This should give you the Armageddon Shield (as long as you have no shields) if you pressed CUSTOM 1, at the cost of 5 rings. And then it should remove any shield you have if you pressed CUSTOM 1 again.

It works "fine", yes, but I'm running into 2 issues:

  1. The game is calling the function way too much (every frame that CUSTOM1 is being held instead of only once per keypress, no matter how long it's held)
  2. The sprite for the shield simply doesn't appear even though it is there, as in, still protects me from a hit and explodes like it should.

I'm guessing that the second one is because calling the shield doesn't also spawns the sprite for it, so I might have an idea or two to circumvent it but I don't know how to work with the first one. Is it possible to make a time window that pressing CUST1 won't activate the script? Or make it so that after the function is called once, it won't happen again until you release the key and press it again? Genuinely don't know what to do here.
 
* You can use P_SpawnShieldOrb(player) to set player's shield orb appearance to the appropriate one.

* To detect when a player first presses a button, store in a player-specific variable whether they are holding that button, and update that variable at the end of your once-per-tic thinker. This means that, during your thinker, the variable represents whether the player was holding the button last tic. When they're holding the button right now, but weren't holding it last tic, that means they've pressed it.
 
* You can use P_SpawnShieldOrb(player) to set player's shield orb appearance to the appropriate one.

* To detect when a player first presses a button, store in a player-specific variable whether they are holding that button, and update that variable at the end of your once-per-tic thinker. This means that, during your thinker, the variable represents whether the player was holding the button last tic. When they're holding the button right now, but weren't holding it last tic, that means they've pressed it.
WOAH this took a while, more than I'm proud to admit, but thank you. I got it working!
 
I would like to point out that this.
Code:
S_StartSound(nil, sfx_shldls)
Code:
S_StartSound(nil, sfx_shield)
Would cause the sound to play for every player in the server, if in a netgame, just add a player argument to the end of the functions so the sound only plays for the player who triggered it, like so.
Code:
S_StartSound(nil, sfx_shldls, player)
 
Status
Not open for further replies.

Who is viewing this thread (Total: 0, Members: 0, Guests: 0)

Back
Top