Jump to content
Sign in to follow this  
BennySouthSt

class EventHadler in Config

Recommended Posts

I'm doing a few tweaks with WW4 Extended for use in a small OFP group. I've created a few units, but have based them off the standard WW4 units, not the WW4 EXT units. This means that they can't use any of the great Extended features, in particular disposable launchers. 

 

In order to enable this, I need the units to run the "sq_infantry" (or something along that line, ill find the correct name later) script. I've tried adding it under the EventHandler class in a units config, but to no success. I'm not that experience with modding, so I'm pretty sure its my syntax.

 

What would I add under class EventHandler in a units config so that it runs the "sq_infantry" script on the unit?

Share this post


Link to post
Share on other sites

You may have forgot (I don't know - haven't seen the code) that there are several types of Event Handlers.

 

What You're looking for is init EH. In case of WW4 EXT (since You've already mentioned it) it looks like that (USMC Rifleman):

class EventHandlers 
{ 
init = "[_this select 0,[""MAP_ILBE_APw""],[""MAP_WW4EXT_W556_30Bmag_PDM"",""MAP_WW4EXT_W556_30Bmag_PDM"",""MAP_WW4EXT_W556_30Bmag_PDM""]] exec ""\WW4EXT_scripts\eh_infantry.sqs"";  ";
};

If Your script is called sq_infantry.sqs then it should look like this:

class EventHandlers 
{ 
init = "_this exec ""\my_scripts\sq_infantry.sqs"";  ";
};

Or something like that, I'm not expert ;) But this preceded with underline, and double quotation marks are important!

Share this post


Link to post
Share on other sites

What would I add under class EventHandler in a units config so that it runs the "sq_infantry" script on the unit?

You need to define the init eventhandler of any new unit unit you create:

class EventHandlers {
    init = "[_this select 0] exec ""\WW4EXT_scripts\eh_infantry.sqs""; ";
};

This will work as long as you still require vanilla Ext or include the "ww4ext_scripts.pbo" file in your version.

--

 

EDIT:

 

Something I forgot to mention myself but that was covered already by krzy:

 

What You're looking for is init EH. In case of WW4 EXT (since You've already mentioned it) it looks like that (USMC Rifleman):

class EventHandlers 
{ 
init = "[_this select 0,[""MAP_ILBE_APw""],[""MAP_WW4EXT_W556_30Bmag_PDM"",""MAP_WW4EXT_W556_30Bmag_PDM"",""MAP_WW4EXT_W556_30Bmag_PDM""]] exec ""\WW4EXT_scripts\eh_infantry.sqs"";  ";
};

The second and third parameters are used to specify the rucksack and rucksack ammo that the unit will use. So the full format for a call to the eh_infantry.sqs script would be:

[_this select 0, [<class of the rucksack>],[<ruckack ammo classes>]] exec "\WW4EXT_scripts\eh_infantry.sqs";

The classes of the rucksacks themselves and the rucksack ammo can be found inside the MAP_Rucksack.pbo (in cfgWeapons.EXT.hpp and cfgRuckMags.EXT.hpp, respectively).

 

I made several spreadsheets covering the classes for almost everything found in Ext, but they were done for the things included in the 0.8 version (or so) only. I guess they should be updated and expanded. Eventually.

 

 

 

Share this post


Link to post
Share on other sites

Great, thanks for all the info guys. If you can't tell, I'm new-ish to all this! I've got the basics down, its just learning the more specific intricacies of things like this - it seems like it was the double speech marks that threw me, as i'd defined the fact that it was an "init" event handler.

 

Sorry to be asking more questions, but asking people who know what they're doing is probably more effective than struggling through random internet pages -  what does the "[_this select 0]" part do? I'm assuming that it specifies that the script is acting directly on the units it's being run from, but I don't really want to work with assumptions. Thanks again!

Share this post


Link to post
Share on other sites

https://community.bistudio.com/wiki/this

 

http://www.ofpec.com/editors-depot/index.php?action=details&id=35&game=OFP%7CAll

 

There's some good info on "this"

 

[_this select 0]

 

That's a way of grabbing an element inside an array.

 

The Fired EH has these elements in it's array, for example:

 

Passed array: [unit, weapon, muzzle, mode, ammo]

  • unit: Object - Object the event handler is assigned to 
  • weapon: String - Fired weapon 
  • muzzle: String - Muzzle which was used 
  • mode: String - Current mode of the fired weapon
  • ammo: String - Ammo used

"_this" would be the EH itself, and select 0 would be the first element in the array. Which in this case would be the unit the EH is attached to. Select 1 would give you the weapon. Select 2 the muzzle type and so on.

Share this post


Link to post
Share on other sites

Expanding on what Maczer said:
 

Great, thanks for all the info guys. If you can't tell, I'm new-ish to all this! I've got the basics down, its just learning the more specific intricacies of things like this - it seems like it was the double speech marks that threw me, as i'd defined the fact that it was an "init" event handler.
 
Sorry to be asking more questions, but asking people who know what they're doing is probably more effective than struggling through random internet pages -  what does the "[_this select 0]" part do? I'm assuming that it specifies that the script is acting directly on the units it's being run from, but I don't really want to work with assumptions. Thanks again!

 
Double quotes: that's just a way to make sure the engine won't stop reading after it finds the second quote once it parses it. The content of EH in a config must be inside quotes (init=""), so if you write some code inside those quotes that also needs further quotes you must use double quotes ("") instead, so the engine knows the new quote is inside the other quote and shouldn't close it. Alternatively, you can also use curled braces ({}) or single quotes (') instead of double quotes ("") in most cases. But for configs I found that brackets wouldn't always work. More info on strings here.
 
_this select 0: All event handlers return their values as an array. Even if the init EH has only one element (the unit) you must still reference it as an element of an array. When we call the eh_infantry.sqs script we need to pass the unit we want to initialize all the EXT stuff on. As we're doing this in the call to the init EH (_this, the full init array) we must get the value of the unit from the init array (select 0, the first -and only- element of the init array). More info on arrays here. And on select here (spoiler: select is used to pick the value of an element of an array, as noted already).

Share this post


Link to post
Share on other sites

I hope You guys won't mind if I'd ask a question.

 

You need to define the init eventhandler of any new unit unit you create

 

So EH are not passed onto other units via class inheritance?

Share this post


Link to post
Share on other sites

I hope You guys won't mind if I'd ask a question.

 

 

So EH are not passed onto other units via class inheritance?

 

They are passed by inheritance.

  • Like 1

Share this post


Link to post
Share on other sites

They are passed by inheritance.

 

To clarify, as OP was referring to init EH regarding Ext units:

 

In Ext v1.0 onwards all infantry classes are derived from a handful "master" ones (which can be found in ww4ext_inf_cfg\base). Those parent classes only have a basic init EH (the one that I first posted in this thread). That means that if you create a child class from any of those, it will inherit the mentioned init EH. That also means that it will spawn without rucksack even if you place the corresponding logic.

 

To assign rucksack and ruck ammo to a new class you still have to define a new init EH there, where you make a full call (all parameters passed) to the eh_infantry.sqs script.

 

So, yes and no, in the context of Extended.

 

If the new classes bennysouthst is creating are children of the "base" classes then they will inherit the basic EH. There's no need to apply any new one (but, remember, no rucks).

 

If he wants the new classes to carry rucksacks OR he's creating new ones from scratch (maybe the parents being the vanilla WW4 infantry classes) then he has to define a new init EH, as pointed in my previous posts.

  • Like 1

Share this post


Link to post
Share on other sites

Ah, I understand - so the double quotations prevent the line from 'ending' prematurely when there's code the requires quotations inside other quotations, gotcha.

 

If he wants the new classes to carry rucksacks OR he's creating new ones from scratch (maybe the parents being the vanilla WW4 infantry classes) then he has to define a new init EH, as pointed in my previous posts.

 

 

That's exactly what i'm currently doing - I'm probably going to write out a pre-made "list" of backpacks and add them into the units. I noticed that WW4 EXT uses #include in its infantry config to pull the code from another location. Would doing this be viable in this situation? For example:

 

 

A file named RiflemanBackpack.hpp which consists of this, located in "CustomBackpacks.pbo"

class EventHandlers
{
[_this select 0, [<RucksackIwant class>],[<ammoIwant classes>]] exec "\WW4EXT_scripts\eh_infantry.sqs";
};

And then, in the actual unit config, I could use

class WW4_23RDRiflemanUCP:WWARifleman //Or whatever the actual classname is - pretend this one right.
{
displayname="(UCP) Rifleman";
vehicleclass=...
...
...

#include "\CustomBackpacks\RiflemanBackpack.hpp"
};

Assuming I understand this right, that would allow me to have all the backpack stuff in once place, allowing me to tweak them all without having to do it to every individual unit? Or have I greatly misunderstood something? :)

Share this post


Link to post
Share on other sites

It would be better to use #define.

 

In custombackpacks.pbo\mybackpackdefines.hpp

#define MY_COOL_EH_1 class EventHandlers { init = "[_this select 0, [myruck1],[myamo1]] exec ""\WW4EXT_scripts\eh_infantry.sqs""; ";};

#define MY_COOL_EH_2 class EventHandlers { init = "[_this select 0, [myruck2],[myammo2]] exec ""\WW4EXT_scripts\eh_infantry.sqs""; ";};

And then in infantry config.cpp:

//Include .hpp file on the beginning of infantry config.cpp
#include "\custombackpacks\mybackpackdefines.hpp"

//And then to CfgVehicles
CfgVehicles
{
    class Mycoolsoldier1:WW4_soldier
    {
        //Just type it like that - no quotes, no commas
        MY_COOL_EH_1
    };
    class Mycoolsoldier2:WW4_soldier
    {
        MY_COOL_EH_2
    };
};

Just note that 1.96 (OFP:Res) preprocessor have it's limitations when it comes to number of defines. 1.99 (A:CWA) uses updated (VBS?) preprocessor. WW4EXT uses a lot of defines, so to make it 1.96 compatible, config file is manually processed.

 

EDIT: a present from Faguss.

Share this post


Link to post
Share on other sites

As krzy says, you are better of using defines to do what you want to do. Inlcuding files so many time seems a bit overkill when you can include once a file with a list of defines.

 

I used this approach for a lot of configs in v1. It gives you extensive modularity and makes it easier to follow and edit them (at least it does for me). The only downside is OFP compatibility, as mentioned. While CWA can preprocess almost anything like a champ, good old OFP can't even start if you go overboard with all this (and I definitely went - once preprocessed ww4ext_inf_cfg.pbo is probably the longest config in the history of OFP/CWA).

 

Just note that if you give each infantry class (of the same "branch") different ruck ammo depending on role then the you're better off just manually writting the loadout of each one in their class area, or you'll need one define per role, which kind of ruins the point of a define. That's what I did for the infantry classes in v1, as you might have noticed already.

Share this post


Link to post
Share on other sites

I don't know whether this is best served in a new topic, but its the same file, so I thought i'd ask here.

 

After spending a few weeks troubling over a problem which turned out to be a spelling mistake :( i've managed to get everything together and in the right places. HOWEVER, upon launching the game with the mod installed, I get this error:

 

ww4_23rdusarm\config.cpp.weapons: Member already defined

 

Now, due to my time tinkering with other mods for private use, I know what this means - it pretty much explains itself, "Weapons" is already defined. The thing is, I haven't defined any weapons in the config - the phrase "weapons" is not present anywhere apart from in the original defines, as they've been added using #include. Curiously, i'm pretty sure that if a units weapon is already defined, the error would include the unit class name (it's been a while since i've had this though, so i could be wrong.) leading me to believe that its something I haven't experienced before. All units inherit their properties from "23RD_ARMY_BASE" (which has no weapons defined) which in turn inherits from "SoldierWB", which I believe is a BIS class. This effectively rules out it being a problem in the units.

 

So; anyone know where this "weapons" define is? 

Share this post


Link to post
Share on other sites

Unless I'm looking at the wrong config that class isn't there. Have you checked the one's referenced in the includes?

 

The error indicates a duplicate "weapons" entry in a class. Which can often happen when copy/pasting. 

 

By duplicate entry I mean something like this:

class Cw40k_RsTm: Cw40k_BaseSMarine                          ///Tactical Marine


 {
 Scope=2;
 vehicleClass="Space Marines-Red Scorpions";
 weapons[]={"throw","put","Cw40k_bolter","CW40k_FragHG","CW40k_KrakHG"};<<<<<<<<
 model="\Cw40k_SmRdS\u\Cw40k_RsTm";
 hiddenSelections[]={"AmmoHpr","Tab","Acc1","Acc2","cloth","seal","mk4Hlm"};
 side=1;
 displayName="Tactical Marine";
 weapons[]={"throw","put","Cw40k_bolter","CW40k_FragHG","CW40k_KrakHG"};<<<<<<<<
 magazines[]={"Cw40k_bolter","Cw40k_bolter","Cw40k_bolter","CW40k_FragHG","CW40k_FragHG","CW40k_KrakHG"};
 };

 I don't know the correct code terminology. But "member" refers to each of those entries/lines. Scope=, model=, side=, etc.

Share this post


Link to post
Share on other sites

If after Maczer advise you aren't able to solve this problem feel free to share your defines. The problem seems to be located there.

Share this post


Link to post
Share on other sites

I can't see any repeated members, but maybe i'm being blind. I'll post the includes now.

 

First, here's "usarmypack.hpp". This just defines the backpacks and I haven't define any weapons here, so I doubt its the issue, but i'm not ruling anything out:http://pastebin.com/bB47GSD1

 

This is where the problem may lie: http://pastebin.com/xmWu9qya. The weapons are defined here but, again, I don't see any repeats. Do you have to 'end' a define somehow before you move onto the next one or something? If so, that's probably my issue - that or I mis-spelt "define" somewhere and it continues including the info in the line below.

 

Any help is appreciated!

Share this post


Link to post
Share on other sites
ww4_23rdusarm\config.cpp.weapons: Member already defined

 

 

The error exists in a class in a config.cpp.

 

hpps are still part of the config.cpp. So error messages would point to a config in that case too.

 

Is it possible you introduced duplicates into WW4/WW4 EXT files while experimenting?

Share this post


Link to post
Share on other sites

I can't see any repeated members, but maybe i'm being blind. I'll post the includes now.

 

First, here's "usarmypack.hpp". This just defines the backpacks and I haven't define any weapons here, so I doubt its the issue, but i'm not ruling anything out:http://pastebin.com/bB47GSD1

 

This is where the problem may lie: http://pastebin.com/xmWu9qya. The weapons are defined here but, again, I don't see any repeats. Do you have to 'end' a define somehow before you move onto the next one or something? If so, that's probably my issue - that or I mis-spelt "define" somewhere and it continues including the info in the line below.

 

Any help is appreciated!

 

It's a syntax error.

 

The contents of the define must be in a single line. If you want to separate a define in several lines (for readability, etc) you must add this symbol  \ to the end of each line (even if it's an empty line)

 

This is wrong:

#define US_ARMY_AA
                        weapons[]={"WW4EXT_M4Aim","WW4_Beretta","Throw","Put","AALauncher"};
                        magazines[]={"WW4EXT_W556_30mag","WW4EXT_W556_30mag","WW4EXT_W556_30mag","WW4EXT_W556_30mag","AA Launcher","WW4_berettamag","WW4_berettamag","WW4_berettamag","WW4_berettamag"};
 
 
This is right:
#define US_ARMY_AA \
                        weapons[]={"WW4EXT_M4Aim","WW4_Beretta","Throw","Put","AALauncher"}; \
                        magazines[]={"WW4EXT_W556_30mag","WW4EXT_W556_30mag","WW4EXT_W556_30mag","WW4EXT_W556_30mag","AA Launcher","WW4_berettamag","WW4_berettamag","WW4_berettamag","WW4_berettamag"};
 
  • Like 1

Share this post


Link to post
Share on other sites

Sorry for misleading ye there Benny. 

 

I had no idea that was the case with defines. I better write that down somewhere. Cheers Kenoxite. ;)

Share this post


Link to post
Share on other sites

Sorry for misleading ye there Benny. 

 

I had no idea that was the case with defines. I better write that down somewhere. Cheers Kenoxite. ;)

Why should you apologize for trying to help? Don't be silly :p

  • Like 1

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×