Monday 29 February 2016

Problem with handling HTML form checkboxes in php

php doesn't handle checkboxes properly in html forms. If you use

My Checkbox <input type="checkbox" name="my_checkbox" id="my_checkbox"'.($current_value?'checked':'').' />

You'll only get a result for isset($_REQUEST['my_checkbox']) if the box is *checked*, not if it's cleared. So you can't test for whether the box was unchecked by the user, because using !isset would be the same whether the page was just loaded, or if the form was submitted with the box unchecked by user input.

The solution from Stack Overflow:

Every checkbox generated is associated with a hidden field of the same name, placed just before the checkbox, and with a value of "0".

<input name="my_checkbox" type="hidden" value="0" />
<input name="my_checkbox" type="checkbox" value="1" />


Then isset($_REQUEST['my_checkbox']) always returns true if the box was modified, and false if the page was just loaded. And you'll always get the correct '0' or '1' value in $_REQUEST['my_checkbox'].

Thursday 25 February 2016

How to create a plugin for Lumberyard

Lumberyard/CryEngine uses waf, a build configuration system similar to CMake.

To add a plugin project, create a subdirectory NewPlugin in dev/Code (a plugin might go in dev/Code/Sandbox/Plugins for example).

Add a wscript file to this directory, looking like this:

 
def build(bld): 
 
 bld.CryPlugin(
  target = 'NewPlugin',
   vs_filter = 'Sandbox/Plugins',
        file_list   = 'newplugin.waf_files',
  features =  ['qt'],
  includes = [
            '.',
            '..'
        ],
  use='EditorCommon'
 )
And add a file newplugin.waf_files, looking like:
{
 "NoUberFile": 
 {
  "Root":
  [
   "NewPlugin.cpp",
   "NewPlugin.h"
  ]
 }
}
Then go to dev/_WAF_/specs, and edit all.json. Add "NewPlugin" to the win_profile_modules and win_debug_modules list. Finally, recreate the solution by opening a command prompt in dev/ and running "lmbr_waf configure"

Sunday 14 February 2016

The Three Hierarchies of C++

Lest anyone ever tell you programming isn't complicated, it occurred to me that a C++ program has three interlocking, but independent hierarchies.

  • Namespace: The name of the function, within its parent namespace and/or the class that owns it.
  • Call Stack: The parent is the calling function, the child is the callee.
  • Inheritance: The class/struct is a child of the base class.

Could we stand to lose one or two of these?

Friday 12 February 2016

Reverting all deletes in Git, leaving file modifications in place

I asked how to do this on StackOverflow, and the answer is:

git diff --diff-filter=D --name-only | xargs git checkout

You need the "xargs": just piping the output of "diff" to "checkout" doesn't work.