Sound while Inventory item is a Pickup

Discussions about Coding and Scripting
User avatar
Barbie
Godlike
Posts: 2069
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Sound while Inventory item is a Pickup

Post by Barbie » Sat Feb 06, 2021 12:58 am

I want to realize the following: as long as an Inventory item is a Pickup, it should emit a sound. If it is taken and has become an inventory item, the sound should switch off. I tried to use the functions BecomePickup(), BecomeItem() and SpawnCopy(), but without success.
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett

User avatar
OjitroC
Godlike
Posts: 2334
Joined: Sat Sep 12, 2015 8:46 pm

Re: Sound while Inventory item is a Pickup

Post by OjitroC » Sat Feb 06, 2021 1:22 am

I think some of the XPickups like the BattleSuit or Berserker emit a sound when on the pickup base and this sounds goes when they are picked up - I could be wrong about that but perhaps worth having a look at?

User avatar
sektor2111
Godlike
Posts: 5209
Joined: Sun May 09, 2010 6:15 pm
Location: On the roof.

Re: Sound while Inventory item is a Pickup

Post by sektor2111 » Sat Feb 06, 2021 8:40 am

I think it can be checked for bHidden in a timer or tick.
If pickup is hidden turn off sound or make it None else it should have sound and/or sound assigned.
Perhaps using an "AmbientSound" which is not a constant - see Movers...

When pickup is taken by a pawn (Owner != None) sound can be removed too...
Sound can be off for default class but this class being capable to assign itself sound if has no owner in a few ticks later. If item spawned is a copy Owned, won't make noise.

In other hand
An external actor spawned at item location will make noise if closer item is visible and removing sound when closer item is hidden. Why only item would need sound ? Any other actor can make noise around. For me this is the solution because it's more easy for me. When this is taken won't do anything, but if said item goes in state pickup, tracking actor will start making noise.

Code: Select all

if (MyItem == None || MyItem.bDeleteMe)
{
//	Disable('Timer');
//	Disable('Tick');
//	GoToState('');
	return;
}
if (MyItem.bHidden)
{
	AmbientSound = None;
	Foo1();
}
else
{
	AmbientSound = WaitingSound;
	Foo2();
}

User avatar
OjitroC
Godlike
Posts: 2334
Joined: Sat Sep 12, 2015 8:46 pm

Re: Sound while Inventory item is a Pickup

Post by OjitroC » Sat Feb 06, 2021 11:02 am

This is from XPickups.BattleSuit - with GroundSound being the sound the pickup makes on the pickup base

Code: Select all

simulated function Tick(float DeltaTime)
{
	if (!bHidden && Pawn(Owner) == None && AmbientSound != GroundSound)
		AmbientSound = GroundSound;
	else if (bHidden && AmbientSound == GroundSound)
		AmbientSound = None;

	if ((Region.Zone.bDestructive || Region.Zone.bKillZone || Region.Zone.DamagePerSec > 0) && Pawn(Owner) == None)
	{
		TimeActive = 0;
		Destroy();
	}
}

User avatar
Barbie
Godlike
Posts: 2069
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: Sound while Inventory item is a Pickup

Post by Barbie » Sat Feb 06, 2021 7:22 pm

Thanks for all answers. :) All these use polling while I'll prefer an event-driven algorithm. But if that isn't possible...
Last edited by Barbie on Sun Feb 07, 2021 12:04 am, edited 1 time in total.
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett

User avatar
sektor2111
Godlike
Posts: 5209
Joined: Sun May 09, 2010 6:15 pm
Location: On the roof.

Re: Sound while Inventory item is a Pickup

Post by sektor2111 » Sat Feb 06, 2021 11:25 pm

Actor helper can have any sort of handling, I think... using bools and checking them, toggling them, these are fast.

User avatar
Feralidragon
Godlike
Posts: 5300
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: Sound while Inventory item is a Pickup

Post by Feralidragon » Sun Feb 07, 2021 12:16 am

The first thing to note is that the code in XPickups was developed at a time when I barely had any understanding of how inventory worked in UT, and even any proper understanding of coding in general, so a lot that you're going to see there is not at all the best way to do something (at most it only shows that it's possible).

The second thing to note is that whenever you have a question about coding, it's good to show the code you have issues with.
You mentioned that you used those functions, which does seem the right direction, but you said it didn't work, and without looking at how you used those it's hard to say why it didn't work.

The third thing to note is that whenever something "doesn't work", you should first test if your initial assumptions are correct, going back to the basics, namely finding out whether it doesn't work because the sound isn't played, or it doesn't work because the functions you used are not called at all to begin with for some reason.

From there, if you see that the functions are not called at all (let's say you also add a log, and you don't see any entry in the log file), then you need to figure out why, with one way being by checking the source code:
https://www.madrixis.de/undox/classtree.html

It's very handy to have a bookmark to a site with the full UT99 source code in UnCodeX format, and it's extremely easy to navigate it and understand it this way.
You can also have the source code locally in a proper editor or IDE, and start analyzing it there too (you have the advantage of doing complete text search to see where the function you're interested in is called from and so forth).

That is to say that your question in this case is easily debuggable, you just need to check why your method doesn't work rather than just move on to a suboptimal method that does.
You will learn a bunch doing it that way, and you will end up doing it efficiently like you want to.

User avatar
Barbie
Godlike
Posts: 2069
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: Sound while Inventory item is a Pickup

Post by Barbie » Sun Feb 07, 2021 12:59 pm

Thanks for the helpfully meant words. Extracting the UC source tree was one of the first things I did when I started developing for UT several years ago (see pic). And of course I add debug statements like BroadcastMessage("Function XYZ entered with parameters=..:") when something does not work as I expected. Searching for a solution may have a learning effect, but my life time is limited and so I have to consider the time I spend solving a little problem. And in this case it was not worth more than several hours spending on it for me.

I also thought about posting my code here but I followed multiple different approaches and so my code looked in this way. :lol2:

Finally I solved it by polling:

Code: Select all

class RedeemerDetector expands Keypoint;
var Sound SavedAmbientSound;
var() float TimerInterval;
var() byte ScanRadius;



event PostBeginPlay() {
	Super.PostBeginPlay();
	SavedAmbientSound = AmbientSound;
	SetTimer(TimerInterval, true);
}



event Timer() {
local WarheadLauncher Redeemer;
local bool NeedSound;

	NeedSound = false;
	ForEach RadiusActors(class'WarheadLauncher', Redeemer, ScanRadius)
		NeedSound = NeedSound || ! Redeemer.bHidden;

	SetAmbientSound(NeedSound);
}


Event SetAmbientSound(bool bSwitchOn) {

	if (bSwitchOn)
	{
		if (AmbientSound == None)
			AmbientSound = SavedAmbientSound;
	}
	else
		if (AmbientSound != None)
			AmbientSound = None;
}


defaultproperties {
   TimerInterval=1.05
    ScanRadius=16
    bStatic=False
    AmbientSound=Sound'Ambancient.Looping.adrum2'
    SoundRadius=16
}
PS: I started coding in the mid 1980th with BASIC, ASM, C and Turbo Pascal. 8)
You do not have the required permissions to view the files attached to this post.
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett

User avatar
sektor2111
Godlike
Posts: 5209
Joined: Sun May 09, 2010 6:15 pm
Location: On the roof.

Re: Sound while Inventory item is a Pickup

Post by sektor2111 » Sun Feb 07, 2021 2:07 pm

So... the solution with an actor attached looks suitable. No doubts... I used this way for multiple purposes and... it works:
- Disabling a Waypoint - enabling it triggered
- lock/unlock paths;
- help for A.I. pawn;
- X etc.