[SOLVED] [ZScript] A_SpawnItemEx returns 2 values?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!
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
User avatar
ADudeCalledLeo
Posts: 7
Joined: Mon Mar 09, 2020 4:15 am
Graphics Processor: nVidia with Vulkan support
Location: Beersheba, Israel
Contact:

[SOLVED] [ZScript] A_SpawnItemEx returns 2 values?

Post by ADudeCalledLeo »

According to the wiki, this is A_SpawnItemEx's signature:

Code: Select all

bool, Actor A_SpawnItemEx (class<Actor> missile [, double xofs [, double yofs [, double zofs [, double xvel [, double yvel [, double zvel [, double angle [, int flags [, int failchance [, int tid]]]]]]]]]]) 
So, I assumed this would use Lua/Python-style syntax, and tried this:

Code: Select all

bool succ;
Actor spawned;
succ, spawned = A_SpawnItemEx("Shotgun");
But nope, that doesn't compile.

So my question is, how do I get both return values from this function?
Last edited by ADudeCalledLeo on Mon Mar 09, 2020 7:40 am, edited 2 times in total.
User avatar
MFG38
Posts: 414
Joined: Sun Apr 14, 2019 8:26 am
Graphics Processor: nVidia (Modern GZDoom)
Location: Finland
Contact:

Re: [ZScript] A_SpawnItemEx returns 2 values?

Post by MFG38 »

Try replacing Actor with class<Actor>.
User avatar
ADudeCalledLeo
Posts: 7
Joined: Mon Mar 09, 2020 4:15 am
Graphics Processor: nVidia with Vulkan support
Location: Beersheba, Israel
Contact:

Re: [ZScript] A_SpawnItemEx returns 2 values?

Post by ADudeCalledLeo »

MFG38 wrote:Try replacing Actor with class<Actor>.
That doesn't make sense to me. A_SpawnItemEx says it returns a pointer to the Actor it spawned, why would it return a class?
Besides, the error I got was something along the lines of "unexpected symbol", not "cannot cast X to Y".
User avatar
Cherno
Posts: 1339
Joined: Tue Dec 06, 2016 11:25 am

Re: [ZScript] A_SpawnItemEx returns 2 values?

Post by Cherno »

You have to encase multiple return values, like this:

Code: Select all

bool succ;
Actor spawned;
[succ, spawned] = A_SpawnItemEx("Shotgun");
That being said, you don't have to "catch" any of the return values. You could use none, one, or both if you wish.
User avatar
Tartlman
Posts: 226
Joined: Thu Oct 11, 2018 5:24 am
Location: meme hell
Contact:

Re: [ZScript] A_SpawnItemEx returns 2 values?

Post by Tartlman »

While this question is pretty much answered, there's something to keep in mind:
ZDoom Wiki wrote:A pointer to the spawned actor. If the actor fails to spawn due to chance or massacre, the returned value is null. When testing for space to spawn the actor in, the actor is spawned, tested and then immediately destroyed if it fails the test. The returned value in this case is still a pointer to the actor, not null. The space test is performed on monster-based actors only.
To avoid this headache, I highly recommend that you use spawn() combined with warp(). While the zdoom wiki page for warp() seems to be for ACS, note that it pretty much works the same in zscript except with an actor pointer instead of a TID, as far as I know. And no ugly junk variables in your code!

There are some cases where using spawnitemex is easier, of course.
User avatar
ADudeCalledLeo
Posts: 7
Joined: Mon Mar 09, 2020 4:15 am
Graphics Processor: nVidia with Vulkan support
Location: Beersheba, Israel
Contact:

Re: [ZScript] A_SpawnItemEx returns 2 values?

Post by ADudeCalledLeo »

Cherno wrote:You have to encase multiple return values, like this:

Code: Select all

bool succ;
Actor spawned;
[succ, spawned] = A_SpawnItemEx("Shotgun");
That being said, you don't have to "catch" any of the return values. You could use none, one, or both if you wish.
Thank you!
User avatar
hideousdestructor
Posts: 9696
Joined: Sun Jan 04, 2004 5:37 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Debian Bullseye
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Contact:

Re: [ZScript] A_SpawnItemEx returns 2 values?

Post by hideousdestructor »

Tartlman wrote:To avoid this headache, I highly recommend that you use spawn() combined with warp(). While the zdoom wiki page for warp() seems to be for ACS, note that it pretty much works the same in zscript except with an actor pointer instead of a TID, as far as I know. And no ugly junk variables in your code!

There are some cases where using spawnitemex is easier, of course.
Why warp instead of just spawning at the desired coordinates and then setting pitch/angle? (not rhetorical question, I never use warp and forgot it even existed until now)
User avatar
AFADoomer
Posts: 1346
Joined: Tue Jul 15, 2003 4:18 pm
Contact:

Re: [SOLVED] [ZScript] A_SpawnItemEx returns 2 values?

Post by AFADoomer »

^^ This

Code: Select all

Actor mo = Spawn("Shotgun", pos);
if (mo)
{
  mo.angle = angle;
  mo.pitch = pitch;
// etc....
}
Post Reply

Return to “Scripting”