As many may know there is a major problem with DECORATE and action functions that get called from weapons. Due to the way a few functions were implemented - most notably the user variable stuff, these essentially end up with a 'self' pointer whose class does not match the calling object.
In any language, 'self' is supposed to be of the same class as the defining object, but when being called from weapon states this is not the case, so we end up with a huge problem once we allow to actually script action functions. As an example, lets assume you have a new weapon
Code: Select all
actor MyCoolWeapon : Weapon
{
int mycoolvariable;
}
Code: Select all
states
{
Fire:
A_MyCoolWeaponAttack("MyCoolProjectile");
}
Code: Select all
action void A_MyCoolWeaponAttack(class<Actor> projectile)
{
// do something interesting with the given projectile name.
}
Internally that function above gets defined as
Code: Select all
void A_MyCoolWeaponAttack(MyCoolWeapon self, Actor caller, StateInfo stateinfo, class<Actor> projectile)
But when called from a weapon state this will fail, because essentially it will get called like
Code: Select all
A_MyCoolWeaponAttack(player, self, stateinfo, "MyCoolProjectile");
My first idea was to use the second parameter, formerly 'stateowner' as self and rename the actual 'self' to 'caller'. But this causes other problems that, for example, calling A_Die from a weapon state, would try to kill the weapon, not the player.
The actual problem for this entire mess is actually, that there is not enough information to decide what a state is being used for until it is being called.
There is absolutely no way to tell at compile time to know if a state is being called from the weapon item itself or from the HUD sprite representing it, so it is impossible to make decisions based on the actual use case. With Dehacked in play this will get even nastier because it can change around state info at will and completely mess things up.
I believe this can be solved, but first I need to get an idea of how much added burden would be acceptable for the users here.
My personal view is to have a special qualifier for weapon-allowed action functions so that 'self' is of type Actor, but to make that work, the 'States' block needs to be broken up into one for regular states and one for inventory-releated states like weapons and CustomInventory because otherwise there is no way to decide where a state may be used.
Now, as long as functions are all native, this can be controlled, but with scripting this is either something the modder has to be aware of, or some clear rules have to be established that make it possible to recognise the different use type of states, and at the same time maintain DECORATE compatibility. And this is what I think needs discussion.