I have two my own classes which i created (Lamp and Button) and a default Character class. In the Character class I created Use method which is binding to the E keyboard. In the Lamp class I created ToggleLight method wchich turn on and turn off light. Finally in the Button class I created OnOverlapBegin and OnOverLapEnd methods:
Now i would like to make possibility that this Character can turn on or turn off light using button but I have no idea how to do that becouse I don’t know how to using another objects in another classes. I think that I should make any pointer to another object but I need any example how to do that.
I wouldn’t necessarily do it with OnOverlapBegin. I would implement OnUse function on the button.
Then on the Character Use function I would do something like this:
float flBest = 100.0f;
class AOnOFfButton *pBest = NULL;
for (TActorIterator<AOnOFfButton> ButtonItr(GetWorld()); ButtonItr; ++ButtonItr)
{
class AOnOFfButton *pButton = *ButtonItr;
float flDist = FVector::Dist(pButton->GetActorLocation(), GetActorLocation());
if (flDist < flBest)
{
flBest = flDist;
pBest = pButton;
}
}
if (pBest)
{
pBest->OnUse();
}
In the button I would define the target that it wants to toggle on and off. You could make a base class for the lamp (for example I have AItemBase class that is the base for all interactive objects in the game) and give it a virtual OnUsed() function. But in your case right now you can just add this to your buttons header:
private:
UPROPERTY(EditInstanceOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
ALamp *LampTarget;
This will allow you to edit LampTarget in the Details panel when selecting the button in the level editor.
Then in your button’s OnUse()
void AOnOFfButton::OnUse()
{
if (LampTarget)
LampTarget->ToggleLight();
}
Thanks a lot! It works. My problem was that I didn’t know that I can set ObjectTarget in level editor And first time I see this function (TActorIterator<AOnOFfButton> ButtonItr(GetWorld()); ButtonItr; ++ButtonItr) it will be very useful for another projects