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

Global variables not working properly - ACS

Recommended Posts

I'm using ACS to test a levelling system in my WAD. Problem is that when an action is preformed by the player that is supposed to change these variables, it doesn't appear to be having any effect at all.  

 

Here is my script:

Spoiler

#include "zcommon.acs"

int playerLevel = 1;
int playerXp = 0;

script 1 ENTER
{
    if (playerXp == 10)
    {
        playerLevel = 2;

print(s:"LEVEL UP!");
    }
}

script 2 OPEN
{
     SetFont("SMALLFONT");
        HudMessage(s:"Level: ",
                    i:playerLevel;
                    HUDMSG_PLAIN, 2, CR_GRAY, 0.1, 0.12, 0);
                    
                     SetFont("SMALLFONT");
        HudMessage(s:"XP: ",
                    i:playerLevel;
                    HUDMSG_PLAIN, 1, CR_GRAY, 0.9, 0.12, 0);
}

script 3 (void)

{
    playerXp = playerXp +1;
}

Quote

script 1 ENTER
{
    if (playerXp == 10)
    {
        playerLevel = 2;

print(s:"LEVEL UP!");
    }
}

 

When the threshold of playerXp (10) is reached, the player levels up and becomes playerLevel 2.

 

Quote

script 2 OPEN
{
     SetFont("SMALLFONT");
        HudMessage(s:"Level: ",
                    i:playerLevel;
                    HUDMSG_PLAIN, 2, CR_GRAY, 0.1, 0.12, 0);
                    
                     SetFont("SMALLFONT");
        HudMessage(s:"XP: ",
                    i:playerLevel;
                    HUDMSG_PLAIN, 1, CR_GRAY, 0.9, 0.12, 0);

 

This is supposed to display both the current level and Xp of the player. The value of both playerLevel and playerXp does not change when the player preforms an action that is supposed to increase it. playerXp stays at 0 and playerLevel stays at 1 on screen.

 

 

Quote

script 3 (void)

{
    playerXp = playerXp +1;
}

 

Increases playerXp by 1. I've assigned this script to a few monsters but killing them doesn't appear to increase the xp at all, and when the threshold is reached theoretically, the player does not level up.

 

Any help will be greatly appreciated :)

Share this post


Link to post

I can assure you 100% that it works.

 

The issue being that you're running the checks and HudMessage only at the start of a level. You're going to want to use while loops to actually run those functions more than once. You'll also want to make script 2 an ENTER script instead of an OPEN one to make sure that it doesn't immediately break as soon as anyone tries to multiplayer in its direction.

Share this post


Link to post

As-is, your OPEN script and your ENTER script will run just once and then stop. So, you are seeing what the level and xp were at the beginning of the map. You need a loop, with a delay so it won't crash. This should work:

 

#include "zcommon.acs"

int playerLevel = 1;
int playerXp = 0;

script 1 ENTER
{
    while (playerLevel == 1)
    {
        if (playerXp == 10)
        {
            playerLevel = 2;
            print(s:"LEVEL UP!");
        }
        Delay (5);
    }
}

script 2 OPEN //maybe ENTER would be better here
{
    while (TRUE)
    {
        SetFont("SMALLFONT");
        HudMessage(s:"Level: ",
                    i:playerLevel;
                    HUDMSG_PLAIN, 2, CR_GRAY, 0.1, 0.12, 0);
        SetFont("SMALLFONT");
        HudMessage(s:"XP: ",
                    i:playerLevel;
                    HUDMSG_PLAIN, 1, CR_GRAY, 0.9, 0.12, 0);
        Delay(5);
    }
}

script 3 (void)

{
    playerXp = playerXp +1;
}

Delay(5) will make the script wait 5 tics (1/7 of a second) before repeating the loop. This should be often enough, while putting a little bit less strain on the computer than Delay(1). If you don't put a delay in there, it will try to repeat the loop infinitely many times in one tic.

 

If you plan on having more than 2 levels, there is probably a more elegant way of coding that, but having a loop for each level in that same script should work.

 

EDIT: Ninja'd!

 

Edited by Empyre : ninja'd, removed unwanted empty lines in code

Share this post


Link to post

@Empyre @Arctangent

 

Thank you very much for the support and advice. The while loops sorted all the technical issues out. But then i figured i made the hud message that was supposed to display the Xp count display the player level by a typo in the code- which explained why nothing happend even after the while loops were added. What a damn fool be me!

 

I tend to use ENTER and OPEN interchangeably, which is bad i know, ive always been confused on the differences between them. Can you clarify please?

 

Also, should i be posting these types of questions that require these types of answers here or in the thread called "editing questions"?

Share this post


Link to post

OPEN ( and REOPEN ) is ran by the world, though in a singleplayer game the player will activate it instead. ENTER ( and RETURN ) instead is run by the player no matter what, which means it'll actually run in several instances when there's several players.

 

Basically, OPEN is for stuff like setting up perpetually moving lifts, setting up script light sequences, and such. ENTER, on the other hand, is for stuff related to the player.

Share this post


Link to post

The difference between ENTER and OPEN scripts is only one: An OPEN script runs upon the map being loaded, only one instance is run in total, and the instance has no activator (therefore action specials in which tag 0 refers to the script's activator will not work properly if you use them with tag 0 in that script). An ENTER script runs upon the player entering the game (in singleplayer, this is always at map start, and in multiplayer, it is whenever the player joins in), one instance is run for each player, and the instance has the respective player as its activator (therefore tag 0 works in it).

 

As for whether to ask questions in Doom Editing or in Editing Questions, I believe it depends purely on whether you personally prefer linear-forum-style or nonlinear-StackOverflow-style arrangement of replies. (I prefer the former, because discussions in the latter are harder to follow when replies are defaultly sorted by number of votes rather than by time.)

Edited by scifista42

Share this post


Link to post
4 hours ago, scifista42 said:

As for whether to ask questions in Doom Editing or in Editing Questions, I believe it depends purely on whether you personally prefer linear-forum-style or nonlinear-StackOverflow-style arrangement of replies. (I prefer the former, because discussions in the latter are harder to follow when replies are defaultly sorted by number of votes rather than by time.)

At the top of a thread (right under the question) in the Editing Questions subforum, there are buttons labeled Sort by Date and Sort by Votes, with the default being Sort by Votes. If you click Sort by Date, it will look more like a normal thread.

Share this post


Link to post

I know, but I don't see any way to make replies sorted by date by default, so I have to remember doing it in every thread, and if I forget to do it, I may easily miss context of the replies at the top.

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
×