Need help making a Zscript Menu

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
Mirvini
Posts: 5
Joined: Tue Jan 26, 2021 7:43 pm

Need help making a Zscript Menu

Post by Mirvini »

I want to make a simple menu, that contains a list of all the "documents" a player has found and lets you select it to read them.

Problem is Menus are really difficult to understand.

The idea I was going with was to make a Menu class inheriting from GenericMenu then iterating through my list of documents to create a new list item for each one.

Heres where things get confusing.
All the exampels in zdoom.p3k seem to use a MenuItem with a MenuItemBase and they seem to kind of just run themselves off of MENUDEF. So I can't for the life of me figure out how to initialize one manually from the code.

For example I tried making a class called DocumentMenuItem extending MenuItemBase but if i just say do this

Code: Select all

DocumentMenuItem new_item;
new_item.init(stuff...);
I get an error when running: "Tried to read from address zero"
This error happens if i call ANY method on the new_item.

Rather than post all of my code, which is mostly copy and pasting what I think is correct, I was hoping to hear some guidence on how to do this properly.
User avatar
Enjay
 
 
Posts: 26971
Joined: Tue Jul 15, 2003 4:58 pm
Location: Scotland
Contact:

Re: Need help making a Zscript Menu

Post by Enjay »

Would Nash's PDA starter kit be of any use to you?

viewtopic.php?f=105&t=65849
User avatar
Player701
 
 
Posts: 1709
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support
Contact:

Re: Need help making a Zscript Menu

Post by Player701 »

Mirvini wrote:For example I tried making a class called DocumentMenuItem extending MenuItemBase but if i just say do this

Code: Select all

DocumentMenuItem new_item;
new_item.init(stuff...);
I get an error when running: "Tried to read from address zero"
This error happens if i call ANY method on the new_item.
Let me explain what's going on here.
Mirvini wrote:

Code: Select all

DocumentMenuItem new_item;
Here you define a local variable called new_item, which is essentially a pointer to an instance of DocumentMenuItem. But you don't assign any value to your variable.
Mirvini wrote:

Code: Select all

new_item.init(stuff...);
And here you dereference new_item by calling a method on the instance of DocumentMenuItem it points to, but since you haven't initialized the variable, it doesn't actually point to an instance. That's why you get an error.

You have to assign a value to the variable first, either by making it point to an existing instance, or by creating a new instance of DocumentMenuItem (I assume you wanted the latter):

Code: Select all

DocumentMenuItem new_item = new('DocumentMenuItem');
new_item.init(stuff...);
Note that you can use an implicit type declaration here because the resulting type of the new expression on the right-hand side is known at compile time:

Code: Select all

let new_item = new('DocumentMenuItem');
And yes, you may want to check out Enjay's link as well as Marrub's documentation project for some useful general info on the ZScript language itself if you're having issues understanding it. Don't be afraid to ask questions too. :)
Mirvini
Posts: 5
Joined: Tue Jan 26, 2021 7:43 pm

Re: Need help making a Zscript Menu

Post by Mirvini »

Ah , so that's how you call the constructor. After calling new DocumentMenuItem() didn't work I couldn't seem to find any examples of how to construct a new object.
User avatar
Player701
 
 
Posts: 1709
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support
Contact:

Re: Need help making a Zscript Menu

Post by Player701 »

Yes, but do note that not everything can be constructed this way: actor classes should be instantiated via Spawn, and some built-in classes have static constructor methods (e.g. ThinkerIterator.Create for thinker iterators).
Mirvini
Posts: 5
Joined: Tue Jan 26, 2021 7:43 pm

Re: Need help making a Zscript Menu

Post by Mirvini »

Does Zscript cover garbage collection, or will I also need to Destroy the items in the array of menuItems afterward when I use the new("blah") constructor?
User avatar
Player701
 
 
Posts: 1709
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support
Contact:

Re: Need help making a Zscript Menu

Post by Player701 »

Yes, normal objects should be destroyed and GC'd automatically after they've gone out of scope. This does not apply to thinkers (including actors, which are also a type of thinker) and event handlers - their lifecycle is managed by the engine and you should neither create nor destroy them manually (unless you want to permanently disable your handler for some reason, but that is a very special case, see here for details).
Post Reply

Return to “Scripting”