Click to See Complete Forum and Search --> : .NET page lifecycle and custom events


Nightslyr
01-10-2009, 12:52 PM
I've asked this before, about a month ago, and no one answered me. Since I haven't found or come up with the answer myself, I figured I'd ask again.

I'm currently building the combat 'engine' for a browser-based game. Right now, I'm trying to get a very simple test combat loop to work. I have a hard-coded Button control which I've tied to a custom event like so:

protected void Page_PreLoad(object sender, EventArgs e)
{
this.attack.Click += delegate { this.PC.attack(0, sender, new AttackEventArgs(this.PC, this.Enemy)); };
}

this.attack is the Button control. this.PC and this.Enemy are the two combatants, and AttackEventArgs is an object that extends EventArgs, so I could use my attack method during the Click event.

The attack is being 'hooked' to the Click event just fine. I've checked it with the debugger, and the method is working as intended. My problem is that it's not being invoked at the right time.

Despite the Click event being set in PreLoad (and I've tried it in Init as well), it's not firing until after the output in Page_Load is rendered. So, upon clicking the attack button, the page reloads itself, but the attack method is processed after the output hits the screen. The output, therefore, is always 1 turn behind the current state of combat.

Is there any way to force the attack method to be invoked before the output is rendered? I thought sticking the event assignment in one of the lifecycle methods before Page_Load would do the trick, but it didn't.

If not, any idea on where to go in order to find a hint that can answer my problem? Or another forum that might be able to help me out? I cannot move forward on my project until this is working properly.

Thanks. :)

chazzy
01-10-2009, 07:44 PM
well, without knowing a single thing about how you've defined your button, what happens if you use an approach like this:


<script runat="server">
Sub attack(sender As Object, e As EventArgs)
'your attack code
End Sub
</script>

<asp:Button OnClick="attack" Text="Attack!!!!!!!!!11111one" runat="server" />

Nightslyr
01-11-2009, 04:02 PM
well, without knowing a single thing about how you've defined your button, what happens if you use an approach like this:


<script runat="server">
Sub attack(sender As Object, e As EventArgs)
'your attack code
End Sub
</script>

<asp:Button OnClick="attack" Text="Attack!!!!!!!!!11111one" runat="server" />


Well, the problem is that in the final, production stage of the project, I need to create the buttons programatically, and tie each one to the proper attack method, as I won't know how many attacks each player character will have available to them once combat starts (each character gains a new attack every 3 levels. So, I can't really 'preload' my attack methods within the .aspx file. I need to link them to the Click events of each Button the code-behind file creates at runtime.

In terms of my actual Button, is was simply defined as such in my .aspx file:
<asp:Button ID="attack" runat="server" Text="Basic Attack" />

My entire code-behind is simply:
using System;
using System.IO;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
private PlayerCharacter PC = null;
private PlayerCharacter Enemy = null;

public void AttackTarget(object sender, EventArgs e)
{
this.PC.Attack(0, sender, new AttackEventArgs(this.PC, this.Enemy));
//this.PC.Attacks[0].ExecuteAttack(this, new AttackEventArgs(this.PC, this.Enemy));
}

protected void Page_Init(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.PC = new PlayerCharacter("Neo", "Male", "A young hacker, unaware that he's the chosen one", new Hacker());
this.Enemy = new PlayerCharacter("Smith", "Male", "An agent of the Matrix", new Shill());
}
else
{
this.PC = (PlayerCharacter)Context.Session["attacker"];
this.Enemy = (PlayerCharacter)Context.Session["target"];
}
}

protected void Page_PreLoad(object sender, EventArgs e)
{
this.attack.Click += new EventHandler(this.AttackTarget);
//delegate { this.PC.Attack(0, sender, new AttackEventArgs(this.PC, this.Enemy)); };
}

protected void Page_Load(object sender, EventArgs e)
{
Vest vest = new Vest();

vest.Equip(this.PC);

mainLabel.Text = this.PC.Name + " " + this.PC.Gender + "\n<br />";
mainLabel.Text += "HP: " + this.PC.CurrentHP + " TP: " + this.PC.CurrentTP + " DMG: " + this.PC.DMG + "\n<br />";
mainLabel.Text += this.PC.Class.GetType().ToString() + "\n<br /><br />";
mainLabel.Text += "Attack info: <br />";
mainLabel.Text += "DMG: " + this.PC.DMG + " DMG Modifier " + this.PC.DMGModifier + " Chance to Hit: " + this.PC.ChanceToHit + "<br />";
mainLabel.Text += "Armor: " + ((Armor)this.PC.Armor).Defense + "<br /><br />";
mainLabel.Text += "Enemy HP: " + this.Enemy.CurrentHP + "<br /><br />";

Context.Session["attacker"] = this.PC;
Context.Session["target"] = this.Enemy;
}
}

As you can see, most of the code simply spits out character info to the screen. The actual .aspx file is even more simple:
<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/GitS.master" CodeFile="index.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" runat="server" contentplaceholderid="head">
</asp:Content>

<asp:Content ContentPlaceHolderID="mainContentPane" ID="mainContent" runat="server">
<div class="left" id="left" runat="server">
Left-hand stuff
</div>
<div class="content" id="content" runat="server">
<asp:Label ID="mainLabel" runat="server">
</asp:Label><br /><br />

<asp:GridView ID="attacks" runat="server">
</asp:GridView>

<!-- Need to figure out how to use my PlayerCharacter object as a datasource -->

<asp:Button ID="attack" runat="server" Text="Basic Attack" />

</div>
</asp:Content>

The GridView doesn't function yet, and may actually be removed (I wanted to put each attack button a character has within the control, but may simply use a table instead). Other than that, it's pretty self-explanatory.

chazzy
01-11-2009, 04:18 PM
So, I can't really 'preload' my attack methods within the .aspx file. I need to link them to the Click events of each Button the code-behind file creates at runtime.

That's only one way to look at it.

an alternate approach would be to look at the args in the click event to figure out which dynamic button was clicked. you should be able to use that information to figure out which button was clicked and what attack to perform.