[zscript] Return a string from an ACS array

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!)
User avatar
Cyanide
Spotlight Team
Posts: 317
Joined: Fri Feb 15, 2013 9:39 pm
Location: 6 ft. under

[zscript] Return a string from an ACS array

Post by Cyanide »

I'm currently trying to convert some stuff in my mod from DECORATE to ZScript. This requires, that at some point in my Zscript I have to return a string from an array I've defined in an ACS script. At one point the Zscript has to choose a random index in the array, and at another point it must use a specific index defined by a constant in the ACS.

The ACS array and the constant looks like this.

Code: Select all

global int 1:zombie_random;

str arrayZombie[16] = {"CyanZombieman2", "CyanZombieMarine", "CyanPistolZombie", "CyanNailBorg", "CyanCyberspectreDemon", "CyanDuneWarrior", "CyanExterminator",	"CyanFemaleZombie", "CyanRapidFireTrooper", "CyanRifleGuy", "CyanJudgmentZombie", "CyanGothicZombie", "CyanGuard", "CyanShadowTrooper", "CyanAndroid", "CyanQuakeGrunt")

script "ZombiePicker" OPEN
{
	zombie_random = Random(0, 15);
}
The Zscript is the SS_ScriptedSpawner by Blue Shadow and it looks like this.

Code: Select all

class FatsoReplacer : SS_ScriptedSpawner replaces Fatso
{
	override name SS_GetActorToSpawn ()
	{
		if(GetCVar("chaos_mode"))
		{
			return '';
		}
		// if chaos_mode is false
		return '';
	}
}
My problem is, that I don't know what to write after the two "return" commands in the Zscript. I've tried this:

Code: Select all

class FatsoReplacer : SS_ScriptedSpawner replaces Fatso
{
   override name SS_GetActorToSpawn ()
   {
      if(GetCVar("chaos_mode"))
      {
         return arrayZombie[Random(0, 15)];
      }
      // if chaos_mode is false
      return arrayZombie[zombie_random];
   }
}
This gives me this error:

Code: Select all

Script error, "SoD_v1,0,1_TEST.pk3:zscript/shades/replacers.txt" line 7:
Unknown identifier 'arrayZombie'
Script error, "SoD_v1,0,1_TEST.pk3:zscript/shades/replacers.txt" line 10:
Unknown identifier 'arrayZombie'
I am completely new to Zscript, so this might be a very basic thing I'm missing.

Thanks in advance. :)
User avatar
ZZYZX
 
 
Posts: 1382
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

Re: [zscript] Return a string from an ACS array

Post by ZZYZX »

What are you trying to do? ZScript means that ACS should be removed from monster code entirely. So it would help to know what's the point of the code instead of "I want to unlink ACS from DECORATE and then link it to ZScript".
User avatar
Cyanide
Spotlight Team
Posts: 317
Joined: Fri Feb 15, 2013 9:39 pm
Location: 6 ft. under

Re: [zscript] Return a string from an ACS array

Post by Cyanide »

ZZYZX wrote:What are you trying to do? ZScript means that ACS should be removed from monster code entirely. So it would help to know what's the point of the code instead of "I want to unlink ACS from DECORATE and then link it to ZScript".
I want to make the Zscript ScriptedSpawner do this:
  • Spawn a random zombie from the array if the CVar "chaos_mode" is true.
  • Spawn a specific zombie from the array based on int zombie_random if CVar "chaos_mode" is false.
User avatar
Arctangent
Posts: 1235
Joined: Thu Nov 06, 2014 1:53 pm

Re: [zscript] Return a string from an ACS array

Post by Arctangent »

Could you describe it in, like, what you're aiming to do? Because we can read the code, but not your intentions.

Best I can tell, you want to make it so that each map only has one kind of replacement per monster in normal play, but with an optional "chaos mode" that instead just does your typical randomizer deal, yes?
User avatar
Cyanide
Spotlight Team
Posts: 317
Joined: Fri Feb 15, 2013 9:39 pm
Location: 6 ft. under

Re: [zscript] Return a string from an ACS array

Post by Cyanide »

Arctangent wrote:Could you describe it in, like, what you're aiming to do? Because we can read the code, but not your intentions.

Best I can tell, you want to make it so that each map only has one kind of replacement per monster in normal play, but with an optional "chaos mode" that instead just does your typical randomizer deal, yes?
Exactly!
User avatar
ZZYZX
 
 
Posts: 1382
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

Re: [zscript] Return a string from an ACS array

Post by ZZYZX »

Code: Select all

class ZombieRandomizer : EventHandler
{
  static const string arrayZombie[] = {"CyanZombieman2", "CyanZombieMarine", "CyanPistolZombie", "CyanNailBorg", "CyanCyberspectreDemon", "CyanDuneWarrior", "CyanExterminator",   "CyanFemaleZombie", "CyanRapidFireTrooper", "CyanRifleGuy", "CyanJudgmentZombie", "CyanGothicZombie", "CyanGuard", "CyanShadowTrooper", "CyanAndroid", "CyanQuakeGrunt"};
  int GlobalZombieRandom;

  static string GetZombieToSpawn()
  {
    ZombieRandomizer mrand = ZombieRandomizer(Find('ZombieRandomizer'));
    if (!mrand.GlobalZombieRandom)
      mrand.GlobalZombieRandom = random(0, sizeof(ZombieRandomizer.arrayZombie)-1)+1;
    if (CVar.GetCVar("chaos_mode").GetBool())
      return ZombieRandomizer.arrayZombie[random(0, sizeof(ZombieRandomizer.arrayZombie)-1)];
    else return ZombieRandomizer.arrayZombie[mrand.GlobalZombieRandom-1];
  }
}
Then add it to MAPINFO as AddEventHandlers under GameInfo.
https://zdoom.org/wiki/Events_and_handlers

You can then use ZombieRandomizer.GetZombieToSpawn() to receive a class name to create.

A new ZombieRandomizer instance will be created for every new map, thus each map will receive own randomized zimba.
User avatar
Cyanide
Spotlight Team
Posts: 317
Joined: Fri Feb 15, 2013 9:39 pm
Location: 6 ft. under

Re: [zscript] Return a string from an ACS array

Post by Cyanide »

ZZYZX wrote:<sexy code>

Then add it to MAPINFO as AddEventHandlers under GameInfo.
https://zdoom.org/wiki/Events_and_handlers

You can then use ZombieRandomizer.GetZombieToSpawn() to receive a class name to create.

A new ZombieRandomizer instance will be created for every new map, thus each map will receive own randomized zimba.
Thanks! I'll give it a shot! :D
User avatar
Cyanide
Spotlight Team
Posts: 317
Joined: Fri Feb 15, 2013 9:39 pm
Location: 6 ft. under

Re: [zscript] Return a string from an ACS array

Post by Cyanide »

So now i've done this:

Code: Select all

class ZombieRandomizer : EventHandler
{
	int GlobalZombieRandom;
	static const string arrayZombie[] = {"CyanZombieman2", "CyanZombieMarine", "CyanPistolZombie", "CyanNailBorg", "CyanCyberspectreDemon", 
										"CyanDuneWarrior", "CyanExterminator",   "CyanFemaleZombie", "CyanRapidFireTrooper", "CyanRifleGuy", 
										"CyanJudgmentZombie", "CyanGothicZombie", "CyanGuard", "CyanShadowTrooper", "CyanAndroid", 
										"CyanQuakeGrunt"};
  static string GetZombieToSpawn()
  {
    ZombieRandomizer mrand = ZombieRandomizer(Find('ZombieRandomizer'));
    if(!mrand.GlobalZombieRandom)
		mrand.GlobalZombieRandom = random(0, sizeof(ZombieRandomizer.arrayZombie)-1)+1;
    if(CVar.GetCVar("chaos_mode").GetBool())
		return ZombieRandomizer.arrayZombie[random(0, sizeof(ZombieRandomizer.arrayZombie)-1)];
    else return ZombieRandomizer.arrayZombie[mrand.GlobalZombieRandom-1];
  }
}

class ZombiemanReplacer : SS_ScriptedSpawner replaces Zombieman
{
	override name SS_GetActorToSpawn()
	{
		return ZombieRandomizer.GetZombieToSpawn();
	}
}
Now I'm getting an error when I start a new game.

Code: Select all

VM execution aborted: array access out of bounds. Max. index = 16, current index = 91
The current index varies, and sometimes it works like it should. I'm guessing it works when the current index is <= 16?
User avatar
ZZYZX
 
 
Posts: 1382
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

Re: [zscript] Return a string from an ACS array

Post by ZZYZX »

Try putting 16 instead of sizeof. (or more specifically random(0, 15))
I have no fucking idea how to retrieve the length of a [] array in the code, if sizeof doesn't work.
User avatar
phantombeta
Posts: 2051
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

Re: [zscript] Return a string from an ACS array

Post by phantombeta »

I think it's "<array>.Size ()" like dynamic arrays. Sizeof is likely giving you the actual size of the array in bytes. (as it should)
User avatar
krokots
Posts: 257
Joined: Tue Jan 19, 2010 5:07 pm

Re: [zscript] Return a string from an ACS array

Post by krokots »

phantombeta wrote:I think it's "<array>.Size ()" like dynamic arrays. Sizeof is likely giving you the actual size of the array in bytes. (as it should)
It is .Size(), i'm using this in my code and it works :wink:
User avatar
ZZYZX
 
 
Posts: 1382
Joined: Sun Oct 14, 2012 1:43 am
Location: Ukraine

Re: [zscript] Return a string from an ACS array

Post by ZZYZX »

:roll: I tried "Size" first but then it said there was invalid expression to the left side and that there is no such member. idk. Might have something gone wrong locally.
User avatar
Cyanide
Spotlight Team
Posts: 317
Joined: Fri Feb 15, 2013 9:39 pm
Location: 6 ft. under

Re: [zscript] Return a string from an ACS array

Post by Cyanide »

Couldn't get .Size() to work right, but specifying the max value worked fine. Thanks a lot!

Return to “Scripting”