How to reload a weapon from script

burn

A Smooth-Skin
Modder
I want to make the dude automatically reload his weapon on combat end (like the companions do). I'm checking both command reference and companion scripts, but I don't see any reference to reloading weapons. Anyone has a clue?
 
You can't do it with regular scripts, AFAIK, only with sfall:

Code:
set_weapon_ammo_count(weapon, count);

But it won't be easy, because you have to take many factors into account: current ammo PID, magazine size, available ammo in inventory. I did this in Party Orders addon for NPCs when they switch ammo types. You could probably borrow that code (sources included in my mod installer, link in signature)
 
Ah so that's your mod that makes them do it. I'm playing with Restoration project, so I wasn't sure. Do I just take the relevant chunk of code and stick it on the top of obj_dude script, or there are more considerations?
 
Ah so that's your mod that makes them do it. I'm playing with Restoration project, so I wasn't sure. Do I just take the relevant chunk of code and stick it on the top of obj_dude script, or there are more considerations?

Party orders is not mine (which is included with RP), but I took it and rewritten some code, added some new stuff. Just study the procedure npc_switch_ammo in _pbs_main/gl_ptyop.ssl. It uses some additional procedures from headers. Ask if you will encounter difficulties.
 
Fallout scripting sure has steep learning curve... Looks like gibberish. I copied npc_switch_ammo procedure to obj_dude.int, and replaced "oC", which I presume means current NPC, with "op_dude_obj()". It won't compile now, saying "Error on line 2395: identifier expected after 'variable'". Line 2395 is "variable begin":
Code:
#define is_ammo_type(pid, type)        ((scan_array(ap_ammo_list, pid) != -1) == (type == AMMO_TYPE_AP))

procedure dude_switch_ammo(variable ammo_type) begin
variable begin
    i;
    weapon := 0;
    ammopid := 0;
    list;
    item;
    bestammo;
    caliber;
    magsize;
end
 
Which compiler are you using? Try script editor from latest sfall modderspack (it has sslc integrated). variable begin .. end is an extended syntax only available in sfall compiler (along with new functions required to switch ammo).
 
I see. That one works better, thanks. Now it complains about "undefined symbol item_type_weapon". From the looks of it, I should include define_lite.h, which has that define. But I don't see it included in gl_ptyop.ssl, either. And the error stays when the include when added, anyway.
 
Any pointers? Is there a guide about more advanced sfall scripting, maybe?
 
Any pointers? Is there a guide about more advanced sfall scripting, maybe?

Well, there is "sfall scripting" folder in sfall modderspack. Try reading sfall_function_notes and sfall_function_list, though the documentation is not very convenient, all information is there.
Also check out "Script Editor/docs/sslc readme.txt" it contains info about new syntax, though outdated.

I have updated some of the documentation in txts for the next sfall modderspack release.

"undefined symbol item_type_weapon"
define_lite.h is included in lib.inven.h, which is included id party orders script. If you need some header, just include it in your script, nothing bad will happen if it's already included somewhere.
 
define_lite.h is included in lib.inven.h, which is included id party orders script. If you need some header, just include it in your script, nothing bad will happen if it's already included somewhere.
The thing is, I already included it, it still complains. define_lite.h, compile.exe, obj_dude.ssl are all in the same directory. Is there something else I'm missing?
 
define_lite.h is included in lib.inven.h, which is included id party orders script. If you need some header, just include it in your script, nothing bad will happen if it's already included somewhere.
The thing is, I already included it, it still complains. define_lite.h, compile.exe, obj_dude.ssl are all in the same directory. Is there something else I'm missing?

Maybe you messed something's up with folder structure. If you use Script Editor, try Preprocess (F4) and see the result. Either you didn't include something or identifier names doesn't match.
 
You are using it from the command line? Have you read the help when running it without arguments? There is a key which enables preprocessing "-p".
Yes, I used it from command line, but I wasn't aware that including is called "preprocessing".

I'm including more and more stuff it requires... I start to wonder if it's even a good idea to dump all that into obj_dude script? Won't it cause slowdowns or other issues? I was hoping this idea could be implmented quick and dirty, but it doesn't seem to be the case.

In any case, right now I'm stuck with "Undefined symbol: is_ammo", which I can't find in any headers.
 
I'm including more and more stuff it requires... I start to wonder if it's even a good idea to dump all that into obj_dude script? Won't it cause slowdowns or other issues? I was hoping this idea could be implmented quick and dirty, but it doesn't seem to be the case.
As long as you use full compiler optimization (-O2) it won't matter how many headers you include, all unused code will not be included. Even without optimization, it's just bigger script file with a lot of unused code. Slowdowns can be caused by calling a lot of script functions inside global scripts with 0 delay (eg. set_global_script_repeat(0) ), so avoid doing this. Crashes are typically caused by invalid or zero object pointers passed to script functions, so add necessary checks for zero pointers. Also you need to remember that in SSL, unlike most other decent languages, all expressions in one condition are always executed. For example, in normal language like LUA you can write something like:
Code:
if (object != nil and object.weapon != nil and object.weapon.pid == PID_SPEAR) then ....
However in SSL similar condition will cause a crash if object is not a valid weapon pointer, since it will evaluate all sub-conditions even when first condition (object != nil) is false. In such cases in SSL I typically write:
Code:
if (object) then if (critter_weapon(object)) then if (obj_pid(critter_weapon(object)) == PID_SPEAR)
It looks ugly, but at least it's safe :)

In any case, right now I'm stuck with "Undefined symbol: is_ammo", which I can't find in any headers.
is_ammo macro is located in Zcustom.h header. It's likely I forgot to include it in mod sources, since it was taken from original Party Orders Addon sources.
I believe you need only this macros from the file (add them somewhere):
Code:
#define is_ammo(x)                        (obj_item_subtype(x) == item_type_ammo)
#define actual_ammo_count(crit, obj)   ((obj_is_carrying_obj_pid(crit, obj_pid(obj)) - 1)*get_proto_data(obj_pid(obj), PRODATA_IT_AM_PACK_SIZE) + get_weapon_ammo_count(obj))

PRODATA_IT_AM_PACK_SIZE may be PROTO_AM_PACK_SIZE depending on which version of header you used.
 
Slowly scrapping the code from both new and old versions of PartyOrders, I finally got this to the point where it compiles. Now it complains about "dude_switch_ammo" being unreferenced. I do have it in the beginning of the file:
Code:
procedure combat_p_proc;
procedure dude_switch_ammo(variable ammo_type);
procedure map_update_p_proc;
In PartyOrders, the procedure is called from another one, which processes pressed keys. Where'd I put it in obj_dude.ssl, if I want it to be done automatically, without any key presses?
 
Need to see the whole code to help you out. You may be interested that some russian modders already did that using sfall engine hacks (reload weapon after combat end), they also added a hotkey to reload current weapon.
I'm hesitating to add this to official sfall though, because you can script that.

A question, how do you plan to run reloading code exactly when combat ends? In dude_obj ?
 
Yes, that's what I'm doing, in obj_dude.ssl. Automatically, and I thought about about adding a hotkey option as well, but not until the first part works.

Although, it it's already done, I see no reason not include it. It's an option, and no one's forced to enable it, right?
 
It's easier for me to implement reloading via script myself than to adopt their changes, to be honest :D
The only thing I'm missing is how you plan to detect when combat ended?
 
Back
Top