1 registered members (AbrahamR),
717
guests, and 4
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Use variable in separate script?
#441692
05/31/14 09:47
05/31/14 09:47
|
Joined: Jun 2010
Posts: 590 California
Ruben
OP
User
|
OP
User
Joined: Jun 2010
Posts: 590
California
|
I am using a main() script and an inventory.c script. I use the inventory.c script to provide an inventory bag for the player to bring up when the player taps the "i" key. I am trying to alter the inventory.c script code so that if I click on any item in the inventory bag (shown on the left side of the screen), a detailed description of that item shows up on the right side of the screen, such as a large image of that item, plus descriptive information underneath it such as weight, damage, value, and effects. If the player has gold in its inventory bag, a small image of gold coins shows in one of the slots of the inventory bag. If the player clicks on the small gold coins image, I have it programmed so that a large image of gold coins shows up on the right side of the screen. I also want to display the amount of gold that the player has, underneath the large image of gold coins that shows up on the right side of the screen. The variable "player_gold" is used and calculated in the main.c program. However, I would like to try and use the same player_gold variable in the inventory.c program as well. Is there a way that I can access the value stored in "player_gold" from the main.c script, to be used in the inventory.c script? Here is my code so far:
// main.c
...
#include "inventory.c"
...
int player_gold;
...
function gldCn_event()
{
if ((event_type == EVENT_CLICK) && (my.STATE == 1))
{
ent_remove(gldCn);
inv_insert_item_into_bag(bag,item_gold);
player_gold += 1;
gldCn_picked_up = 1;
change_mouse_mode();
my.STATE = 2;
}
}
...
action gldCn_action()
{
my.emask |= ENABLE_CLICK;
my.event = gldCn_event;
gldCn_picked_up = 0;
//player_gold += 5;
my.STATE = 1;
while (1)
{
if (!mouse_left && my.STATE == 2)
{
my.STATE = 1;
}
if (my.STATE == 1)
{
if (c_scan(my.x,my.pan,vector(360,0,150),SCAN_ENTS | SCAN_FLAG2 | IGNORE_ME))
{
mouse_map = cursor_green_pcx;
}
else
{
mouse_map = cursor_pcx;
}
}
wait(1);
}
}
...
PANEL* pDisplay =
{
...
digits (180, 190, 5, *, 1, plyr_gold);
...
flags = SHOW;
}
...
function show_player_gold()
{
plyr_gold = player_gold;
}
...
action player()
{
...
while(my.x)
{
...
show_player_gold();
...
}
}
...
void goldCoin_present()
{
if (gldCns_picked_up == 0)
{
gldCn = ent_create ( "gold_coin.mdl",
vector(713,-1870,1709), gldCn_action);
gldCn_scale();
}
}
...
function gldCns_scale()
{
gldCns.scale_x = 0.07; // MAIN LEVEL PLAYER SCALE
gldCns.scale_y = 0.07;
gldCns.scale_z = 0.07;
}
function gldCn_scale()
{
gldCn.scale_x = 0.07; // MAIN LEVEL PLAYER SCALE
gldCn.scale_y = 0.07;
gldCn.scale_z = 0.07;
}
...
void goldCoin_present()
{
if (gldCns_picked_up == 0)
{
gldCn = ent_create ( "gold_coin.mdl", vector(713,-1870,1709), gldCn_action);
gldCn_scale();
}
}
...
function main()
{
...
player_gold = 0;
...
}
// inventory.c
...
BMAP* goldPanel = "Gold_Inv_Display.pcx";
...
TEXT* gold_amt_text =
{
pos_x = 560;
pos_y = 400;
string ("GOLD: ");
}
PANEL* gold_panel =
{
window(600,25,350,350,goldPanel,200,300);
}
function slotOnClick(PANEL* clicked_panel)
{
Slot* clicked_slot = getSlotPtrForPanelPtr(clicked_panel);
Item* clicked_item;
FONT* gold_txt_font = font_create("Times#30i");
STRING* gold_amt_txt;
gold_amt_txt = str_create("Gold: ");
pan_setstring(gold_panel,0,560,400,gold_txt_font,gold_amt_txt);
pan_setdigits(gold_panel,0,500,400,"%2.f",gold_txt_font,1,player_gold);
...
}
If I tried putting this in inventory.c: ...I get weird errors. I do not get weird errors if I include this in main.c" Is there a way that I can access the player_gold variable value from main.c, to be used in inventory.c , so that I can show the player's amount of gold under the large gold coins image on the right side of the screen that shows up when the player clicks on the small gold coins image in the inventory bag that shows up on the left side of the screen?
Last edited by Ruben; 05/31/14 10:00.
|
|
|
Re: Use variable in separate script?
[Re: Ruben]
#441694
05/31/14 10:38
05/31/14 10:38
|
Joined: Dec 2011
Posts: 1,823 Netherlands
Reconnoiter
Serious User
|
Serious User
Joined: Dec 2011
Posts: 1,823
Netherlands
|
Inventory.c script is getting famous by now . Make an additional script file called 'variables.c' (without the quotes) or an other name you prefer. Place the variable player_gold in there instead of in your main script. Now add before you the #include "inventory.c" line.
|
|
|
Re: Use variable in separate script?
[Re: Reconnoiter]
#441696
05/31/14 11:08
05/31/14 11:08
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
The most important thing you should know is the way #include works: The #include directive tells the preprocessor to treat the contents of a specified file as if those contents had appeared in the source program at the point where the directive appears. In other words, the compiler replaces all the #include directives by the content of the specified files and builds a single script before compilation. So there is no such a script file rating on the run. An explanatory example of the #include directive behavior you should never use:
// main.c
#include <acknex.h>
function main ()
{
wait(1);
while ( !key_esc )
{
#include "main_loop.c"
}
sys_exit ( NULL );
}
// main_loop.c
draw_text ( "test", 0, 0, COLOR_WHITE );
wait(1);
|
|
|
Re: Use variable in separate script?
[Re: txesmi]
#441705
05/31/14 16:45
05/31/14 16:45
|
Joined: Jun 2010
Posts: 590 California
Ruben
OP
User
|
OP
User
Joined: Jun 2010
Posts: 590
California
|
What I tried doing was declaring "player_gold" variable before #including "inventory.c" in main.c , as shown here:
// main.c
int player_gold;
...
#include "inventory.c"
In my inventory.c , I am attempting to add an integer variable value next to a string (by showing the string "Gold: ", and player_gold next to it), as shown here:
// inventory.c
...
PANEL* gold_panel =
{
window(600,25,350,350,goldPanel,200,300);
}
function slotOnClick(PANEL* clicked_panel)
{
Slot* clicked_slot = getSlotPtrForPanelPtr(clicked_panel);
Item* clicked_item;
FONT* gold_txt_font = font_create("Times#30i");
STRING* gold_amnt_txt;
gold_amnt_txt = str_create("Gold: ");
pan_setdigits(gold_panel,0,610,400,"%2.f",gold_txt_font,1,player_gold); // GIVING OFF ERROR
pan_setstring(gold_panel,0,598,400,gold_txt_font,gold_amnt_txt);
if (clicked_slot == NULL) { diag("\n*** Error: panel not found\n"); }
if (global_floating_item_ptr->item == NULL) // player clicked on item in inventory while not floating an item
{
if ( event_type == EVENT_CLICK ) // mouse left button event
{// -------------------------------------------------------------------------
PANEL* item_pnl_descr;
int gold_added = 0;
if(str_cmp(clicked_slot->item->floating_icon, "steel_sword.pcx"))
{
reset(mace_panel, SHOW);
reset(gold_panel, SHOW);
set(sword_panel, SHOW);
}
else if(str_cmp(clicked_slot->item->floating_icon, "mace.pcx"))
{
reset(sword_panel, SHOW);
reset(gold_panel, SHOW);
set(mace_panel, SHOW);
}
else if(str_cmp(clicked_slot->item->floating_icon, "gold_coins.pcx"))
{
if(gold_added == 0)
{
gold_added = 1;
reset(sword_panel, SHOW);
reset(mace_panel, SHOW);
set(gold_panel, SHOW);
}
}
if (clicked_slot->item != NULL) // Player is grabbing an item out of the inventory
{
// optionally callback with action, bag_id, slot_id, item_id
if (on_click_callback_function_ptr)
on_click_callback_function_ptr(INV_ITEM_REMOVED,clicked_slot->bag_id,clicked_slot->id,0,clicked_slot->item->id);
// Create the floating panel
global_floating_item_ptr->floating_item_panel = pan_create("",floating_item_layer);
// Set the item and panel image of the floating item structure. This effectively "floats"
// the item that was stored in the inventory slot.
global_floating_item_ptr->item = clicked_slot->item;
global_floating_item_ptr->floating_item_panel->bmap = bmap_create(clicked_slot->item->floating_icon);
global_floating_item_ptr->floating_item_panel->flags |= VISIBLE;
// Set the item pointer of the clicked on slot to NULL
clicked_slot->item = NULL;
// Restore inventory slot image to be the default background image
clicked_slot->slot_panel->bmap = bmap_create(clicked_slot->background_image);
inv_floating_flag = 1;
_inv_do_float(global_floating_item_ptr);
}
}
// 4/16/2014 Addition -----------------------------------------------------
else if ( event_type == EVENT_RIGHTCLICK ) // mouse right button event
{
if (clicked_slot->item != NULL)
{
if (on_click_callback_function_ptr)
on_click_callback_function_ptr(INV_ITEM_RIGHT_CLICK,clicked_slot->bag_id,clicked_slot->id,clicked_slot->item->id,0);
}
}
// -------------------------------------------------------------------------
}
else
{
if ((clicked_slot->group_id == 0) || (clicked_slot->group_id == global_floating_item_ptr->item->group_id)) // Test to make sure that item can be placed in the slot
{
if (clicked_slot->item == NULL) // player is putting an item into an empty inventory slot
{
reset(sword_panel, SHOW);
reset(mace_panel, SHOW);
reset(gold_panel, SHOW);
// optionally callback with action, bag_id, slot_id, item_id
if (on_click_callback_function_ptr) on_click_callback_function_ptr(INV_ITEM_PLACED,clicked_slot->bag_id,clicked_slot->id,global_floating_item_ptr->item->id,0);
// Place item in slot
clicked_slot->item = global_floating_item_ptr->item;
clicked_panel->bmap = bmap_create(global_floating_item_ptr->item->inventory_icon);
global_floating_item_ptr->item = NULL;
// End float
pan_remove(global_floating_item_ptr->floating_item_panel);
inv_floating_flag = 0;
}
else // player is swapping the floating item for the one in the inventory
{
if(str_cmp(clicked_slot->item->floating_icon, "steel_sword.pcx"))
{
reset(mace_panel, SHOW);
reset(gold_panel, SHOW);
set(sword_panel, SHOW);
}
else if(str_cmp(clicked_slot->item->floating_icon, "mace.pcx"))
{
reset(sword_panel, SHOW);
reset(gold_panel, SHOW);
set(mace_panel, SHOW);
}
else if(str_cmp(clicked_slot->item->floating_icon, "gold.pcx"))
{
reset(sword_panel, SHOW);
reset(mace_panel, SHOW);
set(gold_panel, SHOW);
}
// optionally callback with action, bag_id, slot_id, item_id
if (on_click_callback_function_ptr) on_click_callback_function_ptr(INV_ITEM_SWAPPED,clicked_slot->bag_id,clicked_slot->id,global_floating_item_ptr->item->id,clicked_slot->item->id);
// Backup the information of the clicked on slot
Item* clicked_slot_item_pointer = clicked_slot->item;
STRING* clicked_slot_inventory_icon = str_create(clicked_slot->item->inventory_icon);
// Set the slot to the floating item
clicked_slot->item = global_floating_item_ptr->item;
clicked_panel->bmap = bmap_create(global_floating_item_ptr->item->inventory_icon);
// Set the floating item to the slot
global_floating_item_ptr->item = clicked_slot_item_pointer;
global_floating_item_ptr->floating_item_panel->bmap = bmap_create(clicked_slot_inventory_icon);
}
}
}
}
...
When I run main.c , add gold to the inventory bag, and click on the small gold coins image in the inventory bag, this is the error pop-up window I get:
Malfunction W1501
Empty pointer in slotOnClick
OK Cancel
After I click OK on the pop-up window, I see the large image of gold coins show up on the right side of the screen, with the text "Gold: " underneath it, but no value next to "Gold: ". If I comment out the: pan_setstring(gold_panel,0,598,400,gold_txt_font,gold_amnt_txt); in slotOnClick() in inventory.c , I do not get the error pop-up window. Just the large image of gold coins shows up on the right side of the screen, with the text "Gold: " underneath it, with no value next to "Gold: ". Does anyone know why the: pan_setstring(gold_panel,0,598,400,gold_txt_font,gold_amnt_txt); in inventory.c is giving an empty pointer?
Last edited by Ruben; 05/31/14 16:47.
|
|
|
Re: Use variable in separate script?
[Re: Ruben]
#441736
06/01/14 08:39
06/01/14 08:39
|
Joined: Jun 2013
Posts: 108 Alberta, Canada
CanadianDavid
Member
|
Member
Joined: Jun 2013
Posts: 108
Alberta, Canada
|
Does anyone know why the: pan_setstring(gold_panel,0,598,400,gold_txt_font,gold_amnt_txt); in inventory.c is giving an empty pointer? I suppose the first step is to figure out which parameter is NULL, right?
|
|
|
Re: Use variable in separate script?
[Re: CanadianDavid]
#441768
06/02/14 07:04
06/02/14 07:04
|
Joined: Jun 2010
Posts: 590 California
Ruben
OP
User
|
OP
User
Joined: Jun 2010
Posts: 590
California
|
Well, I changed code as follows:
// inventory.c
...
BMAP* goldPanel = "Gold_Inv_Display.pcx";
...
PANEL* gold_panel =
{
window(600,25,350,350,goldPanel,200,300);
}
function slotOnClick(PANEL* clicked_panel)
{
Slot* clicked_slot = getSlotPtrForPanelPtr(clicked_panel);
Item* clicked_item;
FONT* gold_txt_font = font_create("Times#30i");
STRING* gold_amnt_txt;
int testNum = 5;
gold_amnt_txt = str_create("Gold: ");
pan_setstring(gold_panel,0,598,400,gold_txt_font,gold_amnt_txt);
pan_setdigits(gold_panel,0,610,400,"%2.f",gold_txt_font,1,testNum);
...
}
I do not see any parameter in the pan_setdigits that does not have a value, yet I still get the same error with the pan_setdigits above. If I comment out the pan_setdigits , I get no error at all, although I only see the large "gold coins" image show up on the right side of the screen, with the text "Gold: " under it, but no value to the right of the text "Gold: ".
Last edited by Ruben; 06/02/14 07:10.
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|