Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: scott11164 on Mon 17/03/2014 17:37:11

Title: How do i script attacks in an external file
Post by: scott11164 on Mon 17/03/2014 17:37:11
Hi this is my first post not sure if this should be in the advanced forum or not but here goes

what i am trying to do is make an adventure game where i have a player character that every now and again will have to battle through stages, the way i was going to do this is have an external text file for each of the characters with the name of the attack and the power of that attack, iv looked through some posts on the forum and had i look through the help files then give it my best shot but cant get it to work, here is my code please can some1 show me my amature errors ;-D;

Global Script
Code (ags) Select
function Attack_OnClick(GUIControl *control, MouseButton button)
{
int number;
File *input = File.Open("ScottAttack.txt", eFileRead);
number = input.ReadInt(2);
input.Close(0);
}


Text File
Code (ags) Select
# Scott-Attack
Bite: 10
Uppercut: 15
Lowblow: 15
}


At the moment this seems like im biting more than i can chew but if any1 can help me script this right then my demo game will be near completion :)
Title: Re: How do i script attacks in an external file
Post by: Khris on Mon 17/03/2014 22:18:41
I don't think you can use a parameter like that with File.ReadInt(), and the manual says: "Only integers written with File.WriteInt can be read back."
What you need is File.ReadRawLineBack(), then parse the lines.

You're also not doing anything with the number; and since it's a local variable, you can't access it from outside the function later.

There's a module somewhere in the modules and plugins section that can read .ini files, afaik. You could use that. I'm not sure why you would use external files like that in the first place though, and using them like that would also allow people to easily "hack" your game.
Title: Re: How do i script attacks in an external file
Post by: scott11164 on Mon 17/03/2014 22:30:46
Im new to this so i just thought that would be the the way to do it if i did want to have some sort of attack feature how would i go about doing it iv got a game on the local area i live and it could do with some sort of external file for each character on the game to have different attacks and with that each attack having a certain strength
Title: Re: How do i script attacks in an external file
Post by: Intense Degree on Tue 18/03/2014 12:56:10
Unless I'm missing something and you really do want external files for some reason, then it sounds like you want a struct. (Search that in the help file for more information).

Basically, declare a struct like this in the header:

Code (ags) Select
Struct CharacterAttack {
  int bite;
  int uppercut;
  int lowblow;
  String name; // this is completely unnecessary but might help you remember who is who.
};

CharacterAttack CAttack[9]; // replace 9 with the number of characters you want less 1, as the first one will be character 0.



Then, in the main script, you can define all the values:

Code (ags) Select
CAttack[0].bite = 10;
CAttack[0].uppercut = 15;
CAttack[0].lowblow = 15;
CAttack[0].name = “Scott”;
CAttack[1].bite = 12;
CAttack[1].uppercut = 13;
CAttack[1].lowblow = 17;
CAttack[1].name = “Phil”;


And so on until you have reached the right number of characters.

Then where you are coding the attacks, if Scott is using an uppercut you can get the value simply by putting:

Code (ags) Select
CAttack[0].uppercut

…in the relevant place.
Title: Re: How do i script attacks in an external file
Post by: scott11164 on Tue 18/03/2014 15:09:10
That seems like a simpler way thankyou for your reply ill give it a try tonight when i get home and update this if i come across any problems
Title: Re: How do i script attacks in an external file
Post by: Monsieur OUXX on Tue 18/03/2014 23:08:55
Intense degree's solution is the most straightfoward, because if you need to have those enemy stats loaded into your game, then you will need a structure like his "CharacterAttack" struct in your script, to store them.

The whole question is : how do you fill that sruct?
- you can fill it manually in the script, like he described.
- if you really need it to be filled from external, editable files, then the simplest solution is to use module "IniFile" (just search it on the forums). It's like what Khris said, except the module even does the text parsing for you.


You'd just create a file called "scott.ini" containing this :
Code (ags) Select

bite=10
uppercut=15
lowblow=15


Then, in your AGS script, use IniFile to read very easily from that file.
Title: Re: How do i script attacks in an external file
Post by: scott11164 on Wed 19/03/2014 09:44:59
Wow that is perfect just got to make it turn based now and my battle scene will be complete baring the music and sound effects thank for the enormous help much appreciated