Problem with variable

Started by value2k, Mar 04, 2016, 05:35 AM

Previous topic - Next topic

value2k

Hello i got problem with my script

local jobid;
function onScriptLoad()
{
CreatePickup(408, Vector(-315.38, -536.935, 10.3346));
}
function onPlayerSpawn( player )
{
jobid = 0;
}
function onPickupPickedUp( player, pickup )
{
if (pickup.ID == 0)
{
if ( jobid == 1 )
{
MessagePlayer("Nein.", player);
return false;
}
player.Cash = player.Cash + 1000;
jobid=1;
}
}

And server write a ERROR:
AN ERROR HAS OCCURED [the index 'jobid' does not exist]

CALLSTACK
*FUNCTION [onPlayerSpawn()] scripts/main.nut line [54]

LOCALS
[player] INSTANCE
[this] TABLE

AN ERROR HAS OCCURED [the index 'jobid' does not exist]

CALLSTACK
*FUNCTION [onPickupPickedUp()] scripts/main.nut line [209]

LOCALS
[pickup] INSTANCE
[player] INSTANCE
[this] TABLE

Please help :(

ysc3839

#1
Replace local jobid; with jobid <- 0;

.

#2
Every file is compiled as a function. And as with all functions. All local variables are deleted once the function ends. Same goes with that local variable. Which is why you need to make it global. But be aware though. Not everything should be made global to pollute your global scope. Try to keep it clean. Or at least name things appropriately. Use names that you wouldn't find in a function. Usually people prefix them with 'g_' and uppercase name. That 'g_' stands for global variable and avoids confusion and collisions. Like 'g_MyVar'.
.

value2k

Quote from: S.L.C on Mar 04, 2016, 05:54 AMEvery file is compiled as a function. And as with all functions. All local variables are deleted once the function ends. Same goes with that local variable. Which is why you need to make it global. But be aware though. Not everything should be made global to pollute your global scope. Try to keep it clean. Or at least name things appropriately. Use names that you wouldn't find in a function. Usually people prefix them with 'g_' and uppercase name. That 'g_' stands for global variable and avoids confusion and collisions. Like 'g_MyVar'.
Yes, but i need variable only for player, one for player

Xmair

jobid <- array ( GetMaxPlayers ( ) , 0 );

function onPlayerPart( player , reason )
{
jobid [ player.ID ] = 0;
}
I think this is what you meant.

Credits to Boystang!

VU Full Member | VCDC 6 Coordinator & Scripter | EG A/D Contributor | Developer of VCCNR | Developer of KTB | Ex-Scripter of EAD

value2k

Quote from: Xmair on Mar 04, 2016, 06:27 AMjobid <- array ( GetMaxPlayers ( ) , 0 );

function onPlayerPart( player , reason )
{
jobid [ player.ID ] = 0;
}
I think this is what you meant.
Yes, thx