Gothic Online Forums
Give random item for Player - Printable Version

+- Gothic Online Forums (https://archive.gothic-online.com)
+-- Forum: Scripting (English) (https://archive.gothic-online.com/forum-11.html)
+--- Forum: Scripting Help (https://archive.gothic-online.com/forum-12.html)
+--- Thread: Give random item for Player (/thread-2340.html)



Give random item for Player - liegav - 12.02.2018

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"]
]


RE: Give random item for Player - Patrix - 12.02.2018

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.


RE: Give random item for Player - Profesores - 13.02.2018

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. });





RE: Give random item for Player - liegav - 13.02.2018

Thank you so much for both of you!


RE: Give random item for Player - Patrix - 13.02.2018

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/16862337/lua-semicolon-conventions

Also, you can put semicolons at the end of every operation, put it's not required in most cases.


RE: Give random item for Player - Profesores - 13.02.2018

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);


RE: Give random item for Player - Patrix - 14.02.2018

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)