weird problem after using random function with array (solved) -
doreto - 03.04.2013
So let me first tell you about problem and then show you code. Whole point of problem is that everytime is show me same result over and over and is should be random and only if type (check code below) is 0 (wich is have to be 1) . I've made some debugs on code and even 2 (second line of array ) is chosen (code below) aways print same (first) result.
pawn Код:
//on top
enum mission_unload
{
name[128],
type,
Float:unloadx,
Float:unloady,
Float:unloadz
};
new Mission_unload[MAX_PLAYERS][mission_unload];
new unloads[][mission_unload] =
{
{"LS Construction Site", 0, 1859.0, -1314.0, 14.0}, // Location 1
{"SF Construction Site", 1, -2083.0, 209.0, 35.5}, // Location 2
{"LV Construction Site", 1, 2708.0, 878.0, 10.0} // Location 3
//{"Quarry", 587.25, 844.75, -42.5} // Location 11
};
enum mission_load
{
names[128],
type,
Float:loadx,
Float:loady,
Float:loadz
};
new Mission_load[MAX_PLAYERS][mission_load];
new loads[][mission_load]=
{
{"Quarry", 1, 587.25, 844.75, -42.5}
};
// and command
CMD:work2(playerid, params[])
{
if(!strcmp(params, "test"))
{
new rand = random(sizeof(loads));
if(loads[rand][type] == 1)
{
// random load
new sload[128];
format(sload,sizeof(sload),"loads[rand][name] = %s", loads[rand][names]);
SendClientMessage(playerid, -1, sload);
SetPlayerCheckpoint(playerid, loads[rand][loadx], loads[rand][loady], loads[rand][loadz],CHECKPOINT_SIZE);
strset(Mission_load[playerid][names], loads[rand][names]);
Mission_load[playerid][loadx] = loads[rand][loadx];
Mission_load[playerid][loady] = loads[rand][loady];
Mission_load[playerid][loadz] = loads[rand][loadz];
printf("load name: %s", Mission_load[playerid][names]);
printf("load - %d",rand);
// random unload
new rands = random(sizeof(unloads));
printf("unload - %d", rands);
if(unloads[rands][type] == 1)
{
new sunload[128];
format(sunload,sizeof(sunload),"unloads[rand][name] = %s",unloads[rand][name]);
SendClientMessage(playerid,-1,sunload);
strset(Mission_unload[playerid][name], unloads[rand][name]);
Mission_unload[playerid][unloadx] = unloads[rands][unloadx];
Mission_unload[playerid][unloady] = unloads[rands][unloady];
Mission_unload[playerid][unloadz] = unloads[rands][unloadz];
printf("unload name: %s",Mission_unload[playerid][name]);
}
else SendClientMessage(playerid,-1,"again - unload");
new string[128];
format(string,sizeof(string),"deliver |-_-|> ore form %s to %s",Mission_load[playerid][names],Mission_unload[playerid][name]);
SendClientMessage(playerid,-1,string);
cp = 1;
}
else SendClientMessage(playerid,-1,"again - load");
}
return 1;
}
server log from print function
pawn Код:
[13:44:08] load name: Quarry
[13:44:08] load - 0
[13:44:08] unload - 1
[13:44:08] unload name: LS Construction Site
[13:44:18] load name: Quarry
[13:44:18] load - 0
[13:44:18] unload - 2
[13:44:18] unload name: LS Construction Site
[13:44:19] load name: Quarry
[13:44:19] load - 0
[13:44:19] unload - 1
[13:44:19] unload name: LS Construction Site
[13:44:20] load name: Quarry
[13:44:20] load - 0
[13:44:20] unload - 1
[13:44:20] unload name: LS Construction Site
[13:44:21] load name: Quarry
[13:44:21] load - 0
[13:44:21] unload - 1
[13:44:21] unload name: LS Construction Site
EDIT: i'm sorry forgot to include strset stock
pawn Код:
stock strset(dest[], source[])
{
new count = strlen(source);
for(new i = 0; i < count; i++)
{
dest[i] = source[i];
}
dest[count] = 0;
}
Any type of help will be appreciated!
EDIT:
Problem solved
Re: weird problem after using random function with array -
SuperViper - 03.04.2013
You're using
rand for some of your unload variables while you should be using
rands. Let me point out some lines for you:
pawn Код:
format(sunload,sizeof(sunload),"unloads[rand][name] = %s",unloads[rand][name]);
pawn Код:
strset(Mission_unload[playerid][name], unloads[rand][name]);
Re: weird problem after using random function with array -
doreto - 03.04.2013
Thank you very much
SuperViper , i was aways thing there is something easy and obvious but i was "blind" to see it
. You deserve reputation point
EDIT: damm it i was alredy give you reputation point for something that i was find usefull "You must spread some Reputation around before giving it to SuperViper again"
Problem solved