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

Username:   Password: 
 RegisterRegister   
 Pointer Issues
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Clayton




PostPosted: Mon Sep 04, 2006 10:04 am   Post subject: Pointer Issues

ok I'm starting to write an rpg but ive come into some issues with my pointers. Basically its with my add_weapon procedure, what's happening is that i check to see if an element of my weapon_inventory array has a value of nil(meaning the pointer is pointing at nothing), but every time i come to one of these conditionals, i get an error message saying that my variable has no value. I don't know why this is happening, because in my initialize procedure, i explicitly give my weapon_inventory array values of nil, so I don't understand what's going on here. I am also anticipating the same problem with my item_inventory which uses the same thing to add items to the inventory as well. Here is the code:

NOTE: My full code can be found here

Turing:

var weapon_inventory : array 0..3 of ^weapon
proc add_weapon (wpn : ^weapon)
    if weapon_inventory (upper (weapon_inventory)) not= nil then
        put "Too many Weapons"
        return
    end if
    for i : 0 .. 3
        if weapon_inventory (i) = nil then
            weapon_inventory (i) := wpn
            exit
        end if
    end for
    equip_weapon (weapon_inventory (0))
end add_weapon
body proc initialize (location : string, lvl : int)
    %take in base stats for the unit
    for i : 0 .. 3
        item_inventory (i) := nil
        weapon_inventory (i) := nil
    end for
end initialize
Sponsor
Sponsor
Sponsor
sponsor
ericfourfour




PostPosted: Mon Sep 04, 2006 10:47 am   Post subject: (No subject)

Are you sure its not wpn that is has no value? Also try using all weapons (from 0 to 3) individually to see which one has no value.
Clayton




PostPosted: Mon Sep 04, 2006 11:03 am   Post subject: (No subject)

i know its not wpn that has no value because i can use the weapon that is passed in the add_weapon procedure to another one called equip_weapon, and it works fine
Bored




PostPosted: Mon Sep 04, 2006 11:06 am   Post subject: (No subject)

It's not in that code. You actually aren't setting a nil value to the pointers. Why? Because though your Unit class does so your Mage_Character class does not when it overrights the initialize procedure. What i think you should do is maybe have a unit_initialize class that does all the stuff similar to all units, then have each of the initialize procs in the subclasses first call unit_initialize, then do there own thing. Yhis just makes less code, but it may mean rewriting your files as the mage specific stats must be at the bottom. But the asy way to get it working for now is to add
code:
for i : 0 .. 3
    item_inventory (i) := nil
    weapon_inventory (i) := nil
end for
to the mages initialize procedure.
Clayton




PostPosted: Mon Sep 04, 2006 11:42 am   Post subject: (No subject)

ahh thank you Bored, that was the problem, i should have seen that Embarassed oh well

+bits to you Very Happy
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  [ 5 Posts ]
Jump to:   


Style:  
Search: