Custom Variables

Started by Atelier, Mon 18/07/2011 11:43:41

Previous topic - Next topic

Atelier

I've searched it a bit and just checking, is it still impossible to have custom variables, or do I need to make my own array?

Code: ags

player.Age = 26;
cEgo.Height = "Six foot four";
etc

monkey0506

#1
You can't just add variables into existing structures like that, but there's a couple of ways you could get around the same thing:

1) Use underscores:

Code: ags
int player_Age = 26;
String cEgo_Height = "Six foot four";


2) Use a custom struct, with pointers to the built-in types:

Code: ags
// Script.ash
struct CharacterEx
{
  int Age;
  String Height;
  Character *AGSCharacter;
};

import CharacterEx characterEx[1000];

// Script.asc

CharacterEx characterEx[1000];
export characterEx;

characterEx[player.ID].Age = 26;
characterEx[cEgo.ID].Height = "Six foot four";


3) Use extender methods with dynamic arrays!:

Code: ags
// Script.ash

import void SetAge(this Character*, int age);
import int GetAge(this Character*);
import void SetHeight(this Character*, String height);
import String GetHeight(this Character*);

// Script.asc

int character_Age[];
String character_Height;

function game_start()
{
  character_Age = new int[Game.CharacterCount];
  character_Height = new String[Game.CharacterCount];
}

void SetAge(this Character*, int age)
{
  character_Age[this.ID] = age;
}

int GetAge(this Character*)
{
  return character_Age[this.ID];
}

void SetHeight(this Character*, String height)
{
  character_Height[this.ID] = height;
}

String GetHeight(this Character*)
{
  return character_Height[this.ID];
}


There is probably decent reasoning to suggest that you probably shouldn't name a String variable "Height" as that's likely to cause confusion, but hey, it's your code! :P

Edit: Also, you asked if it was "still" possible to create variables (in the way that you showed, linking them into existing structures), and so I just wanted to point out that I've been using AGS since 2.6 or 2.61, and can assure you that since the OO-model was introduced in 2.7 (actually while it was still in the 2.63 Beta stage of development, which was later renamed to 2.7), the method you described has never been possible. ;)

Atelier

#2
Ok thanks :)

Quote from: monkE3y_05_06 on Mon 18/07/2011 12:00:01There is probably decent reasoning to suggest that you probably shouldn't name a String variable "Height" as that's likely to cause confusion, but hey, it's your code! :P

Woops, didn't notice that :P Thankfully it was just an example I grabbed out the air.

edit

I'm curious, what is this pointer for?

Code: ags

Character *AGSCharacter;

monkey0506

Just for fun! ;D

It would basically give you a way to convert from your custom struct instances back to an AGS character. I can't really think of an immediate example where you would actually need to do that (where you couldn't just use the character array or an existing Character* directly), but it's there if you did need it. They should be initialized in game_start if you did want to use them though, like:

Code: ags
// game_start
int i = 0;
while ((i < 1000) && (i < Game.CharacterCount))
{
  characterEx[i] = character[i];
  i++;
}

SMF spam blocked by CleanTalk