• Bitwise Operations gone wrong?
    9 replies, posted
[lua] function _R.Player:GivePermission( name ) local p = self:GetPData( "Perm" ); local a = Permissions[ name ][ 3 ]; local flags = p | a self:SetPData( "Perm" , flags ) print( flags ) print( self:GetPData( "Perm" ) ) end CreatePermission( "Admin" , "lol" ) CreatePermission( "VIP" , "lol" ) CreatePermission( "Mod" , "lol" ) CreatePermission( "SuperAdmin" , "lol" ) CreatePermission( "Owner" , "lol" ) [/lua] outputs : [CODE] ] lua_run print( Entity(1):GivePermission( "Owner" ) ) > print( Entity(1):GivePermission( "Owner" ) )... 1[Perm] 1[Perm] [/CODE] wtf?
Did you print p and a?
[QUOTE=Wizard of Ass;31906171]Did you print p and a?[/QUOTE] Line 4 and 6 is the answer to your question :toad:
Isnt | the bitwise or operator, so of course it will be the same. [editline]23rd August 2011[/editline] try & instead.
[QUOTE=Coon;31906248]Isnt | the bitwise or operator, so of course it will be the same. [editline]23rd August 2011[/editline] try & instead.[/QUOTE] If you want to add flags you should use bitwise or. And should be used when checking for flags.
GetPData returns a string, which is probably what's screwing this up, though I'm not sure why it's not throwing the standard "attempt to perform arithmetic on a string value" error. Regardless, tonumber(self:GetPData( "Perm" )) will probably help here.
Ok Thanks ;) Seems logical your response mrflippy, I will try that now [editline]24th August 2011[/editline] Nope :/ [code] ] lua_run Entity(1):ResetPermissions() > Entity(1):ResetPermissions()... ] lua_run Entity(1):GivePermission("Admin") > Entity(1):GivePermission("Admin")... 0 1 1 1 ] lua_run Entity(1):GivePermission("Owner") > Entity(1):GivePermission("Owner")... 1 1 1 1 [/code] [lua] -- Permissions.lua local Permissions = {} function CreatePermission( name , info ) Permissions[ name ] = { name , info, math.pow(#Permissions + 1 , 2 )}; return math.pow(#Permissions + 1 , 2 ) end function _R.Player:GivePermission( name ) local p = tonumber(self:GetPData( "Perm" )); print( p ) local a = Permissions[ name ][ 3 ]; print( a ) local flags = p | a self:SetPData( "Perm" , flags ) print( flags ) print( tonumber(self:GetPData( "Perm" )) ) end function _R.Player:GetPermissions( ) return tonumber(self:GetPData( "Perm" )) or 0 end function _R.Player:HasPermission( name ) return ( self:GetPermissions( ) & Permissions[ name ][ 3 ] == Permissions[ name ][ 3 ] ) end function _R.Player:ResetPermissions( ) self:SetPData( "Perm" , 0 ) end CreatePermission( "Admin" , "lol" ) CreatePermission( "VIP" , "lol" ) CreatePermission( "Mod" , "lol" ) CreatePermission( "SuperAdmin" , "lol" ) CreatePermission( "Owner" , "lol" ) [/lua] ( the names are Owner and Admin and what ever because i didnt have any inspiration for other names. Im not actually going to use it as a permissions mod for administration ) [editline]24th August 2011[/editline] [QUOTE]] lua_run PrintTable( Permissions ) > PrintTable( Permissions )... Mod: 1 = Mod 2 = lol 3 = 1 Admin: 1 = Admin 2 = lol 3 = 1 Owner: 1 = Owner 2 = lol 3 = 1 SuperAdmin: 1 = SuperAdmin 2 = lol 3 = 1 VIP: 1 = VIP 2 = lol 3 = 1 [/QUOTE] ah , any ideas now? [editline]24th August 2011[/editline] the # operater doesnt work on tables with string indexs? needs fixing
# returns the highest numeric index of a table.
You also have the parameters to math.pow switched around. You want 2^x, not x^2
[QUOTE=mrflippy;31919053]You also have the parameters to math.pow switched around. You want 2^x, not x^2[/QUOTE] Yh i forgot to post but i have it fixed :P And yes, i had the parameters mixed up :P Thanks tho, for all of your help :)
Sorry, you need to Log In to post a reply to this thread.