How to make a variable known thought out all scenes? - xcode

I want to put money in the game like a bank account the problem is that when I put the variable "BankAccount" which is attached to an Interger in the GameScene or the game view controller only those to know what "account" is. When I go to my secondScene it doesn't know what account is. So. I won't every scene to be able to access/know what "account" is. And if I put account as a variable in all scenes then now they are not the same account. I don't know what to do?

You have to make sure that your variable is outside of any functions or classes, unless you want to use those methods to call the variable.
Variables defined outside of any blocks will be available to the entire application.
If your var is defined inside a block then you need to use that block to call it.
struct getMoney {
var money: Int = 100
}
You could access it like...
var playerMoney = getMoney.money

Related

CAPL on sysvar_change procedure with sysvar of custom struct data type

according to the manual,
The procedure on sysVar is called only when the value of the variable
changes. It can also be written as on sysVar_change. If you want to be
notified of value updates to the variable which don’t change the
value, you should use on sysVar_update instead.
In my example scenario, I have a system variable s::sysv of custom Struct Data Type X, where X has two fields: A and B.
In my CAPL script I put the following:
on sysvar_change s::sysv.A
{
// do stuff
}
Expected output is to do stuff only when s::sysv.A changes. However, since s::sysv.B is often updated when my simulation is running, then the procedure on sysvar_change s::sysv.A is called a lot more times than I expect, even if A doesn't change its value.
I don't understand why, and I'm putting a lot of workaround in place to avoid this, can anybody help?
Edit:
according to one reply, the event handler is not the struct element, but still the variable. However, the keyword this is now pointing to the struct element and not to the variable.
This bit of the manual is also relevant:
You can also react in the same way to value changes of specific
elements of a system variable of type struct or generic array. For
this, add the element to the name of the variable.
I have tried this functionality in the latest CANoe and it works as expected. The following is my code.
on key 'a'
{
#sysvar::Var_Struct1.StructMem1++;
}
on key 'b'
{
#sysvar::Var_Struct1.StructMem2++;
}
on sysvar_change Var_Struct1.StructMem1
{
write("StructMem1 value changed");
}
on sysvar_change Var_Struct1.StructMem2
{
write("StructMem2 value changed");
}
Whenever I press the key 'a' or 'b', the corresponding event is triggered.
Your variable is s::sysv. The event handler is called whenever the value of the variable changes. No matter whether A or B changes.
There is no way to restrict it only to certain changes of the value.
This is similar to the fact that you can also not be notified when, e.g. only the 3rd bit of an integer changes.
To me, it seems best to reconsider your setup and ask yourself, whether using the struct is the right approach, or whether it might be better to use two separate system variables A and B.

Buildup and reference of objects in HP UFT

I'm wondering how I can access properties/methods via console/watch.
I have the following code:
Dim page
page = Browser("Welcome: Mercury Tours").Page
Now I want to obtain the title of this Page. Since I inspected the Page object with Object Spy and I saw it has a title property.
When I enter page.title in my watch however, it tells me that page does not contain the property.
1. What is the correct syntax?
2. Why is this not working? I presume that the watch is checking for VBScript object properties instead of TestObject properties?
(I have a programming background and I find it very confusing that I have VBObjects and TestObjects simply walking through the same file. It kind of feels like a black box :/)
Ok, well, your syntax is incorrect...
It appears that you're trying to put something into a variable called "page", but I'm not sure if I can figure out your intention.
If you are trying to put the page object into the var "page", you would need to use a set statement (to indicate to vbscript that it's going to hold an object, not just a single piece of data)...
Regardless of that, your syntax for specifying the Page is wrong.
In your example, you're specifying a browser test object called "Welcome: Mercury Tours" from the repository... but then you put .Page - and that's where your syntax error is.
It helps to understand the difference between Test Objects and Realtime Objects - because you need to specify a page Test Object. You can do that by specifying a page object from the Object Repository, or you can do it descriptively.
Test Objects are descriptions of real objects that QTP tries to find. If it successfully finds a real object that matches the description, then the Test Object kind of (virtually) "attaches to" the real object... then, you can use the test object to query the real attributes of the real object that it attached to.
Sincel you're clearly doing the tutorial, your object repository probably has a Page test object in the heiarchy under the browser object... (and if you had let Intellisense help, it would show you a list of pages to choose from while you type...). If so, you would specify the page object like this:
Browser("Welcome: Mercury Tours").Page("PageObjectNameHere")
If you would prefer to use descriptive programming, you could instead type something like:
Browser("Welcome: Mercury Tours").Page("Title:=Welcome: Mercury Tours")
Changing your syntax to either of those constructs would let you proceed with the next part of solving your question - how to get some data from the page...
So, once you have address the page test object correctly, then you can specify a method to get information from it... such as .GetROProperty()
You can choose from many properties for a page... If you examine a page using GUISPY, it pretty much gives you a list of the properties available to query... For example, if you want to check the URL of the page that's displayed, you could specify
Browser("Welcome: Mercury Tours").Page("Title:=Welcome: Mercury Tours").GetROProperty("url")
This, of course returns a value, so you want to do something with it... like assign it to a variable
result = Browser("Welcome: Mercury Tours").Page("Title:=Welcome: Mercury Tours").GetROProperty("url")
(If you do this, you can then add the variable "result" to the watch list... which answers your question.)
or examine it directly in your code
if Browser("Welcome: Mercury Tours").Page("Title:=Welcome: Mercury Tours").GetROProperty("url") = url_to_compare then DoSomething()
I hope this helps to clear up your understanding :)

Codeigniter - Where should I store an array of reserved usernames?

I'm building a site and would like to create a list of reserved usernames to keep people from creating usernames like account, index, profile and others. I already have my list, I'm just not sure where in Codeigniter to store this data/array.
I'm pretty familiar with Codeingiter and I like to keep things where they are suppose to be. Helpers, libraries and configs just don't seem like places to store an array of reserved variables... but maybe i'm wrong.
I would appreciate suggestions! Thanks in advance.
It depends on your need and preference, config is right but helper is also right because if you keep it in helper file then you may also create a helper function right there, for example
function is_reserved_username($username)
{
$reserved_words = array('account', 'index');
return in_array($username, $reserved_words);
}
So, from anywhere, you can use
if(is_reserved_username($this->input->post('username'))) {
// it's a reserved word
}
Also, if you are using your own base controller (MY_Controller) then you may keep it in that base controller, so it'll be available in every classes and you can access it using something like
In MY_Controller if it's available as
$reserved_words = array('account', 'index');
Use it from a controller/model
if(in_array($username, $this->reserved_words)) {
// it's a reserved word
}
I don't think there is any "right" way to do this. Personally I would just create a table in my database. I'd then create a function that would check this table for reserved names when a new user is registering and return TRUE if the username isn't reserved and FALSE if it is reserved

Is there a way to get the current object and method with CodeIgniter?

I'm looking for a function that would tell me the current object and method by way of the URI. Normally, I would use $this->uri->uri_string(), however, I do not want to pass any "dynamic" segments. For example, a URI of 'products/shoes/123', would be 'products/view_product'.
I want to be able to do this so I can create a config file containing page titles... since I use a model to output my page header. It is called from MY_Controller.php in the construct. For example: $this->template->overall_header($title = "View Product")...
but in the construct, it would be:
$this->template->overall_header($title = $this->config->item($object_method_string));
Anybody have any solutions? Thanks for your time.
for current method
$method= $this->router->fetch_method();
I hope you are talking about this
To expand on Nishant's answer you can access the attributes from the router class.
So for current object;
$this->router->class
And for current method;
$this->router->method

Just how global are Coldfusion variables not declaring using “var”?

I’m using Coldfusion MX 8. I recently had a situation where variables seem to be “swapping” between sessions. I found some information regarding entire sessions swapping, but this was not the case. It was just one variable being swapped, not the entire session. My code snippets follow:
var idArray = ListToArray(arguments.event.getArg("itemIDs"));
var oItemDetail = 0;
var oItem = 0; //Inserting this line seems to have fixed the error.
var i = 0;
for (i=1;i lte ArrayLen(idArray);i=i+1) {
//Log Spot #1 – cflog idArray[i] and arguments.event.getArg("statusNotes")
oItem = getItemService().getItem(idArray[i]);
oItemDetail = getItemService().getItemDetail();
oItemDetail.setItemID(oItem.getItemID());
oItemDetail.setStatusNotes(arguments.event.getArg("statusNotes"));
getItemService().saveItem(oItem);
getItemService().saveItemDetail(oItemDetail);
}
//getItem and getItemDetail just call getTransfer().get()
//saveItem and saveItemDetail just call getTransfer().save()
For example, at Log Spot #1, idArray[i] might have been “1”, and the StatusNotes event arg might be “abc”.
But should another person, in another session, using another login, in another place, another browser, etc.etc. Use the function at exactly the same time, using idArray[i] = “2” and statusNotes = “def”, then Item Detail “abc” might instead attach to Item “2”, and Item Detail “def” attach to Item “1”.
The fact that, at Log Spot #1, the variables logged are correct, but in the database they are swapped, points to these lines of code as the suspects.
This problem has gone away by declaring “var oItem” at the top.
So I guess I’m a little shocked by this revelation. I would assume that not declaring my local variables would mean another variable, with the same name, in another function, but in the same session might get overwritten. But this seems to be some sort of internal memory issue. The variables are not even being overwritten, rather swapped, between sessions!
I was wondering if anyone had any similar experiences and could shed some light on this?
Unvar'd variables are made private variables within the object that they are contained in. Which causes two problems,
They are shared (accessed and written to) by functions (within that same component)
They live beyond the life of the function call
When you var a variable it makes it a local variable only to that function. Only that function can use it and it only lives as long as that function does.
In your case, this problem doesn't really have anything to do with sessions, other than that is the persistent scope where you happen to be storing the data from these functions.
You said
I would assume that not declaring my local variables would mean another variable, with the same name, in another function, but in the same session might get overwritten.
But it would be more accurate to say
I would assume that not declaring my local variables would mean another variable, with the same name, in another function, but in the same OBJECT might get overwritten.

Resources