Jump to content
  • 0

Need code fix (problem with timers)


Kavaline

Question


  • Group:  Members
  • Topic Count:  9
  • Topics Per Day:  0.01
  • Content Count:  18
  • Reputation:   1
  • Joined:  02/15/20
  • Last Seen:  

Hi, I write a script that a NPC have X charges (common for all players), and the players can spent a charge.

After this, the NPC starts a timer (common for all players) to recharge, until their limit of X charges.

Here is:

-	script	TestScript::QuickTest	4_F_03,{
	mes "[Quick Test]";
	mes "What you want?";
	next;
	switch(select("Use Charge", "See Charge", "Exit")) {
	case 1:
		if (.n_now > 0){
			mes "[Quick Test]";
			if (.n_now == .n_max) {
				setnpctimer 0;
				initnpctimer;
                mes "Starting recharges.";
			}
			.n_now -= 1;
			getitem 512, 1;
			mes "Done. Current Charges: "+.n_now+"/"+.n_max+".";
		}
		else{
			mes "[Quick Test]";
			mes "No Charges.";
		}
		close;
	case 2:
		mes "[Quick Test]";
		mes "Next Charge in "+((.cont)/1000)+" seconds.";
		close;
	}
	close;

OnInit:
	set .n_max, 5;		//max charges
	set .n_now, 5;		//actual charge
	set .delay, 10*1000;//time to recover 1 charge
	set .cont, 0;		//actual time left to recharge
	end;
	
OnTimer1000:
	if (.cont <= 0) {
		.cont = .delay;
	}
	else{
		.cont -= 1000;
		stopnpctimer;
		setnpctimer 0;
		if (.cont <= 0) {
			.n_now++;
			if (.n_now == .n_max) end;
			.cont = .delay;
		}
		initnpctimer;
	}
	end;
}

prontera,164,170,4	duplicate(QuickTest)	Test Quick#prt	4_F_03

But I can't make the timer work; the NPC aways says "Next Charge in 10 seconds.", and the charges don't recover.

I will appreciate if someone tell me were is the error, or another easier solution better than npctimer (I use 'OnTimer1000' because others NPCs will have different .delay's, so, I can use the same scope, and the 'addtimer' is attached to the player).

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 1

  • Group:  Members
  • Topic Count:  25
  • Topics Per Day:  0.01
  • Content Count:  924
  • Reputation:   166
  • Joined:  04/05/13
  • Last Seen:  

-	script	TestScript::QuickTest	4_F_03,{
	mes "What you want?";
	next;
	switch(select("Use Charge", "See Charge", "Exit")) {
	case 1:
		if (.n_now > 0){
			.n_now--;
			if (.n_now < .n_max) {
				setnpctimer 0;
				initnpctimer;
				npctalk "Starting recharges.";
			}
			getitem 512, 1;
			.cont = .delay;
			mes "Current C.: "+.n_now+"/"+.n_max;
			mes "Next Charge in "+((.cont)/1000)+"s.";
		}
		else{
			mes "No Charges.";
		}
		close;
	case 2:
		mes "Delay per C.: "+.delay/1000+"s";
		mes "Current C.: "+.n_now+"/"+.n_max;
		mes "Next Charge in "+((.cont)/1000)+"s.";
		close;
	}
	close;

OnTimer4000:
	.n_now++;
	.cont = .delay;
	npctalk "Current C.: "+.n_now+"/"+.n_max;
	stopnpctimer;
	setnpctimer 0;
	if (.n_now >= .n_max)
	end;
	initnpctimer;
	npctalk "Next Charge in "+((.cont)/1000)+"s.";
	end;
	
OnInit:
	.n_max = 5;		//max charges
	.n_now = 5;		//actual charge
	.delay = 4*1000;//time to recover 1 charge
	.cont = 0;		//actual time left to recharge
	end;
}

prontera,164,170,4	duplicate(QuickTest)	Test Quick#prt	4_F_03

Tested.

  • MVP 1
Link to comment
Share on other sites

  • 1

  • Group:  Members
  • Topic Count:  25
  • Topics Per Day:  0.01
  • Content Count:  924
  • Reputation:   166
  • Joined:  04/05/13
  • Last Seen:  

-	script	TestScript::QuickTest	4_F_03,{
	mes "What you want?";
	next;
	switch(select("Use Charge", "See Charge", "Exit")) {
	case 1:
		if (.n_now > 0){
			.n_now--;
			if (.n_now < .n_max) {
				setnpctimer 0;
				initnpctimer;
				mes "Starting recharges.";
			}
			getitem 512, 1;
			mes "Done. Current Charges: "+.n_now+"/"+.n_max+".";
		}
		else{
			mes "No Charges.";
		}
		close;
	case 2:
		mes "Next Charge in "+((.cont)/1000)+" seconds.";
		close;
	}
	close;

OnTimer1000:
	if (.cont <= 0) {
		.cont = .delay;
		.n_now++;
	}
	else{
		.cont -= 1000;
		stopnpctimer;
		setnpctimer 0;
		if (.cont <= 0) {
			.n_now++;
			if (.n_now >= .n_max)
			end;
			.cont = .delay;
		}
		initnpctimer;
	}
	end;
	
OnInit:
	.n_max = 5;		//max charges
	.n_now = 5;		//actual charge
	.delay = 10*1000;//time to recover 1 charge (10,000)
	.cont = 0;		//actual time left to recharge
	end;
}

prontera,164,170,4	duplicate(QuickTest)	Test Quick#prt	4_F_03

Not sure it's work like what do you want but try it first.

  • Upvote 1
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  9
  • Topics Per Day:  0.01
  • Content Count:  18
  • Reputation:   1
  • Joined:  02/15/20
  • Last Seen:  

@Start_

Not full working, same problem: after use one charge (the 5/5 charge), the NPC aways says "Next Charge in 10 seconds.", but now, the charge recover after the right time ?

And, after the second use, he shows the right recharge time. I think that first use when in full charge dont update the .cont. But my idea is: in full charge, start the npctimer, that in this state, give the .count the right countdown time, and after one second, in the OnTimer, update for the player. But looks like he don't update using the 5/5 charge, to tell the player the right time, only after talking again, he update right, and I can't find why.

@edit

The most important is the timer count rightly, the time left message is for help me in debug, so, don't need fix this, because I will remove this showing message. Thanks for the help, the working code is that I need, and is functional.

But, you can explain me why my timer dont start, but start with your edit? I want understand better what I did wrong, and don't repeat the same mistake.

 

On 2/18/2020 at 4:04 PM, Start_ said:

-	script	TestScript::QuickTest	4_F_03,{
	mes "What you want?";
	next;
	switch(select("Use Charge", "See Charge", "Exit")) {
	case 1:
		if (.n_now > 0){
			.n_now--;
			if (.n_now < .n_max) {
				setnpctimer 0;
				initnpctimer;
				npctalk "Starting recharges.";
			}
			getitem 512, 1;
			.cont = .delay;
			mes "Current C.: "+.n_now+"/"+.n_max;
			mes "Next Charge in "+((.cont)/1000)+"s.";
		}
		else{
			mes "No Charges.";
		}
		close;
	case 2:
		mes "Delay per C.: "+.delay/1000+"s";
		mes "Current C.: "+.n_now+"/"+.n_max;
		mes "Next Charge in "+((.cont)/1000)+"s.";
		close;
	}
	close;

OnTimer4000:
	.n_now++;
	.cont = .delay;
	npctalk "Current C.: "+.n_now+"/"+.n_max;
	stopnpctimer;
	setnpctimer 0;
	if (.n_now >= .n_max)
	end;
	initnpctimer;
	npctalk "Next Charge in "+((.cont)/1000)+"s.";
	end;
	
OnInit:
	.n_max = 5;		//max charges
	.n_now = 5;		//actual charge
	.delay = 4*1000;//time to recover 1 charge
	.cont = 0;		//actual time left to recharge
	end;
}

prontera,164,170,4	duplicate(QuickTest)	Test Quick#prt	4_F_03

Tested.

Awesome, much better than what I tried!

Edited by Kavaline
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  25
  • Topics Per Day:  0.01
  • Content Count:  924
  • Reputation:   166
  • Joined:  04/05/13
  • Last Seen:  

You can try debug by npctalk (It's more clear to know) because I forget where it is.

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  9
  • Topics Per Day:  0.01
  • Content Count:  18
  • Reputation:   1
  • Joined:  02/15/20
  • Last Seen:  

35 minutes ago, Start_ said:

You can try debug by npctalk (It's more clear to know) because I forget where it is.

I didn't knew this command, is very useful for debug and feedback, thanks for show me this. Have lots of commands that I don't know, but I'm learning. Slowy, but ok *shrug*

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

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

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

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