![]() |
confusion on array and random()
someone was asking for help on this (probably have an idea of what you're thinking or going to say about that but anyway...)
they wanted to make it so whenever they pressed a key, a random image would be selected every time. I suggested putting the images in an array and using random(); referencing the array list. but what i attempted actually didn't work. and i'm not sure why. any clue on what happened? here is my attempt i won't use their actual image for this example since they're all strings anyway. PHP Code:
PHP Code:
or: 1, 0. are those correct outputs? was i wrong in expecting it to output the images name or whats happening? |
random(a, b) returns a random number in the range [a, b).
So what you probably want to do is take the number you got in your second example and use it as the index of your array. PHP Code:
|
Suddenly i can see how dumb my second attempt looks. I didn't even refer anything from the array at that point. aside from it's size anyway. but anyways thanks. i can see what i did wrong/should have done.
|
Instead of text I would've used your method. Creating 26 images (or however many keys you want) and using int(random(0, 27))
|
Quote:
Ah. if i understand what you're saying correctly, he wasn't trying to use a lot of the keys. he wanted to use onWeaponFired() for his project. the array was just meant to contain his images names. :P he was trying to create a bomb that would randomly change images each time he pressed D. using putbomb2(); Note: just for quick reference, when i say "he" i meant the guy i was trying to help. |
Quote:
PHP Code:
|
Quote:
thanks though i got two methods to use now |
Quote:
Quote:
If it was fixed, I wouldn't use it until v6 is mandatory (or is it already?), since it's broken v5 clientside. |
Quote:
OutPut Weapon/GUI-script tempMD added/updated by Carlito test2 Weapon/GUI-script tempMD added/updated by Carlito test1 Mis-Read your response, didn't know it was broking on V5 Clientside. Will test sometime on that theory. Edit: Also confirmed it works on clientside on V6. PM: received from (npcserver):Kingdoms (Server) test1 test2 test2 test2 test2 test1 test2 |
Quote:
PHP Code:
|
Quote:
|
Serverside Output
Weapon/GUI-script RandomBrokeing added/updated by Carlito 0: foo 1: foo 2: foo 3: foo 4: foo 5: foo 6: foo 7: foo 8: foo 9: foo ClientSide Output PM: received from (npcserver):Kingdoms (Server) 0: foo 1: foo 2: foo 3: foo 4: foo 5: foo 6: foo 7: foo 8: foo 9: foo Works fine |
Quote:
|
| All times are GMT +2. The time now is 09:45 AM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.