Page 1 of 1

[zscript] Return a string from an ACS array

Posted: Wed Jan 10, 2018 9:26 am
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. :)

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

Posted: Wed Jan 10, 2018 11:17 am
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".

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

Posted: Wed Jan 10, 2018 11:34 am
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.

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

Posted: Wed Jan 10, 2018 11:38 am
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?

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

Posted: Wed Jan 10, 2018 11:43 am
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!

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

Posted: Wed Jan 10, 2018 12:11 pm
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.

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

Posted: Wed Jan 10, 2018 12:25 pm
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

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

Posted: Wed Jan 10, 2018 12:56 pm
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?

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

Posted: Wed Jan 10, 2018 2:06 pm
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.

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

Posted: Wed Jan 10, 2018 7:16 pm
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)

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

Posted: Thu Jan 11, 2018 3:11 am
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:

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

Posted: Thu Jan 11, 2018 3:22 am
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.

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

Posted: Thu Jan 11, 2018 11:54 am
by Cyanide
Couldn't get .Size() to work right, but specifying the max value worked fine. Thanks a lot!