I’m trying to create a heartbeat script that will control when a portal is active or not. I looked for functions such as activate and deactivate, but I didn’t find any. I’m pretty new to scripting but I am a Jr. Software Engineer so I know how coding works.
The script will control a portal. If there are enemies around, portal is not active. When there are no enemies around, portal is active.
This is what I have come up with but doesn’t seem to be working.
void main()
{
if (GetNearestCreature(CREATURE_TYPE_IS_ALIVE, PLAYER_CHAR_NOT_PC) == OBJECT_INVALID)
{
SetUseableFlag(OBJECT_SELF, 0);
}
else
{
SetUseableFlag(OBJECT_SELF, 1);
}
}
Your conditional evaluates to TRUE
if there is no non-PC around the portal. Then you deactivate the portal. You need to put the activation under the if
and the deactivation under the else
:
void main()
{
if (GetNearestCreature(CREATURE_TYPE_IS_ALIVE, PLAYER_CHAR_NOT_PC) == OBJECT_INVALID)
{
SetUseableFlag(OBJECT_SELF, 1);
}
else
{
SetUseableFlag(OBJECT_SELF, 0);
}
}
1 Like
That isn’t working.
It only seems to be working when the player is in combat. Like, the portal is active with creatures on the map, as long as the player doesn’t engage he can skip the encounter and go through the portal. Which is what I’m trying to prevent.
IIRC GetNearestCreature first needs a criteria type and then the value the criteria is supposed to have.
Try something like
if (GetNearestCreature(CREATURE_TYPE_PLAYER_CHAR, PLAYER_CHAR_NOT_PC, OBJECT_SELF, 1, CREATURE_TYPE_IS_ALIVE, TRUE) == OBJECT_INVALID)
You’ll probably want to do a distance check somewhere in there, too, unless the area is very small.
How does that feel about henchmen, though? Do those count as non-PCs? Maybe it’d be better to check for hostiles than for non-PCs.
2 Likes
Yeah, that’d be better, I’m creating my own arena module with PvE element to it. Your line seems to be working. Is there a way to disable the visual effect of the portal while it’s not usable?
What are you using for the portal? Some placeables have different animations for activated/deactivated states. You’d want to make them switch via
PlayAnimation(ANIMATION_PLACEABLE_DEACTIVATE);
and
PlayAnimation(ANIMATION_PLACEABLE_ACTIVATE);
1 Like
The portal will be for leaving the area once all the creatures have been killed. It’s just a regular yellow portal. I just tried that PlayAnimation thing and it didn’t seem to work.
It doesn’t seem to work with the yellow portal, but it does with the purple one.
2 Likes
Yeah. Animations are part of the model. Some placeables have on/off or open/close animations, some don’t. :-/
If you’ve got your heart set on the yellow portal, you could switch the appearance of your portal placeable to be an invisible object, and spawn a purely decorative yellow portal placeable in when the portal turns active, and destroy it when it turns inactive.
Indeed it does, I’m gonna try creating the purple portal, and change the appearance to yellow.
EDIT: Seems like the yellow one won’t work like that for some reason. The color of the portal doesn’t really matter to me so I’ll just stick to purple. Thanks guys for the help!
EDIT 2: Actually, how would you check for hostiles? Now that I think of it, what if a user spawns a creature? I wasn’t planning on including henchman, but I just thought of this bug in my logic.
2 Likes
GetNearestCreature, with CREATURE_TYPE_REPUTATION, REPUTATION_TYPE_ENEMY.
Using the PC rather than the portal, so you’ll be looking for enemies of the PC rather than for enemies of the portal placeable. (I think. Disclaimer: Did not test any of this.)
Well if I set the faction of the portal to commoner, the player and the portal should have the same enemies right? I’ll try that.
1 Like
Report the results, please. I’d like to know, too.
1 Like
Yup, seems to be working good. Here’s the final code:
void main()
{
if (GetNearestCreature(CREATURE_TYPE_REPUTATION, REPUTATION_TYPE_ENEMY, OBJECT_SELF, 1, CREATURE_TYPE_IS_ALIVE, TRUE) == OBJECT_INVALID)
{
SetUseableFlag(OBJECT_SELF, 1);
PlayAnimation(ANIMATION_PLACEABLE_ACTIVATE);
}
else
{
SetUseableFlag(OBJECT_SELF, 0);
PlayAnimation(ANIMATION_PLACEABLE_DEACTIVATE);
}
}
2 Likes
Wouldn’t it be easier to simply check if there are any enemies nearby when the portal is clicked/activated?
I understand it is generally a good idea to avoid OnHeartbeat.
Just my 2 copper:
I have nothing against heartbeat but I would rather work with a script in the OnEnter of a trigger zone around the portal (if animated when active maybe put it into a small area that encloses it from 3 sides so the player can only see the portal after the script has activated/deactivated it).