Author Topic: how to make a variable name out of a few strings  (Read 1986 times)

0 Members and 1 Guest are viewing this topic.

Offline -Snowy-

  • Major(1)
  • Posts: 44
    • Snowy
how to make a variable name out of a few strings
« on: January 15, 2008, 07:22:27 am »
problem fixed :D

Quote
what im trying to do here is store the team that a player is on, so that i can put them back onto that team if they try to change using the team changing menu, which is not allowed in my zombie script (currently they are changed to spectator mode when they do this, i want them to go back to the team they were already on)

this isnt my exact code, but its basically what im trying to do


Code: [Select]
var
slot1team: integer;
slot2team: integer;
slot3team: integer;
slot4team: integer;
slot5team: integer;
slot6team: integer;
slot7team: integer;
slot8team: integer;
slot9team: integer;
slot10team: integer;
slot11team: integer;
slot12team: integer;
slot13team: integer;
slot14team: integer;
slot15team: integer;
slot16team: integer;
slot17team: integer;
slot18team: integer;
slot19team: integer;
slot20team: integer;
slot21team: integer;
slot22team: integer;
slot23team: integer;
slot24team: integer;
slot25team: integer;
slot26team: integer;
slot27team: integer;
slot28team: integer;
slot29team: integer;
slot30team: integer;
slot31team: integer;
slot32team: integer;

procedure OnJoinTeam(ID, Team: byte);
begin
'slot' + inttostr(ID) + 'team' := Team;
end;

so for example when player 1 joins team 2 then it should make slot1team := 2; or if player 5 joins team 0 then it should make slot5team := 0;. only problem is that the server wont run with the line 'slot' + inttostr(ID) + 'team' := Team; and i dont know the proper way to do it. i know i could go through and say if id = 1 then begin slot1team := Team; for every single player but that would just take up time and space.

so basically i need to know how to make a variable name out of a few strings put together then change the variable to the player's team. or if someone knows a way to tell the team that a player was coming from onjointeam that would be just as helpful.

Offline Avarax

  • Veteran
  • *****
  • Posts: 1529
    • Official Hexer & MMod site
Re: how to make a variable name out of a few strings
« Reply #1 on: January 15, 2008, 07:33:27 am »
Use an array.

var Player: array[1..32] of byte;
I like to have one Martini
Two at the very most
Three I'm under the table
Four I'm under the host

Offline -Snowy-

  • Major(1)
  • Posts: 44
    • Snowy
Re: how to make a variable name out of a few strings
« Reply #2 on: January 15, 2008, 08:23:48 am »
i had a look around for how to use arrays and this is what i came up with

Code: [Select]
var
Playerteam: array[1..32] of byte;

procedure OnJoinTeam(ID, Team: byte);
begin
Playerteam[ID] := 'Team';
end;

too bad i have no idea what im doing, because then id be able to see what ive done wrong here, because its not working for me


edit: dw it was the ''s around Team

edit again: another problem, how would i use the number in the array in a command or something, eg when i put them back to the team they were on. i tried

Code: [Select]
Command('/setteam' + Playerteam[ID] + ' ' + InttoStr(ID));
but the server crashed, what should i do?

edit: my bad again, another technical error, i forget inttostr

Offline FliesLikeABrick

  • Administrator
  • Flamebow Warrior
  • *****
  • Posts: 6144
    • Ultimate 13 Soldat
Re: how to make a variable name out of a few strings
« Reply #3 on: January 15, 2008, 12:08:03 pm »
Use an array.

var Player: array[1..32] of byte;

I agree with Avarax.  Almost any time that a person wants to create variable names from other variables, they should really be using an array.  There are very, very few circumstances where dynamically-named variables are actually needed

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: how to make a variable name out of a few strings
« Reply #4 on: January 15, 2008, 04:03:25 pm »
Use an array.

var Player: array[1..32] of byte;

I agree with Avarax. Almost any time that a person wants to create variable names from other variables, they should really be using an array. There are very, very few circumstances where dynamically-named variables are actually needed

And anywhere dynamically-named variables are needed, you can just use an ordered map type of data structure, like a dictionary or other, which is exactly how dynamically-named variables are dealt with internally in the languages which use them in the first place. The only difference is you would need to write your own supporting code instead of it being provided as a convenience.

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
Re: how to make a variable name out of a few strings
« Reply #5 on: January 15, 2008, 08:16:03 pm »
Can you turn a string into a variable name with this scripting engine?

just wondering...

btw I like your avatar chris, foxes own all
« Last Edit: January 15, 2008, 08:23:56 pm by Kavukamari »
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: how to make a variable name out of a few strings
« Reply #6 on: January 15, 2008, 09:17:36 pm »
Can you turn a string into a variable name with this scripting engine?

just wondering...

btw I like your avatar chris, foxes own all
No, although I am working on a sort of system that you can do SetVariable('test', 8) and stuff, wrote it up but havn't found the time to get it working yet :P (can't do test := 8 though... must use the functions to access the variables)

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: how to make a variable name out of a few strings
« Reply #7 on: January 16, 2008, 01:35:31 am »
Can you turn a string into a variable name with this scripting engine?

just wondering...

btw I like your avatar chris, foxes own all

No you can't, and yes, I <3 my avatar :D.

Can you turn a string into a variable name with this scripting engine?

just wondering...

btw I like your avatar chris, foxes own all
No, although I am working on a sort of system that you can do SetVariable('test', 8) and stuff, wrote it up but havn't found the time to get it working yet :P (can't do test := 8 though... must use the functions to access the variables)
Code: [Select]
type
  TVariable = record
    name: string
    value: variant
  end;
variables: array of TVariable;

function find(name: string): integer;
begin
  for result := 0 to arrayhigh(variables) do /
    if variables[result].name = name then exit;
  result := -1;
end;

procedure set(name: string; value: variant);
var
  pos: integer;
begin
  pos := find(name);
  if pos > -1 then
    variables[pos].value := value
  else begin
    setlength(variables, arrayhigh(variables) + 1);
    Variables[arrayhigh(variables)].value := value;
    Variables[arrayhigh(variables)].name := name;
  end;
end;

function get(name: string): variant;
var
  pos: integer;
begin
  result := 'Not found';
  pos := find(name);
    if pos > -1 then
      result := variables[pos].value;
end;

More or less in theory how to do it, haven't tried it to see if it even works or not. Probably needs some adjustment to use scripting functions, as I'm used to delphi and not the scripting engine.
« Last Edit: January 16, 2008, 01:39:39 am by chrisgbk »

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: how to make a variable name out of a few strings
« Reply #8 on: January 16, 2008, 06:17:48 am »
Pretty much exactly how mine works

Offline EnEsCe

  • Retired Soldat Developer
  • Flamebow Warrior
  • ******
  • Posts: 3101
  • http://enesce.com/
    • [eC] Official Website
Re: how to make a variable name out of a few strings
« Reply #9 on: January 20, 2008, 10:24:42 pm »
Or, I could just add the SetVar function to the scripting engine, in one line. lolol.

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
Re: how to make a variable name out of a few strings
« Reply #10 on: January 21, 2008, 01:50:23 am »
Or, I could just add the SetVar function to the scripting engine, in one line. lolol.

please :) (we need that very much)
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: how to make a variable name out of a few strings
« Reply #11 on: January 21, 2008, 10:08:56 am »
Or, I could just add the SetVar function to the scripting engine, in one line. lolol.
lol i thought i (and others) already suggested that a while ago ^^

Well ill just list a few suggestions going along with the same function:

function GetVariable(Name: string): variant;
procedure SetVariable(Name: string; Value: variant);
procedure DefVariable(Name: string; Datatype: string);
procedure UndefVariable(Name: string);
function IsDefined(Name: string): boolean;
function GetDatatype(Name: string): string;

Don't know whats possible or what isn't but, might as well list em all..