Page 1 of 1

Returning to child class after "goto super::"

Posted: Mon Apr 25, 2016 11:48 am
by GleasSpty
I am inheriting one class (child_class) from another (parent_class), and parent_class has two states, one of which (state2) should run almost the same in child_class but the other (state1) should run totally differently. To achieve this, I rewrote state1 in the child from scratch and in state2 I wrote one extra piece of code followed by a goto super::state2. The problem is that, after using goto super::state2, further goto statements (in the parent class) will not go to the child's states (and FWIW, I suppose that's what one would expect), resulting in unintended behavior.

To clarify, consider the following code. After a thing of class child_class goes into state2, the *desired* behavior is for it to print
  • "state2 on the other hand is just a small modification of the parent's state2."; followed by
    "parent_class is in state2"; followed by
    "The child class needs to do something totally different in state1.".
On the other hand, what actually happens (disclaimer: I have not tested the code below itself) is that it prints
  • "state2 on the other hand is just a small modification of the parent's state2"; followed by
    "parent_class is in state2"; followed by
    "parent_class is in state1".

Code: Select all

actor parent_class
{
	States
	{
		state1:
			TNT1 A 0	// This is necessary in a spawn state.  It's not necessary in general though, is it?
			TNT1 A 0 A_PrintBold("parent_class is in state1.")
			loop
		state2:
			TNT1 A 0
			TNT1 A 0 A_PrintBold("parent_class is in state2.")
			goto state1
	}	
}

actor child_class:  parent_class
{
	States
	{
		state1:
			TNT1 A 0
			TNT1 A 0 A_PrintBold("The child class needs to do something totally different in state1.")
			// New completely different code goes here.
			loop
		state2:
			TNT1 A 0
			TNT1 A 0 A_PrintBold("state2 on the other hand is just a small modification of the parent's state2.")
			goto super::state2	
	}	
}
One fix to this is obvious: simply just copy and paste all the code for parent_class::state2 in place of "goto super::state2". The problem with this, however, is that it is incredibly tedious. In the example I am interested in, this would involve copy+pasting ~1000 lines of code.

Is there a more efficient way to accomplish what I would like? (Note that the solution can only involve modification of the child class---the parent class is part of a separate mod that is not mine.)

Re: Returning to child class after "goto super::"

Posted: Mon Apr 25, 2016 11:56 am
by NeuralStunner
Use a "soft goto" in the parent:

Code: Select all

TNT1 A 0 A_Jump (256, "state1")
This will always jump to the "newest version" of the state.