Jump to content

Issue with creating own function


Recommended Posts

Hello Doom world! 
Some time ago i started to learning ZScript. After a while working on project i run into a problem with code itself.
I've tried to create my own function with adjusted integer variable. Where it's increases global variable for specific amount (HyperAmount) If that variable is greater than 100 it's deploying specific message. When i run GZDoom i have errors for 8th and 11th lines:

Quote

8th line:
Unexpected ')'
Expecting ';'

11th line:
Unexpected '>'
Expecting '-' or '+' or '++' or '--' or '(' or identifier or string constant or integer constant or 'super' or '~' or '!' or 'sizeof' or 'alignof' or unsigned constant or float constant or name constant or 'false' or 'true' or 'null'

Here is my function that i've made:

class NewWeapon : Weapon{
    States{
        void A_Hyper(int HyperAmount){ //First error
            let g = HyperVariables.Get();
            if (g.HyperStack > 100){ //Second error
                Console.Printf("Hit Hyper max value!");
            }
            else{
                g.HyperStack += HyperAmount;
                Console.Printf("My given Hyper equal: %d" + HyperAmount); 
            }
        }
    }
}

Hope someone will help me :)

Share this post


Link to post
6 hours ago, jaeden said:

Functions (non-anonymous) should be defined outside the States block.


Thanks you so much! Your solution helped me :)
ZScript feels kinda funky after Unity, haha

Share this post


Link to post

Well, while creating mode i AGAIN run into an issue
This time i cant use global variable inside statusbar class
It's gives me "Can't call play function Get from ui context". I guess Statusbar don't want to work with global variables..?
Here is code from HUD:
 

Spoiler



class myStatusBar : BaseStatusBar
{
	
	int shownHp;
	override void Init(){
		super.Init();
		
		SetSize(0,320,200);
		shownHp = 0;
		
	}
	
	override void Tick(){
		int health = CPlayer.mo.health;
		
		Super.Tick();

	}
	
	override void Draw(int state, double TicFrac){
		Super.Draw (state, TicFrac);
		fullscreenOffsets = true;	
		int health = CPlayer.mo.health;
		int my_armor = GetArmorAmount();
		let g = HyperVariables.Get();
		HUDFont bigFont = HUDFont.Create("BIGFONT2");
		
		
		DrawString(bigFont, FormatNumber(health),(40,-25));
		DrawString(bigFont, FormatNumber(my_armor),(40,-45));
		DrawString(bigFont, FormatNumber(g.HyperStack),(40,-65));
		
		DrawImage("HUD_SMOL", (24, -30));
		DrawImage("HUD_ARBG", (24, -32));
		DrawImage("HUD_AR", (24, -32));
		
		DrawImage("HUD_SMOL", (24, -10));
		DrawImage("HUD_HPBG", (24, -12));
		DrawImage("HUD_HP", (24, -12));

		DrawImage("HUD_SMOL", (-24, -10));
		DrawImage("HUD_HYBG", (-24, -12));
		DrawImage("HUD_HY", (-24, -12));

		DrawString(bigFont, FormatNumber(health),(40,-20));*/
    }
}

And code for global variables itself (If needed):


Class HyperVariables : Thinker
{
int HyperStack;
bool HyperSwitch;
HyperVariables Init(){
        ChangeStatNum(STAT_STATIC);
        return self;
	}
	
static HyperVariables Get()
	{
        let it = ThinkerIterator.Create('HyperVariables', STAT_STATIC);
        HyperVariables cur, prev = null;
    
        while((cur = HyperVariables(it.Next())) != null){
            if (prev != null){
                prev.Destroy();
                }
            prev = cur;
            }
    
        if(prev == null){
            prev = new('HyperVariables').Init();
            } 
    
        return prev;
	}
}

 

Share this post


Link to post

If my assumption is correct, it's the thinker iteration and destruction. You cannot do things that modify the playsim from the UI.

Edited by Edward850

Share this post


Link to post
11 hours ago, Edward850 said:

If my assumption is correct, it's the thinker iteration and destruction. You cannot do things that modify the playsim from the UI.


So its not possible to use global variables in Statusbars?

Share this post


Link to post

Not like that, no. Any variables you handle from the playsim to the UI have to be considered read only. I don't know enough about zscript to say how that's done.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...