Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   String-Based Weapon Tutorial [Intermediate] (https://forums.graalonline.com/forums/showthread.php?t=51240)

Termina_Owner 02-21-2004 09:20 AM

String-Based Weapon Tutorial [Intermediate]
 
Hello... Bored one friday night, so I'm writing this! I'm doing this directly in the Forum, so there might be little mistakes.


First off... You want to know what you want your item system to look like. There are many things you can do on this to make what you want. Persay, if you want some gun item system made, something like:

NPC Code:

GUN:
itemINDEX="Gun Name",graphics.png,TYPE,ShootRate,MaxShells,Bullet Type
BULLET:
itemINDEX="Bullet Name",graphics.png,TYPE,DAMAGE,SPEED



INDEX: what the item index is... A code-name to better remember things.
Gun Name: The name of your Gun
TYPE: Type of item (Gun - 0; Bullet - 1)
Shoot Rate: How fast your bullets shoot
Max Shells: How much bullets you can shoot before needing to reload.
Bullet Type: The index of the bullet.
Damage: How much the bullets hurt
Speed: The speed it shoots out

Once you have what you want, you can easily make an item... I'll make one before making the system to refer to it.

NPC Code:

item0="Pistol",pistol.png,0,2,8,2
item1="Uzi",pistol.png,0,0.5,100,3
item2="Pistol Bullets",bullet.png,1,1.5,20
item3="Uzi Bullets",bullet.png,1,0.5,30



Now that you got that determined, you can move on to the actual programming.

First off... You'd want to know what items you have... You can have a multitude of items, and want them easily accessed.
To do so, the most effective way I found was to record them in an array:
NPC Code:

if (playerenters){
maxitems = 4;
setarray this.item,maxitems;
setarray this.itemtype,maxitems;
setarray this.itemstate,maxitems;
func_array();
}


In here, you can see that I have this.item, this.itemtype, and this.itemstate. This is mainly so that I can easily identify whether I selected a gun, or ammo, and whether I have it equiped.
NPC Code:

function func_array(){
itemcount = 0;
for(i=0;i<maxitems;i++){
if (!strequals(#s(client.item#v(i)),)){
this.item[itemcount] = i;
this.itemtype[itemcount] = strtofloat(#I(client.item#v(this.item[itemcount]),2));
this.itemstate[itemcount] = i == strtofloat(#I(client.weap,0));
itemcount ++;
}
}
}


Now, well... In here, you see some traces of something I'll be using next. The client.weap reprosents what weapon your using.

Understand so far? Now... I won't be making the Q-Menu for you, however I'll make the main functions.

NPC Code:

if (playerchats){
tokenize #c;
if (strequals(#t(0),use_item)){
if (strequals(#t(1),#v(strtofloat(#t(1))))){ // If you say the INDEX
this.TI = strtofloat(#t(1));
findindex();
if (this.index>-1){ // Item Excists
this.weap = this.item[this.index];
setstring client.weap,#v(this.weap),#I(client.item#v(this.we ap),0);
func_array();
}
} else { // If you say Item Name
setstring this.name,#t(1);
findname();
if (this.index>-1){ // Item Excists
this.weap = this.item[this.index];
setstring client.weap,#v(this.weap),#I(client.item#v(this.we ap),0);
func_array();
}
}
}
}
function findindex(){
this.index = -1;
for (i=0;i<itemcount;i++){
if (this.item[i] == this.TI){
this.index = i;
break;
}
}
}
function findname(){
this.index = -1;
for (i=0;i<itemcount;i++){
if (strequals(#I(client.item#v(this.item[i]),0),#s(this.name))){
this.index = i;
break;
}
}
}



Erm... Complicated? Basically, if you say an index, it sees if it excists, and if it does, it records as the weapon your wearing. It recalls the item array, which reinitializes, and makes the item "on".

Now, for the actual use... This is mainly the most complex part, once you have the top done.

NPC Code:

if (created){
disableweapons;
timeout = 0.05;
}
if (timeout){
if (timer<0){
if (keydown(5)){
this.TI = strtofloat(#I(client.weap,0));
this.BI = strtofloat(#I(client.item#v(this.TI),5));
timer = strtofloat(#I(client.item#v(this.TI),3));
setshootparams (#I(client.item#v(this.BI),3));
angles = {4.71,3.14,1.57,0};
shoot playerx,playery,playerz,angles[playerdir],0,(#I(client.item#v(this.BI),4),shoot,;
}
} else timer -=0.05;
timeout = 0.05;
}


That's a basic concept of it. It doesn't subtract bullets, nor does it shoot from the center of the player, however, that is the general look of what it would look like.

Hope this tutorial explained what you wanted it to. If you want me to go into more details of things, just ask.

EDIT: Fixed some things.

WanDaMan 02-21-2004 11:16 AM

It's a good tutorial, but it's hard. You didn't describe any of the commands in detail and I couldn't really understand it :(...
But it looks good I guess <3
5 rating:)

osrs 02-21-2004 03:46 PM

Most of the people wont understand it.

Termina_Owner 02-21-2004 06:40 PM

Erm... Not for starters, I must admit. I tried not using too simple commands none-the-less.

I can't see where someone COULD have trouble... Just submit the part of the code which you don't understand, and I'll explain it more in detail.

Kadar 02-21-2004 06:43 PM

use clientr.flags

client.flags can be hacked easily

Termina_Owner 02-21-2004 07:26 PM

I made it conceptual...

Duwul 02-21-2004 08:35 PM

I understand it. <3 Rance

=P

However, for the angle in the shoot part, all you need to do is
NPC Code:

getangle(vecx(playerdir),vecy(playerdir))


WanDaMan 02-21-2004 10:15 PM

Rance, a **** who dosn't know how to indent a script I doubt he knows strings or anything..
It's good though like I said before :)

Termina_Owner 02-21-2004 10:58 PM

Quote:

Originally posted by WanDaMan
Rance, a **** who dosn't know how to indent a script I doubt he knows strings or anything..
It's good though like I said before :)

Ehh?

Duwul 02-21-2004 11:18 PM

Wan, it's obvious you don't know Rance as well as I do. :)

Deek2 02-21-2004 11:53 PM

NPC Code:
if (strequals(#t(1),strtoflaot(#t(1)))){


Remember, being paranoid about spelling errors is crucial for programming.

Termina_Owner 02-22-2004 12:31 AM

Erm.. You posted two errors: "float" , and the whole float thing needs to be in a #v()... :P

R0bin 02-22-2004 02:15 AM

Its cool, someone should sticky.

Thought 02-22-2004 06:07 AM

Indices are not 'code names'.
They are the position in a list, which can be iterated through by using an index.

WanDaMan 02-22-2004 11:40 AM

Quote:

Originally posted by Termina_Owner


Ehh?

A **** couldn't read it, so it's useless to them. But usefull to people like R0bin who know these things.


All times are GMT +2. The time now is 10:17 PM.

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