Pointer Issues
Author |
Message |
Clayton
|
Posted: 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
|
|
|
ericfourfour
|
Posted: 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
|
Posted: 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
|
Posted: 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
|
Posted: Mon Sep 04, 2006 11:42 am Post subject: (No subject) |
|
|
ahh thank you Bored, that was the problem, i should have seen that oh well
+bits to you |
|
|
|
|
|
|
|