C
C#13mo ago
PhoenixXeme

❔ ✅ 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
PhoenixXeme13mo 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ß13mo 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
PhoenixXeme13mo ago
i maent stoneUpg not cookie XD
PhoenixXeme
PhoenixXeme13mo ago
@voflorian99
cap5lut
cap5lut13mo 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
PhoenixXeme13mo ago
could we get in a vc and you show me because my brain cant work rn XD
PhoenixXeme
PhoenixXeme13mo ago
cap5lut
cap5lut13mo 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
PhoenixXeme13mo ago
i tried that already
cap5lut
cap5lut13mo ago
do u know the difference between the public, protected and private keywords?
PhoenixXeme
PhoenixXeme13mo ago
no 😦 still VERY new to C#
cap5lut
cap5lut13mo ago
public = its accessable from other classes protected = its accessable from sub classes (inheritence) private = noone else touch that!
PhoenixXeme
PhoenixXeme13mo ago
cap5lut
cap5lut13mo ago
static int stoneUpg does not have any of these visibility modifiers, which means it defaults to private
PhoenixXeme
PhoenixXeme13mo ago
wait you can do public and static i didnt know that
PhoenixXeme
PhoenixXeme13mo ago
PhoenixXeme
PhoenixXeme13mo ago
no more errors
cap5lut
cap5lut13mo ago
yeah, because they have completely differnt meaning. u can combine them ofc
PhoenixXeme
PhoenixXeme13mo ago
so what dose stiatic do i didnt know that
cap5lut
cap5lut13mo ago
do u know the difference between a class and an instance?
PhoenixXeme
PhoenixXeme13mo ago
no XD
cap5lut
cap5lut13mo ago
okay lemme take a quick ciggy break (5min) while thinking about how to explain it ;p
PhoenixXeme
PhoenixXeme13mo ago
this was working 4 mins ago 😭
PhoenixXeme
PhoenixXeme13mo ago
i dont understand
cap5lut
cap5lut13mo 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
PhoenixXeme13mo ago
yea
cap5lut
cap5lut13mo 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
PhoenixXeme13mo ago
so staitc values can not be changed?
cap5lut
cap5lut13mo ago
now cars usually have colors, u have a red car, a blue, a yellow, etc
PhoenixXeme
PhoenixXeme13mo ago
mhm
cap5lut
cap5lut13mo 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
PhoenixXeme13mo ago
ahhhhhh i get it
cap5lut
cap5lut13mo 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
PhoenixXeme13mo ago
ok
cap5lut
cap5lut13mo ago
my tire count thingy was simply a rough explanation. static members can be changed as well
PhoenixXeme
PhoenixXeme13mo ago
ok
cap5lut
cap5lut13mo ago
my explanation was only about where that member belongs to, static to the type itself, non-static to each instance/object
PhoenixXeme
PhoenixXeme13mo ago
so Form1 is a instance?
cap5lut
cap5lut13mo ago
Form1 is the type, not the instance itself so here stoneUpg has to be static or else the compiler would complain
PhoenixXeme
PhoenixXeme13mo ago
ok i get it
PhoenixXeme
PhoenixXeme13mo ago
new problems
PhoenixXeme
PhoenixXeme13mo ago
this is in Form1.cs
cap5lut
cap5lut13mo ago
that part gives the reason, if u scroll down a bit
PhoenixXeme
PhoenixXeme13mo ago
PhoenixXeme
PhoenixXeme13mo ago
ik but i have no idea how to fix it
PhoenixXeme
PhoenixXeme13mo ago
like i closed it
cap5lut
cap5lut13mo ago
that means that another program has that file opened
PhoenixXeme
PhoenixXeme13mo ago
how do i find out what has it open
PhoenixXeme
PhoenixXeme13mo ago
because this is the code
cap5lut
cap5lut13mo ago
oh, its not even another program, but urs itself
PhoenixXeme
PhoenixXeme13mo ago
? please explain
cap5lut
cap5lut13mo 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
PhoenixXeme13mo ago
i dont understand sorry
cap5lut
cap5lut13mo ago
u should open it for read and write acces and simply use it based on that (need to dig through documentation quick)
PhoenixXeme
PhoenixXeme13mo ago
yea i have no clue what your talking about
cap5lut
cap5lut13mo ago
what im saying is more or less "u cant use it like that"
PhoenixXeme
PhoenixXeme13mo ago
ok
cap5lut
cap5lut13mo 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
PhoenixXeme13mo ago
so i need to move this line
cap5lut
cap5lut13mo 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
PhoenixXeme13mo ago
how please tell me how i do that
cap5lut
cap5lut13mo 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
PhoenixXeme13mo ago
yea i give up i have no idea what im doing and i have being trying to fix this all day
cap5lut
cap5lut13mo 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
MODiX13mo ago
Use the /close command to mark a forum thread as answered
PhoenixXeme
PhoenixXeme13mo ago
cap5lut
cap5lut13mo ago
seems the bot is broken for now, so just ignore it ;p more or less its already marked as resolved anyway ;p
Accord
Accord13mo 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
More Posts