Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Give random item for Player
#1
I am noob in Scripting.
Can someone help with this? 
Any suggestions? Should i create a table with items and get it random ?
Like this:
(I know it is wrong)

giveItem(killerid, Items.id(table_giveitems[random][0]), 1);

table_giveitems <- [

["ITMI_GOLD"],
["ITFO_APPLE"],
["ITFO_MILK"],
["ITFO_BEER"]
]
Reply
#2
1.Learn about scoping (global and local)
2.To random some value use proper function (built in rand in squirrel)
3.table_giveitems isn't table, it's array, in squirrel there is a slightly diffrerence between those two.
-Table: you can index table as you want, also you don't have insert numeric indexes in proper order (from 0 to x). To declare a table, you simply writing something like this: global_table <- {} or local local_table = {}
-Array: you are using methods, to manage array, like push, pop, etc. Array MUST BE indexed from 0 to x, and to add new value as last in the array, you simply using push method, read more in reference:
4.Don't use [] in array values, you have to write them without it, because there aren't indexes of table.

https://developer.electricimp.com/squirrel/array

Squirrel Script
  1. local items =
  2. [
  3. "ITMI_GOLD",
  4. "ITFO_APPLE",
  5. "ITFO_MILK",
  6. "ITFO_BEER", // this comma is optional
  7. ]
  8.  
  9. addEventHandler("onPlayerJoin",function(pid)
  10. { // below code will be executed when player join to the server
  11. local item = rand() % items.len()
  12.  
  13. giveItem(pid, Items.id(item), 1)
  14. })



Forgive me for my grammar mistakes, if i made any.
Reply
#3
Correct

Squirrel Script
  1. local items =
  2. [
  3. "ITMI_GOLD",
  4. "ITFO_APPLE",
  5. "ITFO_MILK",
  6. "ITFO_BEER", // this comma is optional
  7. ];
  8.  
  9. addEventHandler("onPlayerJoin",function(pid)
  10. { // below code will be executed when player join to the server
  11. local item = rand() % items.len();
  12.  
  13. giveItem(pid, Items.id(items[item]), 1);
  14. });


Reply
#4
Thank you so much for both of you!
Reply
#5
Profesores, putting semicolons in your code is optional in squirrel.

@Edit:

You must be putting semicolns in end of the line:
-When it's part of syntax (like for loops)
-When two lines of code can be interpreted ambiguity.

Check this out:

https://stackoverflow.com/questions/1686...onventions

Also, you can put semicolons at the end of every operation, put it's not required in most cases.
Reply
#6
Maybe, but that's my habit excerpted from other programming language, so i put the semicolons at the end of every operation.

The most important difference between your and my code:

giveItem(pid, Items.id(item), 1) -> giveItem(pid, Items.id(items[item]), 1);
Reply
#7
True, you don't create a reference to randomized item element from array, i didn't noticed it Big Grin. Your version of code is slightly better.
PS: putting semicolons isn't a bad habit, but i like sometimes not writing them in some scripting languages.

@Edit
(Except JavaScript as client-side browser scripting language, because of backward compatibility, (script standards are diffrent in older browsers and mostly it's safer to put semicolons)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Give- / Removeitems on Focus ItschiAtschiUtschi 3 2,413 10.06.2020, 16:04
Last Post: Patrix
  how do I Kill a player on the client side? demor140202 4 3,652 24.02.2020, 12:32
Last Post: Patrix
  Random spawn players demor140202 5 4,187 10.02.2020, 07:37
Last Post: Quarchodron
  Short question about Gold, how to give it ? Mattwell 2 3,421 26.01.2018, 00:12
Last Post: Mattwell
  Dev 8 item synchronisation HammelGammel 0 2,003 21.01.2017, 11:04
Last Post: HammelGammel
  Default world Item Xardas0327 1 2,152 21.03.2016, 13:51
Last Post: Bimbol
  Random Number Osmith 5 4,584 20.02.2016, 14:21
Last Post: Blacha

Forum Jump:


Users browsing this thread: