❔ ✅ Access variable from another script

Im trying to make a project in WFA and i want to access a variable from my main form in a diff script. I looked it up and the only stuff i could find was for unity
68 Replies
PhoenixXeme
PhoenixXemeOP2y ago
i know my code is bad but thats not the point i just want to know how to access the stoneUpg variable in the shop script
Florian Voß
Florian Voß2y ago
if you make the cookie field static you can access from everywhere using typename:
Console.WriteLine(Form1.cookie);
Console.WriteLine(Form1.cookie);
or keeping field as it is without static you can access it on an object of type Form1:
Form1 form1;
form1.cookie
Form1 form1;
form1.cookie
PhoenixXeme
PhoenixXemeOP2y ago
i maent stoneUpg not cookie XD
PhoenixXeme
PhoenixXemeOP2y ago
@voflorian99
cap5lut
cap5lut2y ago
the same applies either way either make it static and u can access it via YourClass.NameOfIt, or u need to have an instance yourInstanceOfYourClass.NameOfIt whats the error its giving u there? nvm u now have mixed it vice versa, u have the static member, and try to access it as instance member Form1.stoneUpg would be the way to access it as static member
PhoenixXeme
PhoenixXemeOP2y ago
could we get in a vc and you show me because my brain cant work rn XD
PhoenixXeme
PhoenixXemeOP2y ago
cap5lut
cap5lut2y ago
my english skills regarding speaking/listening are really bad i can only read and write some, so voice is a no-no for me but its literally just Form1.stoneUpg instead of form1.stoneUpg in that case ._.
PhoenixXeme
PhoenixXemeOP2y ago
i tried that already
cap5lut
cap5lut2y ago
do u know the difference between the public, protected and private keywords?
PhoenixXeme
PhoenixXemeOP2y ago
no 😦 still VERY new to C#
cap5lut
cap5lut2y ago
public = its accessable from other classes protected = its accessable from sub classes (inheritence) private = noone else touch that!
PhoenixXeme
PhoenixXemeOP2y ago
cap5lut
cap5lut2y ago
static int stoneUpg does not have any of these visibility modifiers, which means it defaults to private
PhoenixXeme
PhoenixXemeOP2y ago
wait you can do public and static i didnt know that
PhoenixXeme
PhoenixXemeOP2y ago
PhoenixXeme
PhoenixXemeOP2y ago
no more errors
cap5lut
cap5lut2y ago
yeah, because they have completely differnt meaning. u can combine them ofc
PhoenixXeme
PhoenixXemeOP2y ago
so what dose stiatic do i didnt know that
cap5lut
cap5lut2y ago
do u know the difference between a class and an instance?
PhoenixXeme
PhoenixXemeOP2y ago
no XD
cap5lut
cap5lut2y ago
okay lemme take a quick ciggy break (5min) while thinking about how to explain it ;p
PhoenixXeme
PhoenixXemeOP2y ago
this was working 4 mins ago 😭
PhoenixXeme
PhoenixXemeOP2y ago
i dont understand
cap5lut
cap5lut2y ago
back so, lets think about cars Car is basically our class, its a description of our thing
public class Car
{
}
public class Car
{
}
all cars have in common that they have 4 tires right?
PhoenixXeme
PhoenixXemeOP2y ago
yea
cap5lut
cap5lut2y ago
that basically means that they are bound to the type, our class Car. thats expresses by using the static keyword:
public class Car
{
public static int TireCount = 4;
}
public class Car
{
public static int TireCount = 4;
}
PhoenixXeme
PhoenixXemeOP2y ago
so staitc values can not be changed?
cap5lut
cap5lut2y ago
now cars usually have colors, u have a red car, a blue, a yellow, etc
PhoenixXeme
PhoenixXemeOP2y ago
mhm
cap5lut
cap5lut2y ago
so this is something that is not bound to the type (Car) itself, but to each individual car basically an instance of that type
public class Car
{
public static int TireCount = 4;
public Color ColorOfThisCar;
}
public class Car
{
public static int TireCount = 4;
public Color ColorOfThisCar;
}
so now, naturally u can access the tire count via its type Car.TireCount because its bound to the type but u can not do the same for the color, right?
PhoenixXeme
PhoenixXemeOP2y ago
ahhhhhh i get it
cap5lut
cap5lut2y ago
Car redCar = new Car();
redCar.ColorOfThisCar = red;

Car yellowCar = new Car();
redCar.ColorOfThisCar = yellow;
Car redCar = new Car();
redCar.ColorOfThisCar = red;

Car yellowCar = new Car();
redCar.ColorOfThisCar = yellow;
(ofc this is more pseudo code than real code) so both redCar and yellowCar are instances (also called objects) of the type Car
PhoenixXeme
PhoenixXemeOP2y ago
ok
cap5lut
cap5lut2y ago
my tire count thingy was simply a rough explanation. static members can be changed as well
PhoenixXeme
PhoenixXemeOP2y ago
ok
cap5lut
cap5lut2y ago
my explanation was only about where that member belongs to, static to the type itself, non-static to each instance/object
PhoenixXeme
PhoenixXemeOP2y ago
so Form1 is a instance?
cap5lut
cap5lut2y ago
Form1 is the type, not the instance itself so here stoneUpg has to be static or else the compiler would complain
PhoenixXeme
PhoenixXemeOP2y ago
ok i get it
PhoenixXeme
PhoenixXemeOP2y ago
new problems
PhoenixXeme
PhoenixXemeOP2y ago
this is in Form1.cs
cap5lut
cap5lut2y ago
that part gives the reason, if u scroll down a bit
PhoenixXeme
PhoenixXemeOP2y ago
PhoenixXeme
PhoenixXemeOP2y ago
ik but i have no idea how to fix it
PhoenixXeme
PhoenixXemeOP2y ago
like i closed it
cap5lut
cap5lut2y ago
that means that another program has that file opened
PhoenixXeme
PhoenixXemeOP2y ago
how do i find out what has it open
PhoenixXeme
PhoenixXemeOP2y ago
because this is the code
cap5lut
cap5lut2y ago
oh, its not even another program, but urs itself
PhoenixXeme
PhoenixXemeOP2y ago
? please explain
cap5lut
cap5lut2y ago
well, u open the file with read permissions using the StreamReader, afterwards u try to open the file again with write access, this is bound to fail
PhoenixXeme
PhoenixXemeOP2y ago
i dont understand sorry
cap5lut
cap5lut2y ago
u should open it for read and write acces and simply use it based on that (need to dig through documentation quick)
PhoenixXeme
PhoenixXemeOP2y ago
yea i have no clue what your talking about
cap5lut
cap5lut2y ago
what im saying is more or less "u cant use it like that"
PhoenixXeme
PhoenixXemeOP2y ago
ok
cap5lut
cap5lut2y ago
if u "open" a file u have choices on how to open it depending on ur needs. mostly its "split" between read access and write access. imagine 3 people standing around a piece of paper. as long as everyone only wants to read everything is fine but now think about if all of those 3 want to write their text onto that piece of paper, letter by letter, it will become a mess so using a stream reader/writer also implies their intent what they want to do with the file and the OS sorta "locks" the usage to just that. because u want to read in first place (stream reader) its opened as "i only want to read" but then u open it also for writing, where it then explodes
PhoenixXeme
PhoenixXemeOP2y ago
so i need to move this line
cap5lut
cap5lut2y ago
well, basically u cant use neither StreamReader nor StreamWriter like that u would have to open the stream with the read+write access and then create the reader and writer from that stream
PhoenixXeme
PhoenixXemeOP2y ago
how please tell me how i do that
cap5lut
cap5lut2y ago
as u want to also write to the file u need write permissions, these usually also imply read permissions, so it would result in
Stream stream = File.OpenWrite(filePath);
Stream stream = File.OpenWrite(filePath);
generally u should create this and the reader/writer in the method directly, not as member (especially not as static member)
PhoenixXeme
PhoenixXemeOP2y ago
yea i give up i have no idea what im doing and i have being trying to fix this all day
cap5lut
cap5lut2y ago
generally i would say, u should use some simple console applications for now, to test this stuff. GUI is quite complex and to me it feels like u r lacking quite same basics. u could also for a "quick and dirty" fix use the void File.WriteAllText(string path, string content) and string File.ReadAllText(string path) methods but the main issue is stil the mentioned lack @PhoenixXeme use $close to mark this thread as answered 😉
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
PhoenixXeme
PhoenixXemeOP2y ago
cap5lut
cap5lut2y ago
seems the bot is broken for now, so just ignore it ;p more or less its already marked as resolved anyway ;p
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server