[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.

[zscript] Return a string from an ACS array

Postby Cyanide » Wed Jan 10, 2018 9:26 am

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 allExpand view
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 allExpand view
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 allExpand view
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 allExpand view
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
Cyanide
Spotlight Team
 
Joined: 15 Feb 2013
Location: 6 ft. under

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

Postby ZZYZX » Wed Jan 10, 2018 11:17 am

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
ZZYZX
le chat du rabbin
 
 
 
Joined: 14 Oct 2012
Location: Ukraine
Discord: ZZYZX#1394
Github ID: jewalky

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

Postby Cyanide » Wed Jan 10, 2018 11:34 am

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
Cyanide
Spotlight Team
 
Joined: 15 Feb 2013
Location: 6 ft. under

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

Postby Arctangent » Wed Jan 10, 2018 11:38 am

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
Arctangent
squawky
 
Joined: 06 Nov 2014
Discord: SquawkyAtan#2371

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

Postby Cyanide » Wed Jan 10, 2018 11:43 am

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
Cyanide
Spotlight Team
 
Joined: 15 Feb 2013
Location: 6 ft. under

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

Postby ZZYZX » Wed Jan 10, 2018 12:11 pm

Code: Select allExpand view
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
ZZYZX
le chat du rabbin
 
 
 
Joined: 14 Oct 2012
Location: Ukraine
Discord: ZZYZX#1394
Github ID: jewalky

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

Postby Cyanide » Wed Jan 10, 2018 12:25 pm

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
 
Joined: 15 Feb 2013
Location: 6 ft. under

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

Postby Cyanide » Wed Jan 10, 2018 12:56 pm

So now i've done this:
Code: Select allExpand view
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 allExpand view
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
Cyanide
Spotlight Team
 
Joined: 15 Feb 2013
Location: 6 ft. under

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

Postby ZZYZX » Wed Jan 10, 2018 2:06 pm

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
ZZYZX
le chat du rabbin
 
 
 
Joined: 14 Oct 2012
Location: Ukraine
Discord: ZZYZX#1394
Github ID: jewalky

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

Postby phantombeta » Wed Jan 10, 2018 7:16 pm

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
phantombeta
Tired of being treated like trash by control freaks
 
Joined: 02 May 2013

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

Postby krokots » Thu Jan 11, 2018 3:11 am

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
krokots
 
Joined: 19 Jan 2010

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

Postby ZZYZX » Thu Jan 11, 2018 3:22 am

: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
ZZYZX
le chat du rabbin
 
 
 
Joined: 14 Oct 2012
Location: Ukraine
Discord: ZZYZX#1394
Github ID: jewalky

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

Postby Cyanide » Thu Jan 11, 2018 11:54 am

Couldn't get .Size() to work right, but specifying the max value worked fine. Thanks a lot!
User avatar
Cyanide
Spotlight Team
 
Joined: 15 Feb 2013
Location: 6 ft. under


Return to Scripting

Who is online

Users browsing this forum: No registered users and 0 guests