Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
OFFICIALDOOMGUY

How do I create random integers in GZDOOM Builder?

Recommended Posts

I'm writing a script for an RPG based WAD in GZDoom Builder, and naturally, one of the scripts creates an empty integer for credits:

"int credits = 0;"

So far I've set it so that when credits are picked up, this script is run:

"credits++;
print(s:"Credits:", d: credits);"

But instead of just making it add one each time, I want it to add a random amount between 1 and 10. I did try to use "credits = credits + (1,10);" but that didn't work, and I did it before but lost the script, so I was wondering how this might be done.

Share this post


Link to post

Did you #include "zcommon.acs" at the beginning of the SCRIPTS file? If yes and it still doesn't work, post your script.

Share this post


Link to post

Yes, I have. I think if I hadn't my question would probably be more along the lines of "My scripts don't seem to work, but I don't know why?" Anyway. Here is my script:

"#include "zcommon.acs"

int quest1Active = 0;

int credits = 0;
int quest1Count = 0;

script 1 ENTER
{
ClearInventory();
SetFont("BIGFONT");
HudMessage(s:"Press your use key to talk to people.",
s:"\n You can earn money buy picking up armour bonuses or completing quests.";
HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
0.05, 2.0);

}

script 2 (void)
{

SetFont("BIGFONT");
HudMessage(s:"Marine: Welcome to Moonbase Omega.",
s:"\n Each area is known as a sector, and is given a sector number. All sectors and their respective numbers have been marked on your map",
s:"\n Please refrain from committing crimes. It will cost you money and time.",
s:"\n I will just get you your starter pack.";
HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
0.05, 2.0);
delay(800);
GiveInventory("FlashLightCentered", 1);
SetFont("BIGFONT");
HudMessage(s:"You have a flashlight!";
HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_TAN, 1.5, 0.8, 5.0,
0.05, 2.0);
delay(250);
HudMessage(s:"Enjoy your stay here at Moonbase Omega, citizen.";
HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
0.05, 2.0);
}

script 3 (void)
{
if (quest1Active == 0)
{
quest1Active = 1;
SetFont("BIGFONT");
HudMessage(s:"Guard: Damn, the generators are low again, just when the power manager is off...",
s:"\n I can't leave my shift if I want to keep my job, though.",
s:"\n Could you check the generators? They're in S5.",
s:"\n Show this to the guard outside S4 to gain access to the generators.",
s:"\n When you're finished, return to me.";
HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
0.05, 2.0);
}

else if (quest1Active == 1)
{
SetFont("BIGFONT");
HudMessage(s:"Guard: I don't hear them generators, friend.";
HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
0.05, 2.0);
}

else if (quest1Active == 3)
{
quest1Active = 5;
SetFont("BIGFONT");
HudMessage(s:"Guard: Thanks! We've got power now. Have some credits, friend.";
HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
0.05, 2.0);
credits = credits + 150;
}

else if (quest1Active == 5)
{
print(s:"Guard: Stay safe.");
}
}

script 4 (void)
{
if (quest1Active == 0)
{
SetFont("BIGFONT");
HudMessage(s:"You: Give me that key!",
s:"\n Guard: Sorry, no civilian access.";
HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
0.05, 2.0);
}

else if (quest1Active == 1)
{
quest1Active = 2;
SetFont("BIGFONT");
HudMessage(s:"You: I need that key to repair the generators.",
s:"\n Guard: Fine, here. Don't take too long in there,",
s:"\n that place is full of radiation.",
s:"\n Powerfull stuff that'll kill you. Gamma rays, beta rays, alpha rays, The lot of 'em.",
s:"\n If you want to survive that place, get a hazard suit in S3, the shopping district.",
s:"\n They sell weapons, armour, all sorts in that place. Good luck in there.";
HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
0.05, 2.0);
delay(800);
GiveInventory("BlueCard", 1);
SetFont("BIGFONT");
HudMessage(s:"You have a blue key!";
HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_TAN, 1.5, 0.8, 5.0,
0.05, 2.0);
}
}

script 5 (void)
{
print(s:"");
}

script 6 (void)
{
credits = (credits + 1, 10);
print(s:"Credits: ", d: credits);
}

script 7 (void)
{
setmusic("D_E1M3",0,0);
setfont("bigfont");
hudmessage(s:"RECEPTION";
HUDMSG_FADEINOUT,35,CR_WHITE,0.5,0.9,5.0,2.0,2.0);
}

script 8 (void)
{
Sector_SetColor(const:3, 255, 255, 255);
}

script 9 (void)
{
if (quest1Active == 2)
{
quest1Count++;
SetFont("BIGFONT");
HudMessage(s:"Generators activated.";
HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
0.05, 2.0);
if (quest1Count == 1)
{
quest1Active = 3;
SetFont("BIGFONT");
HudMessage(s:"Generators restored, return to the guard in S1";
HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
0.05, 2.0);
}
}

else
{
SetFont("BIGFONT");
HudMessage(s:"Nothing happens";
HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
0.05, 2.0);
}
}

script 11 (void)
{
print(s:"You: I just need to press that button...");
}

script 12 (void)
{
print(s:"");
}

script 13 (void)
{
print(s:"");
}

script 14 (void)
{
print(s:"");
}

script 15 (void)
{
print(s:"");
}

script 16 (void)
{
print(s:"");
}

script 17 (void)
{
print(s:"");
}

script 18 (void)
{
print(s:"");
}"

Share this post


Link to post
OfficialDOOMGuy said:

Here is my script:

I couldn't find the part where you use Random.

EDIT: This should work:

credits = credits + Random(1, 10);
Which is the same as:
credits += Random(1, 10);

Share this post


Link to post

I did try to use the "credits = credits + Random(1, 10);" but it had a syntax error, this was before I started this thread., so I'm guessing I just typed something wrong. Thanks for the help.

Share this post


Link to post

Completely a shot in the dark here. I'm relatively new to ACS, so forgive me if I say anything obvious.

What if, at the beginning of Script 6 you had it come up with a random number and save it as a variable, and then use that variable?

script 6 (void)
{
random_number = random(1,10);
credits += random_number;
print(s:"Credits: ", d: credits);
}

Again, I'm still learning Doom scripting a bit. Good luck.

Share this post


Link to post

Please use the [code] [/code] tags when posting code; it'll be easier for everyone.

Also, if your code is long, put it in a spoiler box with [spoiler] [/spoiler] tags so that we don't have to scroll through all of it every time.

Like this:

Spoiler

"#include "zcommon.acs"

int quest1Active = 0;

int credits = 0;
int quest1Count = 0;

script 1 ENTER
{
    ClearInventory();
	SetFont("BIGFONT");
    HudMessage(s:"Press your use key to talk to people.",
	s:"\n You can earn money buy picking up armour bonuses or completing quests.";
    HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
    0.05, 2.0);
    
}

script 2 (void)
{
    
	SetFont("BIGFONT");
    HudMessage(s:"Marine: Welcome to Moonbase Omega.",
	s:"\n Each area is known as a sector, and is given a sector number. All sectors and their respective numbers have been marked on your map",
	s:"\n Please refrain from committing crimes. It will cost you money and time.",
	s:"\n I will just get you your starter pack.";
    HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
    0.05, 2.0);
	delay(800);
    GiveInventory("FlashLightCentered", 1);
    SetFont("BIGFONT");
    HudMessage(s:"You have a flashlight!";
    HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_TAN, 1.5, 0.8, 5.0,
    0.05, 2.0);
	delay(250);
	HudMessage(s:"Enjoy your stay here at Moonbase Omega, citizen.";
    HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
    0.05, 2.0);
}

script 3 (void)
{
    if (quest1Active == 0)
    {
        quest1Active = 1;
		SetFont("BIGFONT");
        HudMessage(s:"Guard: Damn, the generators are low again, just when the power manager is off...",
		s:"\n I can't leave my shift if I want to keep my job, though.",
		s:"\n Could you check the generators? They're in S5.",
		s:"\n Show this to the guard outside S4 to gain access to the generators.",
		s:"\n When you're finished, return to me.";
        HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
        0.05, 2.0);
    }
    
    else if (quest1Active == 1)
    {
		SetFont("BIGFONT");
        HudMessage(s:"Guard: I don't hear them generators, friend.";
		HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
		0.05, 2.0);
    }
    
    else if (quest1Active == 3)
    {
        quest1Active = 5;
		SetFont("BIGFONT");
		HudMessage(s:"Guard: Thanks! We've got power now. Have some credits, friend.";
		HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
		0.05, 2.0);
        credits = credits + 150;
    }
    
    else if (quest1Active == 5)
    {
        print(s:"Guard: Stay safe.");
    }
}

script 4 (void)
{
    if (quest1Active == 0)
    {
		SetFont("BIGFONT");
        HudMessage(s:"You: Give me that key!",
		s:"\n Guard: Sorry, no civilian access.";
        HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
        0.05, 2.0);
    }
    
    else if (quest1Active == 1)
    {
		quest1Active = 2;
		SetFont("BIGFONT");
        HudMessage(s:"You: I need that key to repair the generators.",
		s:"\n Guard: Fine, here. Don't take too long in there,",
		s:"\n that place is full of radiation.",
		s:"\n Powerfull stuff that'll kill you. Gamma rays, beta rays, alpha rays, The lot of 'em.",
		s:"\n If you want to survive that place, get a hazard suit in S3, the shopping district.",
		s:"\n They sell weapons, armour, all sorts in that place. Good luck in there.";
        HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
        0.05, 2.0);
		delay(800);
        GiveInventory("BlueCard", 1);
        SetFont("BIGFONT");
        HudMessage(s:"You have a blue key!";
        HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_TAN, 1.5, 0.8, 5.0,
        0.05, 2.0);
    }
}

script 5 (void)
{
    print(s:"");
}

script 6 (void)
{
    credits = (credits + 1, 10);
    print(s:"Credits: ", d: credits);
}

script 7 (void)
{
    setmusic("D_E1M3",0,0);
    setfont("bigfont");
    hudmessage(s:"RECEPTION";
    HUDMSG_FADEINOUT,35,CR_WHITE,0.5,0.9,5.0,2.0,2.0);
}

script 8 (void)
{
    Sector_SetColor(const:3, 255, 255, 255);
}

script 9 (void)
{
    if (quest1Active == 2)
    {
		quest1Count++;
		SetFont("BIGFONT");
        HudMessage(s:"Generators activated.";
        HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
        0.05, 2.0);
        if (quest1Count == 1)
        {
            quest1Active = 3;
			SetFont("BIGFONT");
			HudMessage(s:"Generators restored, return to the guard in S1";
			HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
			0.05, 2.0);
        }
    }
    
    else
    {
	SetFont("BIGFONT");
    HudMessage(s:"Nothing happens";
    HUDMSG_TYPEON | HUDMSG_LOG, 0, CR_GREEN, 1.5, 0.8, 5.0,
    0.05, 2.0);
    }
}

script 11 (void)
{
    print(s:"You: I just need to press that button...");
}

script 12 (void)
{
    print(s:"");
}

script 13 (void)
{
    print(s:"");
}

script 14 (void)
{
    print(s:"");
}

script 15 (void)
{
    print(s:"");
}

script 16 (void)
{
    print(s:"");
}

script 17 (void)
{
    print(s:"");
}

script 18 (void)
{
    print(s:"");
}"

Share this post


Link to post
droscoe said:

What if, at the beginning of Script 6 you had it come up with a random number and save it as a variable, and then use that variable?

Yes, that's perfectly possible. Only you need to make sure the variable was declared before (or at the same time as) it was assigned a value into. It can be declared in any scope, but it's best to use script scope whenever possible. For example, the variable "credits" needs to be declared on map scope because it needs to preserve its value after calling the script repeatedly, but the variable "random_number" doesn't need to preserve its value between script calls and therefore it can be declared on script scope.

So, unless you want to reuse the value of "random_number" in later script calls, don't declare it at map scope and declare it at script scope instead, like this: (notice the word "int")

script 6 (void)
{
    int random_number = random(1,10);
    credits += random_number;
    print(s:"Credits: ", d: credits);
}

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×