Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   -now what am i doing wrong? (https://forums.graalonline.com/forums/showthread.php?t=51785)

wonderboysp2p 03-18-2004 03:03 AM

-now what am i doing wrong?
 
ive been nored and working on a rescript of the inventory system and its comin along pretty good

the script works fine except for ONE thing! it repeats the "replace string" -for adding to the quantity of the item- twice for seom hideously odd reason.

if (actionact) {
if (lindexof(#s(this.item),client.inv)==-1){
addstring client.inv,Apple;
addstring client.invcount,1;
}
else {
replacestring client.invcount,lindexof(#s(this.item),client.inv) ,#v(strtofloat(#I(client.invcount,lindexof(#s(this .item),client.inv)))+1);
}
}

the action command is simply

if (keypressed){
if (keydown(6)){
triggeraction playerx+1.5+vecx(playerdir)*2,playery+2+vecy(playe rdir)*2,act,;
}
}

any help, id appreciate ^_^

Dach 03-18-2004 03:29 AM

either keypressed is running twice or that part of the script isn't the problem, do you have a timeout running around the execution of this script? (within the same script I mean)

wonderboysp2p 03-18-2004 03:44 AM

No timeouts at all.... i ran debugger on the actionact part and it executes twice.. i havent debugged on keypressed yet


Any ideas on how ta fix it?

Dach 03-18-2004 05:32 AM

does this happen for others aswell?
I'd say restart the npc server, but that may not be needed, or be the problem... ooh! do it anyway!

DarkShadows_Legend 03-18-2004 08:01 AM

hmmm...
 
If you press the key 'a' then it sends the triggeraction. As long as the key is down it will continue to send the triggeraction.

I assume the part sending the triggeraction is a weapon and the other is an object. Wouldn't you have to destroy the object after the player gets it so the player will not get it added again?

wonderboysp2p 03-18-2004 09:07 AM

Re: hmmm...
 
Quote:

Originally posted by DarkShadows_Legend
Wouldn't you have to destroy the object after the player gets it so the player will not get it added again?

what if there's twelve of the items in that pile and you only want to pick up 3 of them?

Loriel 03-18-2004 03:12 PM

Re: -now what am i doing wrong?
 
Quote:

Originally posted by wonderboysp2p
-now what am i doing wrong?
You are not using code tags and you named this thread so it tells nothing about your problem.

I think the problem is that you use keypressed and keydown. You should either figure out the key that was pressed by the #p thingies you get along with keypressed, or you should use a timeout loop and check whether the key was already pressed in the previous frame (by doing this.oldkeydown = keydown(6) after everything).
Otherwise, any keypress while A is pressed will trigger the action again. It is not true, though, that this gets looped as someone said.

Other than that, you should do some precalculating instead of writing everything in the replacestring line. It helps not only readability but also might be more efficient, if it takes less time to access variables than it takes to call lindexof and stuff. But readability still wins.
NPC Code:
  else {
itemindex = lindexof(#s(this.item),client.inv);
itemcount = strtofloat(#I(client.invcount, itemindex)) + 1;
replacestring client.invcount,itemindex,#v(itemcount);
}
destroy; // Assuming it is supposed to be "picked up" now


Next, you might consider allowing items to not only have a type but also a quantity so if many items of the same type are close, they get put together into one NPC.

And have a look at my item system, perhaps.

wonderboysp2p 03-19-2004 02:29 AM

no idea why it was repeating the keydown command but i DID end up fixing it by making a quick pause in the (actionact) part
NPC Code:

if (actionact) {
if (this.itemcount>0){
if (this.wait==0){
if (lindexof(#s(this.item),client.inv)==-1){
addstring client.inv,#s(this.item);
addstring client.invcount,1;
this.itemcount--;
pause();
}
else {
replacestring client.invcount,lindexof(#s(this.item),client.inv) ,#v(strtofloat(#I(client.invcount,lindexof(#s(this .item),client.inv)))+1);
this.itemcount--;
pause();
}
}
update();
}
else {
destroy;
}
}



im all curious about the repeatingness now... X_x a bug?

Andy0687 03-19-2004 04:19 AM

Quote:

Originally posted by wonderboysp2p
im all curious about the repeatingness now... X_x a bug?
Mabye not

i was intrested how you had

NPC Code:

if (keypressed) {
if (keydown(6)) {
}
}



i was unaware that was possibile, and perhaps thats the reason it is repeating, because you had keydown, which reads fast i thought, so when the key is pressed and its reading it, seems like twice before you can get your finger up.

No bug i dont think.

Dach 03-19-2004 04:50 AM

... Actually none of them repeat, unless you hold them down too long, make sure your keyboard settings don't repeat keys too fast. If not that, it's lag, use a timeout and check that the key wasn't down last time it was checked.

weapon;
NPC Code:

//#CLIENTSIDE
if (keypressed) {
if (keydown(5)) {
this.c++;
setplayerprop #c,#v(this.c);
}
if (strequals(#p(1),d)) {
this.d++;
setplayerprop #c,#v(this.d);
}
if (keydown2(keycode(a),false)) {
this.a++;
setplayerprop #c,#v(this.a);
}
}



toAndy: keydown is a flag, it can't "read fast"

Andy0687 03-19-2004 09:13 PM

Quote:

Originally posted by Dach

toAndy: keydown is a flag, it can't "read fast"
I was thinking of when you did something like

NPC Code:

if (timeout) {
if (keydown(#)) {
}
}



that perhaps that same thing was applying itsself here.

protagonist 03-19-2004 11:00 PM

Quote:

Originally posted by Dach

toAndy: keydown is a flag, it can't "read fast"

Keypressed can make it be read fast, though.

WanDaMan 03-19-2004 11:13 PM

Quote:

Originally posted by protagonist


Keypressed can make it be read fast, though.

keypressed > sensetive > timeout=.1

Loriel 03-20-2004 01:55 AM

I never liked keypressed ;)

Tyrial 03-20-2004 02:12 AM

Quote:

Originally posted by Loriel
I never liked keypressed ;)
What do you like? keysmashed? ;)
'

hehe i like keydown2

wonderboysp2p 03-20-2004 02:37 AM

Quote:

Originally posted by Loriel
I never liked keypressed ;)
meh, its just as fun

Dach 03-20-2004 07:46 AM

Your better off using the timeout approach anyway, it seems to be more reliable
-psuedocode-
NPC Code:

if timeout
if keydown key
if keydownlast = false
do keydown stuff
keydownlast = true
end if else
nothing end else
end if else
keydownlast = false
end else
timeout = .05
end timeout


why did I even bother making that pseudocode anyway?

Loriel 03-20-2004 01:08 PM

It is not really pseudocode. :p

NPC Code:
on a timeout,
if key is pressed
and was not pressed las round, then do stuff with key
anyway, remember it was pressed this round
else remember it was not pressed
reset the timeout


tlf288 03-20-2004 08:21 PM

Quote:

Originally posted by Dach

why did I even bother making that pseudocode anyway?

That is like spanglish. :(


All times are GMT +2. The time now is 11:40 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.