Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Naming a variable with information saved within another variable.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
GreatPumpkin




PostPosted: Sat Dec 28, 2013 5:22 pm   Post subject: Naming a variable with information saved within another variable.

What is it you are trying to achieve?
I'm creating an overhead tile based game similar to civilization using CodeMonkey2000's tutorial for reference. Well I've passed what that tutorial can help me with a while ago. I'm to the point that there's a bit of a gui and I can add units, select them, move them around the map, and delete them flawlessly using arrays. I've also got the same system set up for cities, minus the moving. Now I'd like to make it so that the units belong to a city in specific. I'd like to make it so that in game an array is created, that is named unitsFromCityNo('selectedCity'). The information for each city and unit is held in a 2d array, selectedCity has the same value of whatever cities information you want to view/change. So the unitsFromCityNo('selectedCity') array would have a max depending on how many resources were used to build that city, and that is the maximum number of units that city could support. These arrays named and created in game would replace the single array I have to hold the units information, causing the units to "belong" to a city.

I'm also having problems reallocating the number of elements within a flexible array, I made a dummy script to practice with and it worked fine, but it doesn't seem to be working anywhere in this program, even if I put it outside of all if's loop's and proc's.

What is the problem you are having?
I'm not even quite sure where I'd start in solving this problem, I suppose it would be in learning the proper syntax for something like this:

code:
var selectedCity : int := 1
var cityNoUnits(selectedCity) : int


Describe what you have tried to solve this problem
I tried something like what I have above in a few different methods that were similar to displaying text with variable using put and Font.Draw, but none of them worked, so I erased the bad code before going to bed.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
It's quite a bit of coding for the entire game, so I'll only post relevant bits..

Turing:



%var cityInfo : array 1 .. maxCities, 1 .. 4 of int
var cityInfo : flexible array 1 .. 1, 1 .. 4 of int  %%% holds x/y activeflag, maxunits etc.


procedure addCity
    checkForOpenCity
    if openCityPointer <= maxCities then % if the citypointer is less than the number of allowed cities
        loop
            cursor

            if adjustY < maxy - 10 and adjustY > 10 and adjustX > 0 and adjustX < 590 and mouseB = 1 then  %%%check if unit is placeable
                cityInfo (openCityPointer, 1) := openCityPointer
                cityInfo (openCityPointer, 2) := adjustXdiv
                cityInfo (openCityPointer, 3) := adjustYdiv
                cityInfo (openCityPointer, 4) := 1
                new cityInfo, 5, 4     %%%Should reallocate 5 elements to the first part of the 2d array ?
%%%%%%%%%%%%CODE TO CREATE ARRAY NAMED AFTER SELECTED CITY GOES HERE%%%%%%%%%%%%%%%
                exit
            end if
        end loop
    elsif openCityPointer > maxCities then
        textToDisplay := 2
        displayingText := true
    end if
end addCity



Please specify what version of Turing you are using
<Answer Here>
code:
4.1.1
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Sat Dec 28, 2013 6:44 pm   Post subject: RE:Naming a variable with information saved within another variable.

It seems like you might be better suited by creating a City type that holds each field separately, rather than as an array of integers. Plus, then you can keep the unit identifiers (integers?) right in the same city type.

There's a downside: each instance of a type has to have the same size, so all the stored_units arrays will have the same (maximum) length. You'd have to make sure not to store more than max_units units in that city.

For example:
Turing:

const MAX_CITY_UNITS : int := 5   % the most units any kind of city can contain

type City :
    record
        x, y : int
        active : boolean
        name : string
        max_units : int
        stored_units : array 1 .. MAX_CITY_UNITS of int    % or whatever type you use to identify units
    end record

var cities : array 1 .. MAX_CITIES of City
Raknarg




PostPosted: Sat Dec 28, 2013 8:29 pm   Post subject: RE:Naming a variable with information saved within another variable.

If he's at the point where he needs more complex records, he might as well go and learn classes.

Just note GreatPumpkin if you go with records like DemonWasp is suggesting, yor cities cannot have flexible arrays. If you use classes, it is possible.
GreatPumpkin




PostPosted: Sat Dec 28, 2013 10:12 pm   Post subject: Re: Naming a variable with information saved within another variable.

Thanks for the replies, I ran into another problem, you can't reallocated 2d flexible arrays "yet" in turing. So I've decided to put a cap to the number of cities a player can use, 15, because the map isn't that big anyways, including the enemy cities that will be on the map, 30 cities is pretty much the max amount that it can hold. So I've decided using a 3d array, 15 * 10 * 4 for number of max cities, number of max units, and number of stats. And another variable within the city that will limit the number of units for that city.

Can you guys explain records and types? I haven't really ran into them yet..
Raknarg




PostPosted: Sat Dec 28, 2013 11:11 pm   Post subject: RE:Naming a variable with information saved within another variable.

A type is usually just a way to express a variable. It could be an integer, real number, boolean, string, etc. A record is a type that contains multiple data types inside. for instance:

Turing:

type personData :
    record
        name, gender : string
        age : int
        weight, height : real
        isHuman : boolean
    end record
   
var raknarg : personData

raknarg.name := "Raknarg"
raknarg.gener := "Male"
raknarg.age := 18
raknarg.weight := 3.50
raknarg.height := 50
raknarg.isHuman := false


Now the variable raknarg contains all the data attributed to the type of personData. It's a way to organize your information in a way that makes sense.

Also, please do not use multidimensional arrays to keep track of data like that. Just switch to records. It will make it easier for you to remember all your info, easier for other people to read it, easier to come back to it, easier to make changes, etc.
GreatPumpkin




PostPosted: Sun Dec 29, 2013 1:40 am   Post subject: RE:Naming a variable with information saved within another variable.

There could be up to 150 units on the map at any given time.. How would I go about using records for the units without pre writing all 150 possible units? using an array I can just set it 150 max, the first digit(s above 100) represent the city number, and the last digit represents the unit number.
Raknarg




PostPosted: Sun Dec 29, 2013 1:56 am   Post subject: RE:Naming a variable with information saved within another variable.

You mean like this?

Turing:

type cityUnitData :
    record :
        cityType : something %however you want to keep track of the unit's city
        %Other info about the unit
    end record
   
type cityData :
    record
        cityUnit : array 1 .. 150 of cityUnitData
        %whatever things to keep track of in a city
    end record
   
var city : array 1 .. 10 of cityData %now you have 10 cities.


Note I said cityUnitData, normally I would say unit but that is a keyword in turing
GreatPumpkin




PostPosted: Tue Dec 31, 2013 8:57 pm   Post subject: RE:Naming a variable with information saved within another variable.

I think that will interact with how I check for open units and add them, using arrays is working just fine right now, there are other problems I have to overcome and trying to learn a replacement concept won't help.

Also, I don't really see how I could even start implementing that into my engine-in-porgress
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Wed Jan 01, 2014 1:35 am   Post subject: RE:Naming a variable with information saved within another variable.

Sometimes revamp is better than continuing to forge on. How would that not fit in? All it is doing is organizing code you theoretically should have. You can change it how you like.
GreatPumpkin




PostPosted: Wed Jan 15, 2014 9:50 pm   Post subject: Re: Naming a variable with information saved within another variable.

I know how I could draw and edit the information in the records, but I don't understand how I can reference the information from other variables and procedures.

Such as selecting the unit:

Turing:

proc selUnit
    Input.Flush
    Font.Draw (("Select unit to activate."), 620, 40, font3, red)
    locatexy (615, 45)
    getch (c)                 %%%%%TO BE REPLACED WITH MOUSE SELECTOR
    put strint (c)
    selectedUnit := strint (c)
end selUnit



and my remove unit procedure:

Turing:

procedure removeUnit
    units (Array ((unitInfo (selectedUnit + selectedCity * 10, 2)), (unitInfo (selectedUnit + selectedCity * 10, 3)))) := 0
    unitInfo (selectedUnit + selectedCity * 10, 1) := 0                 %OPEN
    unitInfo (selectedUnit + selectedCity * 10, 2) := 0                 %X
    unitInfo (selectedUnit + selectedCity * 10, 3) := 0                 %Y

end removeUnit


The only way I can see to switch between units would be to name the record using information in my openUnitCounter (the same as the openCityCounter in my first post, just referencing unit information instead of city info). However this is self reflection and from what I've read turing is incapable. In order to continue without arrays I'd need to use another language or throw away important aspects of the game. Something I thought of comparing it to is projectiles, an array can hold the x, y, velocity, and damage that it causes, it wouldn't make sense to use a record.


I've looked at catalysts tutorial on classes and it makes absolutely no sense to me whatsoever. I don't really understand what a class is or what it does, it seems like that tutorial forgets to explain what a class is used for..
Raknarg




PostPosted: Thu Jan 16, 2014 2:07 am   Post subject: RE:Naming a variable with information saved within another variable.

You should clarify what you just said. There is no difference between this:

Turing:

type personData :
   record
      name : string
      age : int
      isMale : boolean
var person : personData


and this:
Turing:

var name : string
var age : int
var isMale : boolean


As for what classes are used for, they're used to encapsulate methods and variables together, called an "object". An object can be anything you want. For instace, I could have a ship class which has this stuff in it:

Turing:

class Ship
   var x, y, hp : int
   var bullet : array 1 .. 10 of Bullet

   proc make_ship(x_, y_, hp_ : int)
      x := x_
      y := y_
      hp := hp_
   end make_ship

   proc shoot
      shoot_bullet()
   end shoot


Its like a record with procedures and functions. I could make a ship which has all that data in it. if I had a variable named playerShip and I wanted to initialize it, I could call playerShip -> make_ship(). I could also call playerShip -> shoot() if I wanted to make it shoot a bullet.
GreatPumpkin




PostPosted: Thu Jan 23, 2014 7:06 pm   Post subject: Declaring a variable named after a string while program is running?

I've checked out classes and records more, and I'm understanding them better, but I've ran into the same problem mentioned in my original post. I have a dummy script that has a class, within the class is an array of a custom type (unitInfo). The array has 8 elements, one for each possible unit. All seems well with the single set of 8 units, so it's time to rewrite the rest of what I have done, the cities and resources. The resources should be more simple than the cities.
I'd like the name of the variable that points to group of units after the city it belongs to, while the program is running.
So a starting point I'd need is

Turing:


var currentCity : string := '1'

var city(currentCity)Units : int



That however returns : Syntax error at '('. Expected 'identifier'.

I'd like to avoid having to manually declare every possible city and set of units that may be used in a game. Is this possible ?
Nathan4102




PostPosted: Thu Jan 23, 2014 8:06 pm   Post subject: RE:Naming a variable with information saved within another variable.

You are not able to name a variable using another variable, AFAIK. You'll have to do it another way, perhaps use an array?
Raknarg




PostPosted: Thu Jan 23, 2014 9:18 pm   Post subject: RE:Naming a variable with information saved within another variable.

GreatPumpkin, that kind of thing is exactly what you would use arrays and records for.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: