Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   removeweapon() (https://forums.graalonline.com/forums/showthread.php?t=80400)

Chompy 07-07-2008 10:57 PM

removeweapon()
 
Well, there's something weird about it..

lets see

PHP Code:

function onActionPlayerOnline() {
  
temp.weps player.weapons;
  
temp.0;
  for(
temp.wep weps) {
    echo(++ 
i SPC wep.name);
  }


(In this case it echoed 28 weapons)

This echoes all the player's weapons on login. However.. If I try to remove all weapons on login, something weird happens.. :(

PHP Code:

function onActionPlayerOnline() {
  
temp.weps player.weapons;
  
temp.0;
  for(
temp.wep weps) {
    
removeweapon(wep.name);
    echo(++ 
i SPC wep.name);
  }


It only echoes 14 weapons and removes those that were echoed..

So to get all weapons removed you have to reconnect like.. >5 times o.o;;

Why is this? Anyone knows?

cbk1994 07-07-2008 11:06 PM

Happened on Utopia too, is very annoying.

DustyPorViva 07-07-2008 11:11 PM

There are a lot of weapons that are hidden that are given to the player by the client... stuff like the server list, player list, client RC. I dunno if this is what you're talking about, I'm having a hard time understanding your post.

I don't think they can be removed.

Also, your second loop isn't accurate. You should run a loop that removes weapons, then run a separate loop that echos the weapons.

Chompy 07-07-2008 11:13 PM

Quote:

Originally Posted by DustyPorViva (Post 1401390)
There are a lot of weapons that are hidden that are given to the player by the client... stuff like the server list, player list, client RC. I dunno if this is what you're talking about, I'm having a hard time understanding your post.

I don't think they can be removed.

Also, your second loop isn't accurate. You should run a loop that removes weapons, then run a separate loop that echos the weapons.

I want to remove all weapons the player has in "Weapons" when I open their attributes :o (Oh, and edited post to add a missing word xD)

Admins 07-07-2008 11:15 PM

When you call player.removeweapon() then its modifying player.weapons[], that's why the loop is only removing 14. Possible solution would be

PHP Code:

for (i=player.weapons.size()-1i>=0i--)
  
player.removeweapon(player.weapons[i].name); 

(might be confusing but player.weapons is a special intelligent object which lets you print the player weapon list as string even when there are weapons which don't really exist, and by using weapons[] you can still directly access the weapon npc)

xXziroXx 07-08-2008 12:32 AM

I do it like this on Mythic, work's like a charm:

PHP Code:

  temp.wsize player.weapons.size();
  for (
temp.0temp.temp.wsizetemp.i++) {
    if (
player.weapons[i].name != name) {
      
removeweapon(player.weapons[i].name);
      if (
temp.wsize != player.weapons.size()) {
        
i--;
        
temp.wsize player.weapons.size();
      }
    }
  } 


DrakilorP2P 07-08-2008 02:24 AM

Wouldn't this work?
PHP Code:

for (temp.i=0temp.i<player.weapons.size(); temp.i++)
  
player.removeweapon(player.weapons[0].name); 


Inverness 07-10-2008 12:38 AM

Quote:

Originally Posted by DrakilorP2P (Post 1401430)
Wouldn't this work?
PHP Code:

for (temp.i=0temp.i<player.weapons.size(); temp.i++)
  
player.removeweapon(player.weapons[0].name); 


Skipping over Stefan's posts usually isn't a smart thing to do. If you don't understand it then just think really hard about how arrays work.

DrakilorP2P 07-10-2008 02:02 AM

Quote:

Originally Posted by Inverness (Post 1401824)
Skipping over Stefan's posts usually isn't a smart thing to do. If you don't understand it then just think really hard about how arrays work.

Just noticed my previous post is a really misleading way of deleting half the list.

Anyway, similar method, different language but guaranteed to work:
PHP Code:

>>> foo = [1"bar"False, ["baz""crux"], 9]
>>> 
cruux len(foo)
>>> for 
i in range(cruux):
...     print 
"Foo: " str(foo[0])
...     
foo.remove(foo[0])
... 
Foo1
Foo
bar
Foo
False
Foo
: ['baz''crux']
Foo9
>>> foo
[]
>>> 

All assuming the precise functioning of this "special intelligent object" did not elude me.

Inverness 07-10-2008 02:23 AM

Quote:

Originally Posted by DrakilorP2P (Post 1401849)
Anyway, similar method, different language but guaranteed to work:

No it wouldn't.

If you want to delete stuff in an array part by part you need to start at the end so when you delete the element and the array shrinks in size you're not missing anything. When you're deleting a part from an array everything after it is decreasing in index so the for loop misses the element that replaced the one it just deleted while it moves on to the next highest index.

Also in your Python code the 'cruux' variable is unnecessary. You're taking the length from the 'foo' array and creating a new array from it but not actually using variable 'i', so you should just do for i in foo:

Edit:
Quote:

Originally Posted by DrakilorP2P
Just noticed my previous post is a really misleading way of deleting half the list.

It appears I was the one who was mislead when reading your post. Since I just noticed it was only deleting index 0 and not index i. If done like that it should work fine I guess.

Dan 07-10-2008 12:03 PM

Drakilors method would work too. He's using [0], not [temp.i], so it would just delete all first weapons in the list untill nothing's left.

zokemon 07-10-2008 06:28 PM

Quote:

Originally Posted by Dan (Post 1401901)
Drakilors method would work too. He's using [0], not [temp.i], so it would just delete all first weapons in the list untill nothing's left.

But like Stefan said, player.weapons is a special object in that it can give you a string list which could include weapons that don't actually exist. If you try to removeweapon() a weapon in that list that isn't an actual weapon, the weapon won't be removed from the list thus causing an infinite loop.

Dan 07-10-2008 08:52 PM

Quote:

Originally Posted by zokemon (Post 1401984)
But like Stefan said, player.weapons is a special object in that it can give you a string list which could include weapons that don't actually exist. If you try to removeweapon() a weapon in that list that isn't an actual weapon, the weapon won't be removed from the list thus causing an infinite loop.

Indeed you're right. Perhaps there should come some built in check to see if a weapon in someone's weaponlist still exists.

Inverness 07-10-2008 11:55 PM

Quote:

Originally Posted by Dan (Post 1402010)
Indeed you're right. Perhaps there should come some built in check to see if a weapon in someone's weaponlist still exists.

player.weapons.index() ?

zokemon 07-11-2008 05:23 AM

Quote:

Originally Posted by Inverness (Post 1402060)
player.weapons.index() ?

Fairly costly to be doing array searching on each iteration.

Dan 07-11-2008 10:29 AM

Quote:

Originally Posted by Inverness (Post 1402060)
player.weapons.index() ?

I more ment that sometimes, lets say when you have the weapon "Test" in your weaponlist and the weapon "Test" is not on the server anymore (deleted), it messes up.


All times are GMT +2. The time now is 02:47 AM.

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