Having trouble with Updating material with SetVectorParameterValue

Hi all,

I’m having some trouble,
I have an actor I’d like to change the colour of depending on which state it’s in, I was trying to use the command SetVectorParameterValue;

If I call it in ‘begin play’ it works,
If I call it in ‘on overlap’ it works,

But if I set one colour in ‘begin play’ and another in ‘on overlap’ it doesn’t change :confused:

I have


MatDynamic->SetVectorParameterValue("InputColour", FVector(0.0f, 1.0f, 1.0f));

In begin play, then:


MatDynamic->SetVectorParameterValue("InputColour", FVector(1.0f, 0.0f, 0.0f));

in on overlap.

Is there something else I should be calling to update the colour?

Could it be that the Actor is colliding or overlapping the world causing it to just be set as the overlap colour immediately after begin play happens?

Or is it set as the BeginPlay colour and then never changing when you collide? If so, then you might not have your overlap event setup correctly.

When I was debugging I had a breakpoint to make sure it was being called when the player ran over it, so I don’t believe the overlap even was being called repeatedly.
Also when there’s a state change there’s a flag stopping the change being re-called.

as far as I can see, the code “MatDynamic->SetVectorParameterValue(“InputColour”, FVector(1.0f, 0.0f, 0.0f));” is being called when it’s meant to, but the change isn’t taking effect.
I was wondering if there was some sort of ‘refresh material instance’ step I was missing.

Are you sure that the MatDynamic has been applied to the Mesh?

You don’t need to call a refresh material as long as the Parameter with that name actually exists. I doubt that this is required but maybe the parameter name requires FName(TEXT(“InputName”)). Don’t think it actually matters to be honest.

well If I put “MatDynamic->SetVectorParameterValue(“InputColour”, FVector(1.0f, 0.0f, 0.0f));” in begin play the mesh changes colour.
It’s just if I call it anywhere else it seems to not be working. :confused:

Ah yes, this is very odd. You’re certain that the the default value of InputColour is not coincidently set as Red? Haha, I’ve made that mistake before.

If you’re sure that the other place that you’re calling the SetVecterParameterValue is being called then I have no idea what it could be. Again, I don’t think it matters but try using FLinearColor instead of FVector.

Maybe also try triggering it elsewhere too, not just the overlap event.

Haha, no, it’s green then turns red.

I’ve put breakpoints in and seen it call it, everything seeming ok. but the colour not updating.
I’ve put it in on overlap, on button press etc, each time the breakpoint hits, but no change.

Could I be forgetting something on the server? (this is a multiplayer game) some replication I’ve just brainfarted over?

Very odd, especially if the breakpoints are being called. Are you sure that the breakpoint is being called on the client? If you’ve got the Client and Server running on the same PC then sometimes it can be hard to tell which it’s being called on. This sounds like it could be the most likely reason at this point. Begin Play is called on Client & Server but the Overlap might not be, and key-presses might also be Local to the server too, causing the breakpoint to fire.

Add this to the same places as you set the Colour, see when it’s called (instead of breakpoints):

GEngine->AddOnScreenDebugMessage( -1, 5.f, (Role == ROLE_Authority) ? TEXT(“Auth”) : TEXT(“Client”) );

Right, so, I’m doing this.
It’s saying ‘Auth’

The function that calls it is:


void ASTrap::ServerSetTrap_Implementation(AActor* OtherActor)
{
	trapState = ETrapState::Set;
	if (MatDynamic)
	{
		GEngine->AddOnScreenDebugMessage(5, 5.0f, FColor::Red, (Role == ROLE_Authority) ? TEXT("Auth") : TEXT("Client"));
		MatDynamic->SetVectorParameterValue("InputColor", FVector(0.0f, 1.0f, 0.0f));
	}
}

Now, I’m fairly new at unreal, so if something feels obvious I may have missed, just let me know :stuck_out_tongue:

If it says Auth, then it means that the colour will change on the server. That function looks like it’s being called from a client to the owning server? The server will then change the colour for it’s own representation but other clients and the client that sent the message will not be recoloured by that function.

You may need to set the colour on the client itself before calling that ServerSetTrap function, then have a bool variable in the ASTrap class with a RepNotify function, ServerSetTrap should set the bool to true which will be sent to all non-owning clients. The notify function when called on clients should set the correct colour.

This will mean that the client that calls the function will have the colour, the server will then have the colour and all other clients will get the colour from the notified function. Take a look at the UE4 networking tutorials and Wiki to see examples of how these are done.

Alright,
I’ll try it out when I get home, I swear I tried it locally though.
I’ve had so much hacking around with this I’ve lost track of what I tried hahaha

That function came around day 2, but I’ll take what you’ve said and see where it gets me :slight_smile:

Oh,
My,
God…

so, I’m like, from scotland…In this country we spell it ‘Colour’
My code says ‘Color’, my Material says ‘Colour’ facepalm

need to undo like, everything to see if that’s it haha

I must admit if I have code that works somewhere and fails somewhere else I’ll often cut 'n paste the working version in to the other area, just to make sure I’m not missing something subtle like this. Of course usually I’m onto my third coffee and have re-written half my code by then…

yeah, I’m not convinced it’s that simple, but, I don’t know.

I ran out of time to fiddle with it last night and get it back into a working state.
I’ll see if there’s any evidence that that was the error in the old commits (the only thing I can do at work)

Turns out it’s not that, I thought that would have been too simple.

It’s clearly to do with the server etc.
The colour changes in ‘Begin Play’ and prints the debug ‘Client’ and every other time prints ‘Auth’ and doesn’t change the colour.
So this is the problem.

I’ve read through the networking wiki, examples etc etc.
I just don’t get it… It seems every which way I call it, it’s always printing ‘Auth’…Something must be setup wrong in another part of my code so it’s not getting down to the client