Posted: Fri Feb 13, 2009 8:00 pm Post subject: OOT, destorying objects
Hi I can do this to make an object
code:
var Helmet1 : ^Helmet
new Helmet, Helmet1
my question is can I then remove that object?
example
code:
end Helmet, Helmet1
if yes, can you please give me the correct syntax to do so? Thank you
Sponsor Sponsor
Scott
Posted: Fri Feb 13, 2009 11:13 pm Post subject: Re: OOT, destorying objects
I didnt figure out my first post but I have another question so hopefully you can also help me with this. Well Im making an rpg and I have a shop, and a restock procedure. In that procedure it is supposed to make objects but I dont know how to get the computer to designate the object name. Ive also relized just now that I dont know how to get a program to make and create a variable.
Here is part of the code
code:
OBNAME := TYPE_ + intstr (NUMofITEM + 1) % The name of the object is created
var OBNAME : ^Weapon % I don't know how to use the variable OBNAME to name the new object.
new Weapon, OBNAME
Hopefully someone can help me. Thanks to anyone who helps even a bit .
michaelp
Posted: Mon Feb 16, 2009 3:40 pm Post subject: Re: OOT, destorying objects
For the first post: I think if you put that in a flexible array of 1, you would be able to get rid of it by setting the size to 0.
See here and here.
Hope that helps.
CodeMonkey2000
Posted: Mon Feb 16, 2009 4:41 pm Post subject: Re: OOT, destorying objects
For the first question try
code:
free Helmet, Helmet1
And for the second question, I'm not sure what you want. You should look through the Turing Walkthrough for tutorials on pointers and classes.
DemonWasp
Posted: Mon Feb 16, 2009 6:38 pm Post subject: RE:OOT, destorying objects
For the second question, you probably want arrays. They let you have multiple objects with a single name.
Scott
Posted: Mon Feb 16, 2009 7:37 pm Post subject: Re: OOT, destorying objects
Thanks so much DemonWasp I never even thought of applying arrays to that. I haven't tried the answers to the first question but I'm sure they will help thanks everyone.
A.J
Posted: Mon Feb 16, 2009 7:42 pm Post subject: Re: OOT, destorying objects
CodeMonkey2000 wrote:
For the first question try
code:
free Helmet, Helmet1
And for the second question, I'm not sure what you want. You should look through the Turing Walkthrough for tutorials on pointers and classes.
instead of :
code:
free Helmet, Helmet1
you can also do :
code:
free Helmet1
omitting the 'Helmet' also works too
copthesaint
Posted: Tue Feb 17, 2009 8:39 am Post subject: Re: OOT, destorying objects
Why don't you just use a bind statement?
code:
var n : int
loop
bind var item to n %you can bind multiple variables to each other
bind var item2 to n
item := 10
put item
put item2
exit when item = 10 or item2 = 10
end loop
%put item %The bind function doesn't work outside of loops/if's/case/ect
put n % but bind will change another variable.
The only problem is you need an external variable to bind too.
Sponsor Sponsor
DemonWasp
Posted: Tue Feb 17, 2009 9:13 am Post subject: RE:OOT, destorying objects
I'm assuming his problem was "I want to make some variable number of objects and have some way of referring to them", in which case bind doesn't do what he wants (bind is similar to how parameter-passing to procedures/functions works in Turing).
The way to do this is with arrays or flexible arrays (or a proper collection object...). This is how you solve the "inventory" problem; it makes the code a LOT simpler and easier to manage, and it certainly makes it easier to generalise to more kinds of objects.
Scott
Posted: Tue Feb 17, 2009 5:41 pm Post subject: Re: OOT, destorying objects
Demon Wasp is close to correcct, I want the game to create objects (instances) while its running. What im trying to do is make a shop in my text based RPG, That when you buy from the shop it needs to generate instance(object) and put it in the player inventory.
DemonWasp
Posted: Tue Feb 17, 2009 6:26 pm Post subject: RE:OOT, destorying objects
Example of exactly how to do this:
Let's assume that you have a base class "Item" from which all in-game items descend. Item probably has information about, for example, the item's weight and price, name and description and so forth. It WILL NOT have such things as weapon damage or armour benefits (for those, you would have a "Weapon" class and an "Armour" class, both of which would inherit from Item).
Your ShopKeeper class then looks like the following:
Turing:
class ShopKeeper
var items :array1..MAX_ITEMS ofpointerto Item
proc setUpShop () % In here, you need to initialise the shopkeeper's inventory. You can do this right in your Turing code if you wish, though there are better (read: more advanced) methods. new Sword, items(1)% Item #1 is a sword!
items(1) -> setDamage (5)% it deals 5 damage
new Shield, items(2)% Item 2 is a shield!
items(2) -> setDamageAbsorb (2)% it absorbs 2 damage per hit end setUpShop
fcn buyItem ( itemNumber :int, who :pointerto Player ):pointerto Item
if items ( itemNumber )=nilthen% If the player tries to buy something that doesn't exist, ignore them resultnil endif var item :pointerto Item := items ( itemNumber ) if player->hasGold ( item->price )then put"You buy the ", item->name, " for ",item->price," gold!"
player->takeGold ( item->price ) result item
else put"Sorry, you're poor!" resultnil endif end buyItem
end ShopKeeper
I haven't tested the above code, it's just an overview.
Scott
Posted: Tue Feb 17, 2009 8:00 pm Post subject: Re: OOT, destorying objects
:O Thank you soo much Demon Wasp. Yes I have it set up with Item as the top class but I never thought of it as a large array from which to make items from like that. That helps so much and now I can move on with my RPG . Hopefully it works but I doubt it wont, I'm going to get to work right away. Thanks again.
Also on a side note its not game progress stopping but if anyone knows the for sure answer that "free Helmet1" works please say.
DemonWasp
Posted: Tue Feb 17, 2009 9:30 pm Post subject: RE:OOT, destorying objects
AJ's right. You can new and free objects either with the class name spelled out for you:
code:
new ClassName, VariableReference
free ClassName, VariableReference
or you can new/free without the class name:
code:
new VariableReference
free VariableReference
The reason that one might use the first form is if you have a bunch of variables (for example, Item 's) but want to have them actually be Sword / Shield / etc.
Scott
Posted: Tue Feb 17, 2009 10:24 pm Post subject: Re: OOT, destorying objects
Thanks everyone, especially DemonWasp. All my questions have been answered and so the topic has been marked accordingly. Bye all you extremely helpful people.
DemonWasp
Posted: Tue Feb 17, 2009 10:32 pm Post subject: RE:OOT, destorying objects
Don't say bye!
Compsci.ca is a community of programmers from complete newb to expert. Come visit frequently, and if you have advice, feel free to contribute.