Author Topic: TorqueScript Coding Help  (Read 977 times)

Hey there! I'm having a bit of trouble with a mod I'm making. What it does (or is supposed to do) is allow players to challenge each other to duels by typing commands into the chat with the format of /challenge playername #kills to win. The player that is challenged must then type /acceptchallenge or /declinechallenge. If they accept, the first person to get the specified # of kills wins the duel. All it does is keep track of kills and print things to the chat. Here's the code.

Code: [Select]
function serverCmdChallenge(%client, %targetName, %goal)
{
if(%goal <= 0)
return;

%player = %client.player;
%targetClient = findClientByName(%targetName);
%target = %targetClient.player;

if(isInMinigame(%player))
{
if(isInMinigame(%target))
{
%targetClient.challenged = true;
%targetClient.challenger = %client.name;
%targetClient.challengeGoal = %goal;

messageAll('', '\c2%1 has challenged %2 to a duel of %3 kills! Will he accept?', %client.name, %targetClient.name, %goal);
}
else
{
messageClient(%client, '', '\c2%1 is not in a minigame!', %targetClient.name);
}
}
else
{
messageClient(%client, '', '\c2You are not in a minigame!');
}
}

function serverCmdAcceptChallenge(%client)
{
//keep in mind %client and %target will be switched as opposed to what it was in serverCmdChallenge().
if(%client.challenged == false || %client.challenger == 0)
messageClient(%client, '', '\c2Nobody has challenged you!');
else
{
%client.challenged = false;
%player = %client.player;
%targetClient = %client.challenger;
%target = %targetClient.player;

%player.dueling = %target;
%target.dueling = %player;
%player.wins = 0;
%target.wins = 0;
%player.goal = %client.challengeGoal;
%target.goal = %client.challengeGoal;

messageAll('', '\c2%1 has accepted the duel challenge against %2! The goal is %3 kills!', %client.name, %target.name, %player.goal);
}
}

function serverCmdDeclineChallenge(%client)
{
//keep in mind %client and %target will be switched as opposed to what it was in serverCmdChallenge().
if(%client.challenged == false || %client.challenger == 0)
messageClient(%client, '', '\c2Nobody has challenged you!');
else
{
%client.challenged = false;
%client.challengeGoal = 0;

messageAll('', '\c2The cowardly %1 has declined the duel challenge against the courageous %2!', %client.name, findClientByName(%client.challenger).name);

%client.challenger = 0;
}
}

function Player::isDueling(%player, %target)
{
if(%player.dueling == %target
&& %target.dueling == %player)
return 1;
else return 0;
}

function GameConnection::onDeath(%this, %obj, %killer, %type, %area)
{
Parent::onDeath(%this, %obj, %killer, %type, %area);

%player = %this.player;
%target = %killer.player;

if(isDueling(%player, %target))
{
%target.wins++;
messageAll('', 'Duel Status: %1: %2' TAB '%3: %4', %this.name, %player.wins, %killer.name, %target.wins);

if(%target.wins == %target.goal) //if %target won
{
messageAll('', '%1 has won the duel against %2!', %killer.name, %this.name);
%player.dueling = 0;
%target.dueling = 0;
%player.wins = 0;
%target.wins = 0;
%player.goal = 0;
%target.goal = 0;
}
}
}

function IsInMinigame(%player)
{
if(isobject(%player))
{
if(%player.getclassname() $= "Player")
{
if(%player.client.minigame != 0 && %player.client.minigame != -1)
{
return 1;
}
}
if(%player.getclassname() $= "AiPlayer")
{
if(%player.spawnbrick.getgroup().client.minigame != 0 && %player.spawnbrick.getgroup().client.minigame != -1)
{
return 1;
}
}
return 0;
}
}
The problem that mainly occurs is I can challenge another player successfully, but then when that player types /acceptchallenge or /declinechallenge, it says "Nobody has challenged you!". Please help!

Note: The isInMinigame() function was taken from another mod, I do not take credit for it.

This is your main problem:

%targetClient.challenger = %client.name;

Later on you try to compare this to 0 and treat it like an object while it's just their name; set it to %client instead.

This is your main problem:

%targetClient.challenger = %client.name;

Later on you try to compare this to 0 and treat it like an object while it's just their name; set it to %client instead.
Thank you, that seems to have worked. However I've run into another problem. Overriding the onDeath function has created some problems, and when I kill the person I'm dueling or he kills me, whoever dies cannot respawn, and the chat glitches out. I tried moving the parent function call to the end of the onDeath function instead of the beginning, but that doesn't seem to have made a difference.

GameConnection::onDeaht() has to be on a package and parented.
Check out http://forum.blockland.us/index.php?topic=21811.0 to learn how to do it.

GameConnection::onDeaht() has to be on a package and parented.
Check out http://forum.blockland.us/index.php?topic=21811.0 to learn how to do it.

Ok, I put the onDeath function into a package and activated it in the server.cs file, but now nothing works, not even the commands like /challenge.

Did you check to see if the add-on loaded without syntax errors? Remember you need to close your package with }; not }

Did you check to see if the add-on loaded without syntax errors? Remember you need to close your package with }; not }

Oh wow...I totally forgot the semicolon. I got it working now, thanks so much for your help!