Author Topic: Say please how to make command on type /command [ID] [Amount] (for VBS)  (Read 2455 times)

CANYON

  • Newbie
  • *
  • Posts: 29
  • Karma: +1/-0
    • View Profile
Say please how to make command on type /command [ID] [Amount]  (for VBS)

for example to give to the player the weapon with a certain quantity of ammunition

morphine

  • Newbie
  • *
  • Posts: 9
  • Karma: +2/-0
    • View Profile
Re: Say please how to make command on type /command [ID] [Amount] (for VBS)
« Reply #1 on: August 27, 2012, 01:59:38 pm »
I don't know what VBS looks like but it's very simple in general. You firstly need a tokenization function.

What are tokens?
Tokens are strings of anything separated by something. For example, the sentence "Hello World" has two tokens - "Hello" and "World", split by a single space (" "). The weapon command will go the same way. You will have an ID token and an ammo token.

Here's the tokenization function:
Code: [Select]
function GetTok(string, separator, n, ...)
{
local m = vargv.len() > 0 ? vargv[0] : n,
  tokenized = split(string, separator),
  text = "";

if (n > tokenized.len() || n < 1) return null;
for (; n <= m; n++)
{
text += text == "" ? tokenized[n-1] : separator + tokenized[n-1];
}
return text;
}

(Credits go to Force)

All that is left now is to make a weapon command.
Code: [Select]
else if( command == "wep" || command == "weapon" )
{
if( !text ) MessagePlayer( "Error - the syntax is /we(a)p(on) <ID> <Ammo>", player );
else
{
local
ID = GetTok( text, " ", 1 ), // The first token which comes after the command's name, separated by a space (" ")
ammo = GetTok( text, " ", 2 ); // The second token which comes after the ID, again separated by a space (" " )

if( IsNum( ID ) ) // Assuming you only want IDs to be used in your command, we will only accept digits
{
ID = ID.tointeger(); // Convert to an integer
ammo = ammo.tointeger(); // Convert to an integer

player.SetWeapon( ID, ammo ); // Success

local msg = format( "Successfully given a %s with %d ammo.", GetWeaponName( ID ), ammo ); // Pre-formatting messages is always nice.
MessagePlayer( msg, player );
}
else MessagePlayer( "The first parameter needs to be a digit.", player );
}

Have a nice day.

Vortrex

  • Full Member
  • ***
  • Posts: 267
  • Karma: +54/-73
    • View Profile
Re: Say please how to make command on type /command [ID] [Amount] (for VBS)
« Reply #2 on: August 27, 2012, 02:29:22 pm »
Doesn't GetTok() already exist in LU?

EDIT: morphine, it probably would be best if you also checked to see if ammo is an integer before converting it.
« Last Edit: August 27, 2012, 02:32:44 pm by Vortrex »

morphine

  • Newbie
  • *
  • Posts: 9
  • Karma: +2/-0
    • View Profile
Re: Say please how to make command on type /command [ID] [Amount] (for VBS)
« Reply #3 on: August 27, 2012, 02:48:21 pm »
Doesn't GetTok() already exist in LU?


Whoops. Wasn't aware it was a built-in function in LU.

EDIT: morphine, it probably would be best if you also checked to see if ammo is an integer before converting it.

I'll be sure to edit it when I have a chance, if CANYON won't figure out how to himself.
« Last Edit: August 27, 2012, 02:51:35 pm by morphine »

h05th

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Say please how to make command on type /command [ID] [Amount] (for VBS)
« Reply #4 on: December 18, 2012, 09:55:41 am »
Not function , error expression expected colum 5...

 

© Liberty Unleashed Team.