Type casting actors/classes and getting spawned missile

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

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
D2JK
Posts: 545
Joined: Sat Aug 30, 2014 8:21 am

Type casting actors/classes and getting spawned missile

Post by D2JK »

I have a couple of questions about 1) type casting and 2) getting actors or classes from missile spawning functions.

1) Suppose a following example...

Code: Select all

Class MyDoomPlayer : DoomPlayer
{
  int myCounter;
  ...
}

Class MyWeapon
{
 (State 1)
 {
   MyDoomPlayer(players[0].mo).myCounter++;
 }
}
While this works, it's a bit lengthy and cryptic if the "MyDoomPlayer(players[0].mo)" - bit has to be used repeatedly when modifying the counter. Ok, I could simplify it this way:

Code: Select all

Class MyWeapon
{
 (State 1)
 {
   let PLR = MyDoomPlayer(players[0].mo);
   PLR.myCounter++;
 }
}
But then PLR couldn't be seen in State 2, so now I would have to repeatedly write the "let PLR = MyDoomPlayer(players[0].mo);" - line. So at this point I was thinking of trying the following:

Code: Select all

Class MyWeapon
{
 MyDoomPlayer PLR;

 override void BeginPlay()
 {
  PLR = MyDoomPlayer(players[0].mo);
  Super.BeginPlay();
 }

 (State 1)
 {
   PLR.myCounter++;
 }

 (State 2)
 {
   PLR.myCounter++;
 }

}
The above would be nice and short (in a way, heh) if it worked. But this produces an error saying "Self pointer used in ambiguous context; VM execution may abort!", and I don't know how to proceed from here. Of course, this is not a huge a problem as I have this working in one way, but it would nice to learn to do what I was attempting in the last code sample, if possible. I am also aware I could create the variable on the weapon itself and then access it using the "invoker." - prefix, but then I'm not sure how I would access and modify a variable on a player's weapon from some completely unrelated actor.

2) Is it possible to easily "get" or "create as" the actor created by A_FireProjectile, similar to what [spawned, mo] = A_SpawnItemEx("MyCoolActor") does, or in some other way?
User avatar
Apeirogon
Posts: 1606
Joined: Mon Jun 12, 2017 12:57 am

Re: Type casting actors/classes and getting spawned missile

Post by Apeirogon »

Dont use
players[0]
because in multiplayer it will break. Use
owner.player.mo
instead.

And a_fireprojectile already return pointer to missile. Or null if missile dont spawn.
D2JK
Posts: 545
Joined: Sat Aug 30, 2014 8:21 am

Re: Type casting actors/classes and getting spawned missile

Post by D2JK »

Dont use players[0] because in multiplayer it will break.
True, I'm aware of that, but that hasn't been a concern so far. But thanks anyway, I know it would be better practice.

Well, actually "MyDoomPlayer(self).myCounter++;" (called within the weapon) seems to work too and is a bit shorter, if that's ok to use.
And a_fireprojectile already return pointer to missile.
Ahh. It never occurred to me to actually try that, as the wiki page didn't mention anything about a return value. Though, I understand that the page was written well before ZScript. Thanks!
gramps
Posts: 300
Joined: Thu Oct 18, 2018 2:16 pm

Re: Type casting actors/classes and getting spawned missile

Post by gramps »

Surely held weapons will already have a reference to the thing holding them. I see "invoker" mentioned in some other threads, is that what you're supposed to use here instead of storing a reference yourself?
User avatar
Apeirogon
Posts: 1606
Joined: Mon Jun 12, 2017 12:57 am

Re: Type casting actors/classes and getting spawned missile

Post by Apeirogon »

No, invoker point to weapon itself. Because self, in case of weapon, means player which "run" states of weapon.
gramps
Posts: 300
Joined: Thu Oct 18, 2018 2:16 pm

Re: Type casting actors/classes and getting spawned missile

Post by gramps »

Ahh, got it, thanks. I wonder where that error message about "self pointer" comes from in that first example where "self" never appeared?
User avatar
Gutawer
Posts: 469
Joined: Sat Apr 16, 2016 6:01 am
Preferred Pronouns: She/Her

Re: Type casting actors/classes and getting spawned missile

Post by Gutawer »

The self pointer is implicitly there when using member variables. If var is a member variable, var will be the same as self.var (at least, if var isn't shadowed). Here it's ambiguous because of old design decisions within the engine, so you have to be more specific if ambiguity would be created.
gramps
Posts: 300
Joined: Thu Oct 18, 2018 2:16 pm

Re: Type casting actors/classes and getting spawned missile

Post by gramps »

Right, it's basically the "this" keyword from Java/C++, isn't it?

So like, if you had a parameter name shadowing a class member in Java, you'd have to write

void setX(int x) { this.x = x; }

where otherwise you can just write

void setX(int _x) { x = _x; }

but in the example in OP, I'm not sure where the ambiguity came from?
Post Reply

Return to “Scripting”