Add a message to an in-use item (Scanner)?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
User avatar
Enjay
 
 
Posts: 27352
Joined: Tue Jul 15, 2003 4:58 pm
Location: Scotland
Contact:

Add a message to an in-use item (Scanner)?

Post by Enjay »

I have made a modified version of the Strife scanner powerup so that I can use it in a Doom map. It's not vastly changed from the original, but there are a few differences, like duration, the sprite sequence, the fact it plays a sound if you try to use it without an allmap active etc. etc. However, it's still very similar to the standard item.

One of the things I have changed is that it is possible to carry more than one scanner in the inventory. So, a player might be able to pick up a few and use the scanner several times during a long map. That's all fine.

I was also pleased to find that if the scanner is already active, you can't activate a second one from your inventory until the first one has run out (so you don't waste one by accident if you hit the inventory activation key by mistake while the scanner is active). So, that's good too. I was going to try to figure out how to do that, but there's no need; it already works like that.

However, it would be nice if I could add a message and sound to the item so that if the player does try to use a second scanner while one is already active, they get told something like "You already have an active scanner" and a little "beep" plays. At the moment, it just does nothing - which is fine, but it could be better.

The problem is, I can't figure out where or how to add it. Any ZScript gurus out there able to help?

My code for reference:

Code: Select all

class NJScanner : PowerupGiver
{
	Default
	{
		+INVENTORY.FANCYPICKUPSOUND
		Inventory.MaxAmount 25;
		Powerup.Duration -90;
		Scale 0.5;
		Tag "Portacomp Scanner\n90 seconds of enhanced map";
		Inventory.Icon "I_PMSC";
		Powerup.Type "PowerScanner";
		Inventory.PickupSound "misc/allmap";
		Inventory.UseSound "misc/allmap";
		Inventory.PickupMessage "Picked up a map scanner.";
	}
	States
	{
	Spawn:
		PMSC ABCD 6;
		Loop;
	}
	
	override bool Use (bool pickup)
	{
		if (!level.AllMap)
		{
			if (Owner.CheckLocalView())
			{
				Console.MidPrint(null, "\cEThis only works\n\cEif you have a computer map active.");
				Owner.A_StartSound ("*puzzfail", CHAN_ITEM, CHANF_DEFAULT, 1.0, ATTN_NORM, 0, 0);
			}
			return false;
		}
		return Super.Use (pickup);
	}
	
}
If there's anything in there that is wrong, I'll happily take feedback on that too. I struggle with the more complex ZScript stuff. e.g. it took me ages to figure out how to make *puzfail play from where the player is. Prior to adding "owner." to the A_StartSound line, the sound was being played from wherever the pickup was collected. :lol:
User avatar
MartinHowe
Posts: 2097
Joined: Mon Aug 11, 2003 1:50 pm
Preferred Pronouns: He/Him
Location: East Suffolk (UK)

Re: Add a message to an in-use item (Scanner)?

Post by MartinHowe »

Probably have a static (variable that is shared by the entire class, not per instance) boolean variable that says if one is in use; or even an actor pointer to it. Set it (after checking it) in Use, clear it in EndEffect. Default value for bool/pointer is zero (i.e., false/null) so it won't need initialising.

So something like: in use, check if the pointer is null; if not then print the message and play the sound; if so then set the pointer to 'self'.
User avatar
fakemai
Posts: 408
Joined: Mon Feb 12, 2018 12:26 am
Location: Australia

Re: Add a message to an in-use item (Scanner)?

Post by fakemai »

If the item already rejects being stacked like most artifacts, e.g. Tome of Power, Super.Use should return false, that's the thing you want to check to print your message to the owner. Incidentally the wiki article has void as its return type which is bogus. https://zdoom.org/wiki/Use

EDIT: Yep. So you can use the checks from the vanilla scanner Use as you are, then if that's fine check Super.Use and print your message if false.
User avatar
Enjay
 
 
Posts: 27352
Joined: Tue Jul 15, 2003 4:58 pm
Location: Scotland
Contact:

Re: Add a message to an in-use item (Scanner)?

Post by Enjay »

Thanks for the input, I appreciate it. Unfortunately, with anything more complicated than DECORATE-like levels of ZScript, I'm struggling. I'm at the stage where everything that has been said makes sense to me: I can follow the logic; I know the words and I understand the principles. However, putting it into practice in the form of actual working code is still a stumbling block.

It's almost like if you were learning to drive a manual car and someone described pressing down the clutch, putting the car into gear, increasing pressure on the accelerator pedal slightly, bringing up the clutch to its biting point and feathering in the clutch while moving away and increasing the acceleration. I'm at the stage where I know what all the words mean, I know what a gearstick and clutch do, I can follow the logic of the instructions but I'm left asking "OK, I get that, but which one is the clutch?" :lol:

So, anyway, I've just spent almost two hours stabbing and stabbing at doing this, firing what I'd written up in UZDoom, having UZDoom give me an error message (which I sometimes understood, and sometimes didn't) but, ultimately, I failed. Frustratingly, I know that it's nothing complex - just a small amount of simple code - but when you don't know exactly how to write it, the game doesn't care, because it's simply not correct. I've also done several things that just failed silently (literally - given that I'm trying to play a sound). i.e. it obviously wasn't illegal code, it just wasn't doing what I wanted it to (presumably, it was doing nothing).

If some kind soul is able to give me how it should actually look, then I can get this item working and keep the code (as I do with other snippets that I've been helped with) so that I can learn from it and (hopefully) not have to ask again. (I'll keep stabbing away in the meantime though.)
User avatar
fakemai
Posts: 408
Joined: Mon Feb 12, 2018 12:26 am
Location: Australia

Re: Add a message to an in-use item (Scanner)?

Post by fakemai »

Your initial code looks fine but where the return Super.Use(Pickup); you can instead do:

Code: Select all

bool r = Super.Use(pickup);
if(!r) {
    // Do your print failure message here and maybe a SFX.
}
return r;
The return value of Use indicates if the use of the item succeeded which then gets used by whatever called it to, say, play a SFX and deduct an item and all the other stuff that can happen. A simpler case is the class for HP restoring items like the Quartz Flask. That above snippet could be the entire body of a function override for a custom health item that plays a failure SFX if you try using it from full, you're just leveraging the parent's Use function. Same for your scanner which would be going through this nice function but effectively if it failed it's probably because the player has an active effect already.
User avatar
Enjay
 
 
Posts: 27352
Joined: Tue Jul 15, 2003 4:58 pm
Location: Scotland
Contact:

Re: Add a message to an in-use item (Scanner)?

Post by Enjay »

Thank you for both the code and the explanation. I've got it working now. A scanner in the inventory will play a bleep and put a message on screen if you try to use it when one is already active.

Embarrassingly, the code I had been trying didn't look much like what you posted. I had looked at several powerups and the powerups.zs page that you linked to but I didn't understand it well enough to pick out the right bits and make them work with my item.

Thank you once again.
Post Reply

Return to “Scripting”