• Why the cached OOP CUserCMD is not changed on a user action ?
    3 replies, posted
I have API to retrieve and cache OOP class of type CUserCmd. I need to extract from it an IN_ENUM key that the user has pressed. When I go trough the test TOOL object, the cached object does not change and "RUN" is printed even if I am pressing "USE". My guess is that this object is not updated and I have to create new predication user command to read "USE" local function ReadCommand(ply)   local sNick  = ply:Nick()   local curCmd = tCmd[sNick]   if(not curCmd) then     tCmd[sNick] = ply:GetCurrentCommand()     curCmd = tCmd[sNick]   end; return curCmd end local function CheckButton(ply, IN)   local sNick  = ply:Nick()   local curCmd = tCmd[sNick]   if(not curCmd) then LogInstance("Precache missing"); return ply:KeyDown(IN) end   return (bit.band(curCmd:GetButtons(),IN) ~= 0) -- Read the cache end function TOOL:Reload(stTrace)   ReadCommand(self:GetOwner())   if(CheckButton(ply, IN_SPEED)) then     print("test", "RUN")   if(CheckButton(ply, IN_USE)) then     print("test", "USE")   end end
1) You are checking for IN_USE after checking IN_SPEED, so your IN_SPEED check will prevent the IN_USE check. This is expected behavior with your code. Try changing the order, putting IN_USE check first, using switch-case statement or not using elseif 2) Are you actually surprised that a cached variable doesn't update outside where you made it update? 3) What you are doing is quite dumb - there are many better alternatives to doing actions when a key press happens, for example Player/KeyDown and the input library.
1) Yes, that's correct, though the "IN_SPEED" was still set in the cached object and was returning true and skipping IN_USE 2) No, I was hoping the object was created just once and to be an interface for player keys 3) Yes, I guess making CUserCmd is not that expensive, but I only need to check shift/use so I better use KeyDown Thanks !
if(cmd->buttons & IN_SPEED) then print("test","RUN"); end if(cmd->buttons & IN_USE) then print("test,"USE"); end
Sorry, you need to Log In to post a reply to this thread.