My First Piece of Code

Get technical support about the C++ source code and about Lua scripts for maps, entities, GUIs, the console, materials, etc. Also covered are the Cafu libraries and APIs, as well as compiling, linking, and the build system.
Posts: 95
Joined: 2014-04-06, 00:16

My First Piece of Code

Postby SoulRider » 2014-04-16, 02:48

I like to place little reminders about my current ability levels, so I can look back and laugh in the future about how much I didn't know.

Here is my first piece of code for Cafu. I am a big fan of opening as much up as possible in lua, so to give someone an option to create a custom GUI for a particular weapon for example, I decided to expose the current active weapon slot to lua.

I haven't been able to test it yet, as I can't figure out any code to indicate if it is working or not in lua, any feedback on whether it looks correct or not would be great :)

HumanPlayer.hpp:95 - I added the line

Code: Select all

static int GetActiveWeaponSlot(lua_State* LuaState);

That was placed at the bottom of the //Script methods list.
Then it was onto create the exposure code.


Code: Select all

//Line 94 Added:
{ "GetActiveWeaponSlot", EntHumanPlayerT::GetActiveWeaponSlot },

//At the end of the file, Line 1375 I added:
int EntHumanPlayerT::GetActiveWeaponSlot(lua_State* LuaState)
    cf::ScriptBinderT Binder(LuaState);
    IntrusivePtrT<EntHumanPlayerT> Ent   = Binder.GetCheckedObjectParam< IntrusivePtrT<EntHumanPlayerT> >(1);
    const EntityStateT&            State = Ent->State;

   if (State.ActiveWeaponSlot)
      lua_pushstring(LuaState, State.ActiveWeaponSlot)
        // Let the HUD know that we have no weapon.
        lua_pushstring(LuaState, "");

    return 1;

There maybe errors in the code, if you spot any then please let me know. I plan to try and expose more info to lua to aid game developers.
User avatar
Site Admin
Posts: 2143
Joined: 2004-08-19, 13:46
Location: Germany

Re: My First Piece of Code

Postby Carsten » 2014-04-16, 18:25

Hi SoulRider,

your code looks good, except for line

Code: Select all

lua_pushstring(LuaState, State.ActiveWeaponSlot)

where a semicolon is missing (the compiler should have complained about that), and more importantly, the function is expecting a const char* string as the second parameter, but you're passing a number (State.ActiveWeaponSlot).

To make posting code fragments easier, did you consider posting the output of git diff? It saves you from the extra work of describing which lines you edited where. It would even be better if you pushed your changes into a branch at a public repository (BitBucket or GitHub), depending on how large you intend your samples to grow. ;-)

Something very important: You're editing the EntHumanPlayerT class in branch entity-component-system -- exactly the area of code where I'm currently making big big changes, too (and will in fact remove soon). So most likely your edits and mine will collide sooner rather than later (causing "conflicts" when merging or rebasing in Git, e.g. as a consequence of a git pull command).

I'd recommend you to look, for now, better at "anything but" the human player code.
For example, one possible way to do that is to familiarize yourself with the way the "Eagle" or other entities work: Maps now consist of two files per map: *.cent are Lua scripts that define the entities in the map, and *.cmap have the related geometry primitives (the map compile tools convert the latter into a *.cw file, so the Engine actually reads for each map the pair of *.cent and *.cw files).
That is, although .cent files are read and written by CaWE and the tools, you can read it in an editor to see the Eagle's definition in there. (It is also possible to make minor edits in this file, as long as you make no "structural" changes that would require updates to the .cmap or .cw as well.) The Eagle's behavior script (loaded by the Script component) is in Games/DeathMatch/Scripts/. This easily leads you to more Lua or C++ code.

So just some thoughts about possible places to look at other than the human player. ;-) Once my larger structural changes to the human player code are done, it will be a much better opportunity to deal with it more deeply.
Best regards,

Return to “Coding and Scripting”

Who is online

Users browsing this forum: No registered users and 1 guest